小技巧

1.遍历map的最好方式 ,entryset.iterator()

使用泛型


Map map = new HashMap();

Iterator> entries = map.entrySet().iterator();

while (entries.hasNext()) {

  Map.Entry entry = entries.next();

  System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());

}

不使用泛型:


Map map = new HashMap();

Iterator entries = map.entrySet().iterator();

while (entries.hasNext()) {

  Map.Entry entry = (Map.Entry) entries.next();

  Integer key = (Integer)entry.getKey();

  Integer value = (Integer)entry.getValue();

  System.out.println("Key = " + key + ", Value = " + value);

}

你可能感兴趣的:(小技巧)