[JAVA] Map method

I will also summarize the Map method as a review. It will be updated as needed.

put(K key, V value) Associates the specified value with the specified key on this map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
System.out.println(map);

//The key and value are set at the same time as the map is initialized,
//It is OK to execute separately as follows.
//map.put("apple", "Apple");
//map.put("orange", "Orange");
//map.put("banana", "banana");
{orange=Orange, banana=banana, apple=Apple}

get(Object key) Returns the value to which the specified key is mapped. Returns null if this map does not contain a mapping for that key.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
System.out.println(map.get("apple"));
System.out.println(map.get("app"));
Apple
null

size() Returns the number of key-value mappings in this map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
System.out.println(map.size());
3

containsKey(Object key) Returns true if the specified key mapping is included in this map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
System.out.println(map.containsKey("orange"));
System.out.println(map.containsKey("banana"));
System.out.println(map.containsKey("oge"));
true
true
false

containsValue(Object value) Returns true if the map maps one or more keys to the specified value.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
System.out.println(map.containsValue("Apple"));
System.out.println(map.containsValue("Orange"));
System.out.println(map.containsValue("range"));
true
true
false

clear() Remove all mappings from the map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
map.clear()
System.out.println(map);
{}

entrySet() Returns a Set view of the mappings contained in this map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
System.out.println(map.entrySet());
[orange=Orange, banana=banana, apple=Apple]

keySet() Returns a Set view of the keys contained in this map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
//You can get the key at once
System.out.println(map.keySet());

//To get the keys one by one, use the extended for statement as shown below.
for(String s : map.keySet()) {
	System.out.println(s);
}
[orange, banana, apple]

orange
banana
apple

values() Returns a Collection view of the values contained in this map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
//You can get the value at once
System.out.println(map.values());

//If you want to get the values one by one, use the extended for statement as shown below.
for(String s : map.values()) {
	System.out.println(s);
}
[Orange,banana,Apple]

Orange
banana
Apple

remove(Object key) Remove the key mapping (if any) from this map.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
map.remove("orange");
System.out.println(map);
//orange has been removed
{banana=banana, apple=Apple}

remove(Object key, Object value) Deletes the entry for the specified key only if the specified key is currently mapped to the specified value.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
//If you set a value that does not exist, it will not be deleted and will remain as it is
map.remove("orange","Olesi");
System.out.println(map);

//If you set a key that does not exist, it will not be deleted and will remain as it is
map.remove("oge","Orange");
System.out.println(map);

//If you set an existing key and value, it will be deleted
map.remove("orange","Orange");
System.out.println(map);
{orange=Orange, banana=banana, apple=Apple}
{orange=Orange, banana=banana, apple=Apple}
{banana=banana, apple=Apple}

replace(K key, V value) Replaces the entry for that key only if the specified key is currently mapped to some value.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
//If you set a key that does not exist, the Map remains as it is
map.replace("bana", "Banana Nanana");
System.out.println(map);

//If you set an existing key, the value associated with it will be replaced.
map.replace("banana", "Banana Nanana");
System.out.println(map);
{orange=Orange, banana=banana, apple=Apple}
{orange=Orange, banana=Banana Nanana, apple=Apple}

replace(K key, V oldValue, V newValue) Replaces the entry for the specified key only if it is currently mapped to the specified value.

Map<String,String> map = new HashMap<String,String>() {

			{	put("apple", "Apple");
				put("orange", "Orange");
				put("banana", "banana");
			}
};
//If you specify an old value that does not exist, it will not be replaced
map.replace("bana", "Bana","Natto");
System.out.println(map);

//Specifying an existing key and value replaces the value
map.replace("banana", "banana","Natto");
System.out.println(map);
{orange=Orange, banana=banana, apple=Apple}
{orange=Orange, banana=Natto, apple=Apple}

isEmpty() Returns true if this map does not retain a key-value mapping.

Map<String,String> map = new HashMap<String,String>();

System.out.println(map.isEmpty());
true

Recommended Posts

Map method
[Practice] Map method
ruby map method
Method
Stream API map method
About the map method
Java method
to_i method
getRequestDispatcher () method
merge method
include method
Abstract method
initialize method
JAVA (Map)
List method
puts method
Java method
Class method
save! method
getParameter method
[Java] method
private method
rails method
[Java] method
[Rails] How to use the map method
List, Set, Map
Ruby to_s method
Screen transition method
Binary search binary search method
Method to search
Factory Method Pattern
Exception switching method
RxSwift method chain
Rails delegate method
scan method problem
Java8 method reference
[Ruby] slice method
[Ruby] Method memorandum
[HTTP method PATCH]
Array / list / map
About the method
[Java] forEach method
[Servlet] Basic method
[Java] Map comparison
variable and method
Binary search method
Template Method pattern
Template Method Pattern
0-filled (non-zero) method
JavaSilver11 study method
keycloak test-skip method
Factory Method pattern
[Ruby] initialize method
Ruby build method
undefined method `** _ path'error
java8 method reference
[Java] Random method
Ruby accessor method
[Java] split method