[Java] How to use Map

Initialization

Map cannot specify primitives as types, so specify wrapper classes such as Integer and String.

Map<Integer, String> map1 = new HashMap<Integer, String>(); 

Add element

Object name.put (key, "value")

map1.put(1, "apple"); 
map1.put(2, "orange");
map1.put(3, "cherry"); 

Get element

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.

Delete element

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 {}

Element replacement

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

Get the key

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

Get the number of keys

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

[Java] How to use Map
[Java] How to use Map
How to use Java Map
How to use Map
How to use map
How to use Map
How to use java Optional
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to use string.format
How to use Java variables
[Java] How to use Optional ①
How to use Java HttpClient (Get)
How to use Java HttpClient (Post)
[Java] How to use join method
[Processing × Java] How to use variables
[Java] How to use LinkedHashMap class
[JavaFX] [Java8] How to use GridPane
How to use class methods [Java]
[Java] How to use List [ArrayList]
How to use classes in Java?
[Processing × Java] How to use arrays
How to use Java lambda expressions
[Java] How to use Math class
How to use Java enum type
Multilingual Locale in Java How to use Locale
[Java] How to use the File class
How to use rbenv
[Java] How to use the hasNext function
How to use letter_opener_web
How to use with_option
How to use fields_for
How to use java.util.logging
How to use submit method (Java Silver)
[Java] How to use the HashMap class
[Rails] How to use the map method
How to use collection_select
[Easy-to-understand explanation! ] How to use Java instance
[Java] How to use the toString () method
Studying how to use the constructor (java)
[Processing × Java] How to use the loop
How to use Twitter4J
How to use active_hash! !!
How to use MapStruct
How to use hidden_field_tag
How to use TreeSet
[How to use label]
How to use identity
How to use hashes
How to use Java classes, definitions, import
[Easy-to-understand explanation! ] How to use Java polymorphism
[Java] [Maven3] Summary of how to use Maven3
[Processing × Java] How to use the class
How to use Java Scanner class (Note)
How to use JUnit 5
[Processing × Java] How to use the function
[Easy-to-understand explanation! ] How to use ArrayList [Java]
How to use Dozer.mapper
[Java] How to use the Calendar class
How to use Gradle