Map cannot specify primitives as types, so specify wrapper classes such as Integer and String.
Map<Integer, String> map1 = new HashMap<Integer, String>();
Object name.put (key, "value")
map1.put(1, "apple");
map1.put(2, "orange");
map1.put(3, "cherry");
Object name.get (key)
map1.get(1);
map1.get(2);
map1.get(3);
System.out.println(map1.get(1));
//The output result is"cherry"become
** * If the key is duplicated **
map1.put(1, "apple");
map1.put(2, "orange");
map1.put(1, "cherry");
System.out.println(map1.get(1));
//The output result is"cherry"become
Elements in Map manage values by key, so they cannot be duplicated. When you put a value in a duplicate key with put, it will be replaced with the previous one.
When deleting one Object name.remove (key)
map1.put(1, "apple");
map1.put(2, "orange");
map1.put(3, "cherry");
map1.remove(1);
System.out.println(map1);
//The output result is{2=orange, 3=cherry}Becomes
When deleting all Object name.clear ()
map1.put(1, "apple");
map1.put(2, "orange");
map1.put(3, "cherry");
map1.clear();
System.out.println(map1);
//The output result will be {}
map1.put(1, "apple");
map1.put(2, "orange");
map1.put(3, "cherry");
map1.replace(2, "banana");
System.out.println(map1.get(2));
//The output result will be banana
Returns the key that exists in the map Object name.keySet ()
map1.put(1, "apple");
map1.put(2, "orange");
map1.put(3, "cherry");
System.out.println(map1.size());
//The output result is[1, 2, 3]become
Returns the number of keys that exist in the map Object name.keySet ()
map1.put(1, "apple");
map1.put(2, "orange");
map1.put(3, "cherry");
System.out.println(map1.size());
//The output result will be 3
Recommended Posts