티스토리 뷰

개발/Java

[Java] Map

ddoddi14 2023. 1. 27. 18:13

Map

  • SortedMap
    • TreeMap
  • HashTable
  • LinkedHashMap
  • HashMap

 

Interface Map<K,V>

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

 

Map.Entry<K,V>

Map<String,Integer> map = new HashMap<String,Integer>();
map.put("apple",1);
map.put("tomato",2);
map.put("banana",3);

Set<Map.entry<String,Integer>> entries=map.entrySet();
for(Map.entry<String,Integer> entry:entries){
	Sytem.out.println(entry.getKey()+", "+entry.getValue())
}

//apple, 1
//tomato, 2
//banana, 3

 

HashMap.forEach()

public void forEach(Biconsumer<? super K, ? super V> action)
//	Biconsumer is a functional interface that takes two arguments and does not return.

Map<String,Integer> map = new HashMap<String,Integer>();
map.put("apple",1);
map.put("tomato",2);
map.put("banana",3);

map.forEach((key,value)
	-> System.out.println("key : "+key+", value : "+value)
    );

map.entrySet().forEach((entry)
	-> System.out.println("key : "+entry.getKey()+", value : "+entry.getValue())
    
//key : apple, value : 1
//key : tomato, value : 2
//key : banana, value : 3
최근에 올라온 글
Total
Today
Yesterday