Javaコレクション(Mapコレクション)

2382 ワード

目次
一、Map集合の特徴
二、Map集合の機能
1.機能を追加する:V put、この戻り値は少し特殊で、v>
2.削除機能:
3.判断機能:戻り値は:boolean
4.取得機能:
 5.Mapを巡る
一、Map集合の特徴
インタフェースMapでは、K-このマッピングによって維持されるキーのタイプ、V-マッピング値のタイプであり、Kは一意であり、SetインタフェースはMapインタフェースの特徴と基本的に類似しており、HashSetの他の操作はHashMapに基づいており、HashSetはキーKのみであり、一意性を保証し、Setは無秩序であり、Mapの実装クラスも無秩序である.
二、Map集合の機能
1.機能追加:V put、この戻り値はちょっと特殊
Map map = new HashMap();

//     V  put(K,V),        ,         (     ),    
//        ,     ,       
String str = map.put("  ","  ");
System.out.println(str);
String str1 = map.put("  ","  ");
System.out.println(str1);
  :
null
  

2.削除機能:
//       
//map.clear();
//       ,     ,     null
System.out.println(map.remove("  "));

3.判断機能:戻り値は:boolean
map.put("  ","  ");
map.put("  ","  ");
map.put("  ","  ");
//          
System.out.println(map.containsKey("  "));
//          
System.out.println(map.containsValue("  "));
//        
System.out.println(map.isEmpty());
  :
true
true
false

4.取得機能:
//    
System.out.println(map.size());
//  map  ,   (  Set  )
System.out.println("map:"+map);

//    
//      
System.out.println(map.get("  "));
//     
Set s = map.keySet();
System.out.println(s);
//     
Collection c = map.values();
System.out.println(c);
  :
3
map:{  =  ,   =  ,   =  }
  
[  ,   ,   ]
[  ,   ,   ]

 5.Mapを巡る
2つの方法に分けられます.
方法1:
//1.
//A:      
//B:      ,        
//C:      
Set sk = map.keySet();
for(String a : sk){
    System.out.println(a+"---");
    System.out.println(map.get(a));
}

方法2:この方法はキーと値Set>entrySet()を同時に取得するために用いられ、この方法はSetオブジェクトを返し、中のタイプはMapである.Entry.
/2.
//A:           
//B:            
//C:          ,            
//D:           
Set> sem = map.entrySet();
for(Map.Entry m : sem){
    System.out.println(m.getKey());
    System.out.println(m.getValue());
    System.out.println(m);
}