如何迭代Java HashMap

时间:2020-01-09 10:35:04  来源:igfitidea点击:

在本文中,我们将介绍使用Java迭代Map或者HashMap的不同方法。我们应该知道的一件事是,我们无法直接在Java中循环映射(除非我们使用forEach语句)。有些方法可以使用该视图返回Map的"集合视图",我们可以在Java中迭代HashMap。

可以用来获取地图"集合视图"的方法如下:

  • Set <Map.Entry <K,V >> entrySet()–返回此映射中包含的映射的Set视图。
  • Set <K> keySet()–返回此映射中包含的键的Set视图。
  • Collection <V> values()–返回此映射中包含的值的Collection视图。

迭代Java HashMap的选项

从上述方法中可以看到,我们将获得一个具有Map.entry元素的Set,地图的一组键或者Map值的Collection视图。

使用该视图,我们可以使用以下选项之一在Java中迭代Map:

  • 我们可以使用Java 5中提供的For-Each循环(增强的循环)。
  • 我们可以使用Iterator进行迭代。使用iterator()方法可以获得一个迭代器,然后使用该迭代器的hashNext()和next()方法可以迭代一个HashMap。
  • 我们还可以使用Java 8提供的forEach语句遍历Map。

在Java中迭代HashMap的示例代码

这是一个使用上述所有方法迭代HashMap的示例。

使用entrySet()方法

使用entrySet()方法,我们可以以Map.entry元素的形式获取存储在HashMap中的映射的设置视图。使用该设置视图,我们可以迭代HashMap并获取键和值。

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapIteration {
  public static void main(String[] args) {
    // Creating HashMap
    Map<String, String> carMap = new HashMap<String, String>();
    // Storing elements
    carMap.put("1", "Audi");
    carMap.put("2", "BMW");
    carMap.put("3", "Jaguar");
    carMap.put("4", "Mini Cooper");
    System.out.println("***Looping using entrySet***");
    Set<Map.Entry<String, String>> carSet =  carMap.entrySet();
        
    System.out.println("***Using for-each loop***");
    for(Map.Entry<String, String> entry : carSet){
      System.out.println("Key is " + entry.getKey() + " Value is " + entry.getValue());
    }
        
    System.out.println("***Using iterator***");
    Iterator<Map.Entry<String, String>> itr = carSet.iterator();
    while (itr.hasNext()) {
      Map.Entry<String, String> entry = itr.next();
      System.out.println("Key is " + entry.getKey() + " Value is " + entry.getValue());    
    }
        
    System.out.println("***Using forEach statement***");
    carSet.forEach((c)->System.out.println("Key is " + c.getKey() + " Value is " + c.getValue()));
  }
}

输出:

***Looping using entrySet***
***Using for-each loop***
Key is 1 Value is Audi
Key is 2 Value is BMW
Key is 3 Value is Jaguar
Key is 4 Value is Mini Cooper
***Using iterator***
Key is 1 Value is Audi
Key is 2 Value is BMW
Key is 3 Value is Jaguar
Key is 4 Value is Mini Cooper
***Using forEach statement***
Key is 1 Value is Audi
Key is 2 Value is BMW
Key is 3 Value is Jaguar
Key is 4 Value is Mini Cooper

使用keySet()方法

使用keySet()方法可以获取HashMap键的设置视图。拥有键之后,还可以使用get()方法获取映射到这些键的值,但是与其他方法相比,这会使Map的循环变慢。

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapIteration {
  public static void main(String[] args) {
    // Creating HashMap
    Map<String, String> carMap = new HashMap<String, String>();
    // Storing elements
    carMap.put("1", "Audi");
    carMap.put("2", "BMW");
    carMap.put("3", "Jaguar");
    carMap.put("4", "Mini Cooper");
    System.out.println("***Looping using keySet***");
    Set<String> carSet =  carMap.keySet();
    System.out.println("***Using for-each loop***");
    for(String key : carSet){
      System.out.println("Key is " + key + " Value is " + carMap.get(key));
    }
    System.out.println("***Using iterator***");
    Iterator<String> itr = carSet.iterator();
    while (itr.hasNext()) {
      String key = itr.next();
      System.out.println("Key is " + key + " Value is " + carMap.get(key));    
    }
    System.out.println("***Using forEach statement***");
    carSet.forEach((c)->System.out.println("Key is " + c + " Value is " + carMap.get(c)));
  }
}

输出:

***Looping using keySet***
***Using for-each loop***
Key is 1 Value is Audi
Key is 2 Value is BMW
Key is 3 Value is Jaguar
Key is 4 Value is Mini Cooper
***Using iterator***
Key is 1 Value is Audi
Key is 2 Value is BMW
Key is 3 Value is Jaguar
Key is 4 Value is Mini Cooper
***Using forEach statement***
Key is 1 Value is Audi
Key is 2 Value is BMW
Key is 3 Value is Jaguar
Key is 4 Value is Mini Cooper

使用values()方法

如果只想遍历HashMap的值,则可以使用values()方法。

import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class HashMapIteration {
  public static void main(String[] args) {
    // Creating HashMap
    Map<String, String> carMap = new HashMap<String, String>();
    // Storing elements
    carMap.put("1", "Audi");
    carMap.put("2", "BMW");
    carMap.put("3", "Jaguar");
    carMap.put("4", "Mini Cooper");
        
    System.out.println("***Looping using values***");
    Collection<String> cars = carMap.values();
    System.out.println("***Using for-each loop***");
    for(String car : cars){
      System.out.println("Value is " + car);
    }
    System.out.println("***Using iterator***");
    Iterator<String> itr = cars.iterator();
    while (itr.hasNext()) {
      System.out.println("Value is " + itr.next());
    }
    System.out.println("***Using forEach statement***");
    cars.forEach((c)->System.out.println("Value is " + c));
    // forEach with method reference
    cars.forEach(System.out::println);
  }
}

输出:

***Looping using values***
***Using for-each loop***
Value is Audi
Value is BMW
Value is Jaguar
Value is Mini Cooper
***Using iterator***
Value is Audi
Value is BMW
Value is Jaguar
Value is Mini Cooper
***Using forEach statement***
Value is Audi
Value is BMW
Value is Jaguar
Value is Mini Cooper
Audi
BMW
Jaguar
Mini Cooper

俗话说"尽力而为",这是一种直接使用forEach语句(从Java 8开始)在Java中迭代HashMap的方法。

public class HashMapIteration {
  public static void main(String[] args) {
    // Creating HashMap
    Map<String, String> carMap = new HashMap<String, String>();
    // Storing elements
    carMap.put("1", "Audi");
    carMap.put("2", "BMW");
    carMap.put("3", "Jaguar");
    carMap.put("4", "Mini Cooper");
    
    carMap.forEach((K, V) -> System.out.println("Key is " + K + " value is " + V));
  }
}

输出:

Key is 1 value is Audi
Key is 2 value is BMW
Key is 3 value is Jaguar
Key is 4 value is Mini Cooper