Java 基础——HashMap 遍历方式

参考文章:
HashMap 的 7 种遍历方式与性能分析!「修正篇」

HashMap 遍历从大的方向来说,可分为以下 4 类:

  • 迭代器 (Iterator) 方式遍历;
  • foreach 方式遍历;
  • Lambda 表达式遍历 (JDK 1.8+);
  • Streams API 遍历 (JDK 1.8+)。

但每种类型下又有不同的实现方式,因此具体的遍历方式又可以分为以下 7 种:

  • 使用迭代器(Iterator)EntrySet 的方式进行遍历;
  • 使用迭代器(Iterator)KeySet 的方式进行遍历;
  • 使用 foreach EntrySet 的方式进行遍历;
  • 使用 foreach KeySet 的方式进行遍历;
  • 使用 Lambda 表达式的方式进行遍历;
  • 使用 Streams API 单线程的方式进行遍历;
  • 使用 Streams API 多线程的方式进行遍历。

1.使用迭代器 (Iterator) EntrySet 的方式进行遍历

class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "Java");
        hashMap.put(2, "C++");
        hashMap.put(3, "Python");
        // Iterator entrySet
        Iterator<Map.Entry<Integer, String>> iterator = hashMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, String> entry = iterator.next();
            System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
        }
    }
}

输出结果如下,下面几种遍历方式的结果于下面的一样,因此将不再展示。

(1, Java)
(2, C++)
(3, Python)

2.使用迭代器 (Iterator) KeySet 的方式进行遍历

class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "Java");
        hashMap.put(2, "C++");
        hashMap.put(3, "Python");
        // Iterator keySet
        Iterator<Integer> iterator = hashMap.keySet().iterator();
        while (iterator.hasNext()) {
            Integer key = iterator.next();
            System.out.println("(" + key + ", " + hashMap.get(key) + ")");
        }
    }
}

3.使用 foreach EntrySet 的方式进行遍历

class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "Java");
        hashMap.put(2, "C++");
        hashMap.put(3, "Python");
        // foreach entrySet
        for (Map.Entry<Integer, String> entry : hashMap.entrySet()) {
            System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
        }
    }
}

4.使用 foreach KeySet 的方式进行遍历

class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "Java");
        hashMap.put(2, "C++");
        hashMap.put(3, "Python");
        // foreach keySet
        for (Integer key : hashMap.keySet()) {
            System.out.println("(" + key + ", " + hashMap.get(key) + ")");
        }
    }
}

5.使用 Lambda 表达式的方式进行遍历

class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "Java");
        hashMap.put(2, "C++");
        hashMap.put(3, "Python");
        // Lambda
        hashMap.forEach((key, value) -> {
            System.out.println("(" + key + ", " + value + ")");
        });
    }
}

6.使用 Streams API 单线程的方式进行遍历

class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "Java");
        hashMap.put(2, "C++");
        hashMap.put(3, "Python");
        // Streams API 单线程
        hashMap.entrySet().stream().forEach((entry) -> {
            System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
        });
    }
}

7.使用 Streams API 多线程的方式进行遍历

class HashMapTest {
    public static void main(String[] args) {
        Map<Integer, String> hashMap = new HashMap<>();
        hashMap.put(1, "Java");
        hashMap.put(2, "C++");
        hashMap.put(3, "Python");
        // Streams API 多线程
        hashMap.entrySet().parallelStream().forEach((entry) -> {
            System.out.println("(" + entry.getKey() + ", " + entry.getValue() + ")");
        });
    }
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
判断两个list集合里的对象某个属性值是否一样可以按照以下步骤进行: 1. 遍历第一个list集合,以该对象的某个属性值为key,将对象存入一个Map中。 2. 遍历第二个list集合,以该对象的某个属性值为key,从Map中获取该key对应的对象,进行比较。 3. 如果两个对象的属性值相同,则说明两个list集合里的对象某个属性值是一样的。 下面是Java代码实现: ```java public boolean isSameProperty(List<Object> list1, List<Object> list2, String property) { Map<Object, Object> map = new HashMap<>(); for (Object obj : list1) { try { Field field = obj.getClass().getDeclaredField(property); field.setAccessible(true); Object key = field.get(obj); map.put(key, obj); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } for (Object obj : list2) { try { Field field = obj.getClass().getDeclaredField(property); field.setAccessible(true); Object key = field.get(obj); Object obj1 = map.get(key); if (obj1 != null) { if (obj.equals(obj1)) { return true; } } } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } return false; } ``` 使用示例: ```java List<User> list1 = new ArrayList<>(); list1.add(new User("张三", 20)); list1.add(new User("李四", 30)); List<User> list2 = new ArrayList<>(); list2.add(new User("王五", 20)); list2.add(new User("赵六", 30)); boolean isSameAge = isSameProperty(list1, list2, "age"); System.out.println("isSameAge: " + isSameAge); // true ``` 其中,User类实现了equals和hashCode方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码星辰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值