夯实JAVA基础之 -- Map

Map HashMap TreeMap

Map的遍历

HashMap 的遍历方式1:

Set keySet = map.keySet();
Iterator it = keySet.iterator();
while(it.hasNext()){
    Integer key = it.next();
    String value = map.get(key);
    System.out.println(key + ":" + value);
}

遍历方式2:

Set> entrySet = map.entrySet();
Iterator> it = entrySet.iterator();

while(it.hasNext()){
    Map.Entry me = it.next();
    Integer key = me.getKey();
    String value = me.getValue();
    System.out.println(key + ":" + value);
}

遍历方式3:

Collection values = map.values();
Iterator it = valuse.iterator();
while(it.hasNext()){
    System.out.println(it.next());
}

常用子类

  • Hashtable 内部结构是哈希表,是同步的。不允许null作为键,null作为值。
    • Properties 用来存储键值对型的配置文件的信息,可以和 IO 结合。
  • HashMap 内部结构是哈希表,不是同步的。允许null作为键,null作为值。
  • TreeMap 内部结构是二叉树,不是同步的。可以对Map中的键进行排序。

你可能感兴趣的:(夯实JAVA基础之 -- Map)