Map<String, String> myMap = new HashMap<String, String>() {{
put("a", "b");
put("c", "d");
}};
//remember to first import java.util.*; first
//you can swap out string or integer for other data types
Map<String, Integer> d = new HashMap<>();
HashMap<Integer,String> map=new HashMap<Integer,String>();//Creating HashMap
import java.util.HashMap;
class Main {
public static void main(String[] args) {
// create a hashmap
HashMap<String, Integer> languages = new HashMap<>();
// add elements to hashmap
languages.put("Java", 8);
languages.put("JavaScript", 1);
languages.put("Python", 3);
System.out.println("HashMap: " + languages);
}
}
// this works for up to 10 elements:
Map<String, String> test1 = Map.of(
"a", "b",
"c", "d"
);
// this works for any number of elements:
import static java.util.Map.entry;
Map<String, String> test2 = Map.ofEntries(
entry("a", "b"),
entry("c", "d")
);