R.K.
0
Q:

hashmaps java

import java.util.HashMap;
//Within a class
//You can do new HashMap<Key Type, Value Type>();, but you don't need to
HashMap<Int, String> examplehashmap=new HashMap<>();
{
//put in values
 examplehashmap.put(5, "example");
};
//get value
examplehashmap.get(5);
//returns "example"
7
import java.util.HashMap;

HashMap<String, Integer> key = new HashMap<String, Integer>();
key.put("abcd", 5);

System.out.println(key.get(5)); //prints out abcd
2
import java.util.Map;
import java.util.HashMap;

Map<String, Integer> key = new HashMap<>();
key.put("addedVal", 245);

System.out.println("" + key.get("addedVal")); //prints out 245
System.out.println("" + key.getOrDefault("addedVal", 0)); //prints out 245, key could be found
System.out.println("" + key.getOrDefault("asff", 0)); //prints out 0, no given key found in map
1
import java.util.HashMap; // import the HashMap class

HashMap<String, String> capitalCities = new HashMap<String, String>();
3
import java.util.Map; // import Map Interface
class MyClass {
    public static void main( String args[] ) {
    HashMap<Integer, String> shapes = new HashMap<Integer,String>(); // Create an ArrayList object with string data type
    }
}
  
-1

New to Communities?

Join the community