Java????α???Map?????4?????
???????????? ???????[ 2015/4/24 13:17:56 ] ????????Java ???? ???
??????Java?????????Map????
????How to Iterate Over a Map in Java
??????java?б???Map?в????????????????3????????????????
???????java?е?????map???????Map???????·??????????κ?map????HashMap?? TreeMap?? LinkedHashMap?? Hashtable?? ????
?????????????for-each????????entries??????
??????????????????????????????????????????????????????á?
????Map<Integer?? Integer> map = new HashMap<Integer?? Integer>();
????for (Map.Entry<Integer?? Integer> entry : map.entrySet()) {
????System.out.println("Key = " + entry.getKey() + "?? Value = " + entry.getValue());
????}
???????for-each?????java 5?б?????????÷???????????java 5??????汾?С???????????????????map????for-each????????NullPointerException???????????????????ü??????á?
??????????????for-each????б???keys??values??
???????????map?е?????????????????keySet??values??????????????????entrySet??
????Map<Integer?? Integer> map = new HashMap<Integer?? Integer>();
????//????map?е??
????for (Integer key : map.keySet()) {
????System.out.println("Key = " + key);
????}
????//????map?е??
????for (Integer value : map.values()) {
????System.out.println("Value = " + value);
????}
?????÷?????entrySet????????????????????10%??????????????????
???????????????Iterator????
??????÷????
????Map<Integer?? Integer> map = new HashMap<Integer?? Integer>();
????Iterator<Map.Entry<Integer?? Integer>> entries = map.entrySet().iterator();
????while (entries.hasNext()) {
????Map.Entry<Integer?? Integer> 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);
????}
?????????????keySet??values???????????????
??????????????????????????????????????????汾java??????Ω?????map????????????????????????????????iterator.remove()?????entries?????????????????????javadoc????????????for-each?????г?????????????????????????
????????????濴???÷????????for-each???????????????????????
?????????????????????????Ч????
????Map<Integer?? Integer> map = new HashMap<Integer?? Integer>();
????for (Integer key : map.keySet()) {
????Integer value = map.get(key);
????System.out.println("Key = " + key + "?? Value = " + value);
????}
??????????????????????????????????????????????????????Ч?????????????????????????????????????Map????и÷???????20%~200%????????????FindBugs????????????鰱???????????Щ???Ч?????????????????????á?
???????
??????????????(keys)???(values)??÷????????????????????汾????java 5??????????????????entries????????÷?????????????÷????(??????)??
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11