There are some methods to be used in the daily development of very many times, but a lot of times on the keyboard forget the code, how can not remember, or use less ah, young people…… Today let’s revisit the HashMap traversal form. Since Java8 came out, HashMap traversal has become more versatile and easier to write. Here are seven traversal forms, which can be divided into iterator traversal, For Each traversal,Lambda traversal,Stream API traversal.

* @param map the hashMap */ public void doEntrySet(hashMap <String,String> map){ Iterator<Entry<String, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()){ Entry<String, String> next = iterator.next(); System.out.println(next.getKey()); System.out.println(next.getValue()); Public void doKeySet(hashMap <String,String> map){public void doKeySet(hashmap <String,String> map){ Iterator<String> iterator = map.keySet().iterator(); while (iterator.hasNext()){ String key = iterator.next(); System.out.println(key); System.out.println(map.get(key)); @param map the hashMap public void doForEntrySet(hashMap <String,String> map){for (Map.Entry<String,String> entry : map.entrySet()){ System.out.println(entry.getKey()); System.out.println(entry.getValue()); @param map the hashMap */ public void doForKeySet(hashMap <String,String> map){for (String key : map.keySet()){ System.out.println(key); System.out.println(map.get(key)); Public void doLambda(hashMap <String,String> map){public void doLambda(hashMap <String,String> map){ map.forEach((k,v) -> { System.out.println(k); System.out.println(v); }); Public void doStreamSingle(hashMap <String,String> map){public void doStreamSingle(hashMap <String,String> map){ map.entrySet().stream().forEach(entry -> { System.out.println(entry.getKey()); System.out.println(entry.getValue()); }); } /** * @param map */ public void doStream(HashMap<String,String> map){ map.entrySet().parallelStream().forEach((entry) -> { System.out.print(entry.getKey()); System.out.print(entry.getValue()); }); }Copy the code

ParallelStream provides parallel processing of streams, with the underlying implementation using the Fork/Join framework. Simple understanding is a multithreaded asynchronous task implementation.

If you look at the compiled code from a.class file compiled by javac, you can see that the compiled code is the same except for the Lambda and Stream API loops. Each iteration through EntrySet creates an Entry object in the loop, and each iteration through keySet creates a String local variable whose value is fetched directly from the map.

  public IteratorMapTest() {
  }

  public void doEntrySet(HashMap<String, String> var1) {
    Iterator var2 = var1.entrySet().iterator();

    while(var2.hasNext()) {
      Entry var3 = (Entry)var2.next();
      System.out.println((String)var3.getKey());
      System.out.println((String)var3.getValue());
    }

  }

  public void doKeySet(HashMap<String, String> var1) {
    Iterator var2 = var1.keySet().iterator();

    while(var2.hasNext()) {
      String var3 = (String)var2.next();
      System.out.println(var3);
      System.out.println((String)var1.get(var3));
    }

  }

  public void doForEntrySet(HashMap<String, String> var1) {
    Iterator var2 = var1.entrySet().iterator();

    while(var2.hasNext()) {
      Entry var3 = (Entry)var2.next();
      System.out.println((String)var3.getKey());
      System.out.println((String)var3.getValue());
    }

  }

  public void doForKeySet(HashMap<String, String> var1) {
    Iterator var2 = var1.keySet().iterator();

    while(var2.hasNext()) {
      String var3 = (String)var2.next();
      System.out.println(var3);
      System.out.println((String)var1.get(var3));
    }

  }

  public void doLambda(HashMap<String, String> var1) {
    var1.forEach((var0, var1x) -> {
      System.out.println(var0);
      System.out.println(var1x);
    });
  }

  public void doStreamSingle(HashMap<String, String> var1) {
    var1.entrySet().stream().forEach((var0) -> {
      System.out.println((String)var0.getKey());
      System.out.println((String)var0.getValue());
    });
  }

  public void doStream(HashMap<String, String> var1) {
    var1.entrySet().parallelStream().forEach((var0) -> {
      System.out.print((String)var0.getKey());
      System.out.print((String)var0.getValue());
    });
  }
Copy the code

Finally, after the execution and test, except for the Stream API multi-threaded implementation performance teaching, other implementation methods performance difference is basically not much.