HashMap 的常用遍历方式

迭代器方式遍历

迭代器 EntrySet

public class HashMapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");

        Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
        while(iterator.hasNext()) {
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

迭代器 KeySet

public class HashMapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");

        Iterator<Integer> iterator = map.keySet().iterator();
        while(iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.println(key + ": " + map.get(key));
        }
    }
}

ForEach遍历

ForEach EntrySet

public class HashMapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");

        for(Map.Entry<Integer, String> entry : map.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

ForEach KeySet

public class HashMapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");

        for(Integer key : map.keySet()) {
            System.out.println(key + ": " + map.get(key));
        }
    }
}

Lamda 方式遍历

public class HashMapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");

        map.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
    }
}

Hash怎么在遍历时安全删除元素

迭代器方式

在迭代器方式中,可以在遍历过程中调用迭代器的remove()方法(不是 HashMap 的remove() 方法)进行删除

  public class HashMapDemo {
      public static void main(String[] args) {
          Map<Integer, String> map = new HashMap<>();
          map.put(1, "a");
          map.put(2, "b");
          map.put(3, "c");

          Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
          while(iterator.hasNext()) {
              Map.Entry<Integer, String> entry = iterator.next();
              if(entry.getKey() == 1) {
                iterator.remove();
              }
          }
      }
  }

Lamda 方式

可以使用 Lambda 中的 removeIf 来提前删除数据

public class HashMapDemo {
    public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<>();
        map.put(1, "a");
        map.put(2, "b");
        map.put(3, "c");

        map.keySet().removeIf(key -> key == 1);
        map.forEach((key, value) -> {
            System.out.println(key + ": " + value);
        });
    }
}
posted @ 2022-06-26 17:10  栗子leeleezl  阅读(70)  评论(0)    收藏  举报