ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [JAVA] Map 정리(LinkedHashMap, containKey, keySet(),Map 합치기putAll,순서보장하는 Map)
    WEB/BACK 2021. 12. 6. 14:47
    반응형

    오늘은 Map 정리를 해보도록 하겠습니다. 고고

    1. Map 생성 (PUT)

    //map1 생성 put
    Map<String,Integer> map1=new HashMap();
    map1.put("map1_key1",1);
    map1.put("map1_key2",2);
    map1.put("map1_key3",3);
    map1.put("map1_key4",4);
    map1.put("map1_key4",5);//겹치는 key 가 있다면 새로운 key로 대체된다.
    System.out.println("map1은 "+map1);

    map1은 {map1_key2=2, map1_key1=1, map1_key4=5, map1_key3=3}

    map1_key4가 겹치기때문에 마지막 으로 입력한 key가 들어갔음을 확인할 수 있다.

    입력한 순서대로 나오지 않는것을 확인할 수 있다. 입력한 순서대로 하려면 LinkedHashMap을 사용하여야한다.

    2. GET

    //get
    System.out.println("map1_key1의 value는? ="+map1.get("map1_key1"));

    map1_key1의 value는? =1

    get("key")는 key에 해당하는 value값을 보여줍니다.

     

    3. containKey, containValue

    //containKey
    System.out.println("containkey " + map1.containsKey("map1_key1"));
    
    //containValue
    System.out.println("contain Value "+ map1.containsValue(4));

    containkey true
    contain Value false

    존재하면 True, 아니면 false를 반환한다.

     

    4. map 합치기 putAll

    //map2생성
    Map<String,Integer> map2=new HashMap();
    map2.put("map2_key1", 11);
    map2.put("map2_key2", 22);
    System.out.println(map2);
    
    //map2에 map1을 합치기
    map2.putAll(map1);
    System.out.println("map1과 map2합치기 결과 :"+map2);
    
    Map<String,Integer> map3=new HashMap(map2);
    System.out.println("생성하면서 map3에 담기, map3 = "+map3);

    {map2_key2=22, map2_key1=11}


    map1과 map2합치기 결과 :{map2_key2=22, map1_key2=2, map2_key1=11, map1_key1=1, map1_key4=5, map1_key3=3}


    생성하면서 map3에 담기, map3 = {map2_key2=22, map1_key2=2, map2_key1=11, map1_key1=1, map1_key4=5, map1_key3=3}

     

    5. LinkedHashMap

    입력한대로 출력하고싶을때 사용한다.

    //순서 보장하는 linkedHaashMap 자료가 입력된 순서를 기억한다.
    LinkedHashMap<String, String> lm = new LinkedHashMap<String, String>(); 
    lm.put("lm_key1", "one");
    lm.put("lm_key2", "two");
    lm.put("lm_key3", "three");
    lm.put("lm_key4", "four");
    lm.put("lm_key4", "four2");
    System.out.println("순서 보장하는 linkedHashMap : " + lm);

    순서 보장하는 linkedHashMap : {lm_key1=one, lm_key2=two, lm_key3=three, lm_key4=four2}

     

    전체코드

    	public static void main(String[] args) {
    		//map1 생성 put
    		Map<String,Integer> map1=new HashMap();
    		map1.put("map1_key1",1);
    		map1.put("map1_key2",2);
    		map1.put("map1_key3",3);
    		map1.put("map1_key4",4);
    		map1.put("map1_key4",5);//겹치는 key 가 있다면 새로운 key로 대체된다.
    		System.out.println("map1은 "+map1);
    		
    		//get
    		System.out.println("map1_key1의 value는? ="+map1.get("map1_key1"));
    	
    		//containKey
    		System.out.println("containkey " + map1.containsKey("map1_key1"));
    		
    		//containValue
    		System.out.println("contain Value "+ map1.containsValue(4));
    		
    		String[] keys = {"map1_key1","no","map1_key2","no1","no2"};
    
    		int count = 0;
    		for (int i = 0 ; i < keys.length; i++){ 
    			if(map1.containsKey(keys[i])){
    				System.out.println(keys[i]+"의 겹치는 key가 존재한다.");
    				count++; 
    				}
    		}
    		System.out.println("겹치는 key의 개수 : "+count);
    
    			
    		//keyset()순환
    		for(String key:map1.keySet()) {
    			System.out.println("key는 "+key+",value는 "+map1.get(key));
    		}
    		
    		//map2생성
    		Map<String,Integer> map2=new HashMap();
    		map2.put("map2_key1", 11);
    		map2.put("map2_key2", 22);
    		System.out.println(map2);
    		
    		//map2에 map1을 합치기
    		map2.putAll(map1);
    		System.out.println("map1과 map2합치기 결과 :"+map2);
    		
    		Map<String,Integer> map3=new HashMap(map2);
    		System.out.println("생성하면서 map3에 담기, map3 = "+map3);
    		
    		//순서 보장하는 linkedHaashMap 자료가 입력된 순서를 기억한다.
    		 LinkedHashMap<String, String> lm = new LinkedHashMap<String, String>(); 
    			 lm.put("lm_key1", "one");
    			 lm.put("lm_key2", "two");
    			 lm.put("lm_key3", "three");
    			 lm.put("lm_key4", "four");
    			 lm.put("lm_key4", "four2");
    		 System.out.println("순서 보장하는 linkedHashMap : " + lm);
    			
    
    
    	}
    반응형

    댓글

Designed by Tistory.