How to iterate through a Map in java?

前端 未结 3 428
囚心锁ツ
囚心锁ツ 2021-02-01 06:22

I need to iterate through a BucketMap and get all keys but how do I get to something like buckets[i].next.next.next.key for instance witho

3条回答
  •  粉色の甜心
    2021-02-01 07:03

    With Java 8, I would suggest you to use Stream API.

    It will allow you to iterate through the Map in a much more convenient approach:

    public void iterateUsingStreamAPI(Map map) {
        map.entrySet().stream()
          // ...
          .forEach(e -> System.out.println(e.getKey() + ":" + e.getValue()));
    }
    

    Check out for more examples about iteration through maps in Java.

提交回复
热议问题