MENU

Map

June 5, 2018 • Read: 142 • Java

  1. 保存形式:key——>value的方式保存
  2. 常用子类:

HashMap:无序,key不允许重复
TreeMap:有序,key不允许重复

public static void main(String args[]){
    Map<String,String> map = new HashMap<String,String>();
    map.put<"key1","www">;
    map.put<"key2","wmathor">;
    map.put<"key3","com">;
    String str = map.get("key1");
    System.out.println(str);//www
    if(map.containsKey("key2"){
        System.out.println("key2存在");
    }
    if(map.containsValue("wmathor"){
        System.out.println("wmathor存在");
    }
    
    Set<String> s = map.keySet();
    Iterator<String> it = s.iterator();
    while(it.hasNext()){
        System.out.println(it.next());
    }
    
    Collection<String> c = map.values();
    Iterator<String> i = c.iterator();
    while(i.hasNext()){
        System.out.println(it.next());
    }
}
最后编辑于: March 22, 2019
Archives Tip
QR Code for this page
Tipping QR Code