欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java中遍歷Map集合的5種方式總結(jié)

 更新時(shí)間:2021年01月31日 10:11:37   作者:揚(yáng)帆向海  
這篇文章主要給大家介紹了關(guān)于Java中遍歷Map集合的5種方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

方式一 通過(guò)Map.keySet使用iterator遍歷

@Test
public void testHashMap1() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "數(shù)據(jù)庫(kù)");
 map.put(003, "Vue");
 System.out.println(map);

 // 通過(guò)Map.keySet使用iterator遍歷key,然后通過(guò)key得到對(duì)應(yīng)的value值
 Iterator<Integer> iterator = map.keySet().iterator();
 while (iterator.hasNext()) {
 Integer key = iterator.next();
 String value = map.get(key);
 System.out.println("key = " + key + ", value = " + value);
 }
}

結(jié)果:

{1=Java, 2=數(shù)據(jù)庫(kù), 3=Vue}
key = 1, value = Java
key = 2, value = 數(shù)據(jù)庫(kù)
key = 3, value = Vue

方式二 通過(guò)Map.entrySet使用iterator遍歷

@Test
public void testHashMap2() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "數(shù)據(jù)庫(kù)");
 map.put(003, "Vue");
 System.out.println(map);

 // 通過(guò)Map.entrySet使用iterator遍歷key和value;注意 Set entrySet():返回所有key-value對(duì)構(gòu)成的Set集合
 Iterator<Map.Entry<Integer, String>> entries = map.entrySet().iterator();
 while (entries.hasNext()) {
 Map.Entry<Integer, String> entry = entries.next();
 System.out.println(entry);
 }
}

結(jié)果:

{1=Java, 2=數(shù)據(jù)庫(kù), 3=Vue}
1=Java
2=數(shù)據(jù)庫(kù)
3=Vue

方式三 通過(guò)Map.keySet遍歷

@Test
public void testHashMap3() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "數(shù)據(jù)庫(kù)");
 map.put(003, "Vue");
 System.out.println(map);

 // 通過(guò)Map.keySet遍歷key,然后通過(guò)key得到對(duì)應(yīng)的value值
 for (Integer key : map.keySet()) {
 System.out.println("key = " + key + ", value = " + map.get(key));
 }
}

結(jié)果:

{1=Java, 2=數(shù)據(jù)庫(kù), 3=Vue}
key = 1, value = Java
key = 2, value = 數(shù)據(jù)庫(kù)
key = 3, value = Vue

方式四 通過(guò)For-Each迭代entries,使用Map.entrySet遍歷

@Test
public void testHashMap4() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "數(shù)據(jù)庫(kù)");
 map.put(003, "Vue");
 System.out.println(map);

 // 使用For-Each迭代entries,通過(guò)Map.entrySet遍歷key和value
 for (Map.Entry<Integer, String> entry : map.entrySet()) {
 System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
 }
}

{1=Java, 2=數(shù)據(jù)庫(kù), 3=Vue}
key = 1, value = Java
key = 2, value = 數(shù)據(jù)庫(kù)
key = 3, value = Vue

方式五 使用lambda表達(dá)式forEach遍歷

@Test
public void testHashMap5() {
 Map<Integer, String> map = new HashMap<>();
 map.put(001, "Java");
 map.put(002, "數(shù)據(jù)庫(kù)");
 map.put(003, "Vue");
 System.out.println(map);

	// 使用lambda表達(dá)式forEach遍歷
 map.forEach((k, v) -> System.out.println("key = " + k + ", value = " + v));
}

forEach 源碼

default void forEach(BiConsumer<? super K, ? super V> action) {
 Objects.requireNonNull(action);
 for (Map.Entry<K, V> entry : entrySet()) {
  K k;
  V v;
  try {
  k = entry.getKey();
  v = entry.getValue();
  } catch(IllegalStateException ise) {
  // this usually means the entry is no longer in the map.
  throw new ConcurrentModificationException(ise);
  }
  action.accept(k, v);
 }
 }

從源碼可以看到,這種新特性就是在傳統(tǒng)的迭代方式上加了一層殼,但是讓代碼變得更加簡(jiǎn)單。(開(kāi)發(fā)中推薦使用)

總結(jié)

推薦使用 entrySet 遍歷 Map 類(lèi)集合 KV (文章中的第四種方式),而不是 keySet 方式進(jìn)行遍歷。

keySet 其實(shí)是遍歷了 2 次,第一次是轉(zhuǎn)為 Iterator 對(duì)象,第二次是從 hashMap 中取出 key 所對(duì)應(yīng)的 value值。而 entrySet 只是遍歷了一次,就把 key 和 value 都放到了 entry 中,效率更高。

values()返回的是 V 值集合,是一個(gè) list 集合對(duì)象;keySet()返回的是 K 值集合,是一個(gè) Set 集合對(duì)象;entrySet()返回的是 K-V 值組合集合。

如果是 JDK8,推薦使用Map.forEach 方法(文章中的第五種方式)。

到此這篇關(guān)于Java中遍歷Map集合的5種方式的文章就介紹到這了,更多相關(guān)Java遍歷Map集合內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論