# 踩坑Java HashMap遍历

**URL:** <https://www.shiqidu.com/t/topic/802>\
**Category:** 开发调优\
**Tags:** Java\
**Created:** [September 3, 2023, 2:57am UTC](https://www.shiqidu.com/t/topic/802 "2023-09-03T02:57:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![danton](https://www.shiqidu.com/user_avatar/www.shiqidu.com/danton/32/489_2.png) [@danton](https://www.shiqidu.com/u/danton)\
**Post date:** [September 3, 2023, 2:57am UTC](https://www.shiqidu.com/t/topic/802/1 "2023-09-03T02:57:11Z")

</div>

重构PHP接口写出的bug，误以为HashMap遍历是有序的，说说怎么掉进陷阱的

```auto
public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
		Map<String, Integer> map = new HashMap<>(3);
		map.put("A", 1);
		map.put("B", 2);
		map.put("C", 3);
		for (Map.Entry<String, Integer> entry : map.entrySet()) {
			System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
        }
        System.out.println("--------------------------------------------");
    }
}

```

跑了多次，按顺序输出，看起来没问题，继续

```auto
public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        Map<TestEnum, Integer> map = new HashMap<>(3);
        map.put(TestEnum.A, 1);
        map.put(TestEnum.B, 2);
        map.put(TestEnum.C, 3);
        for (Map.Entry<TestEnum, Integer> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
        }
        System.out.println("--------------------------------------------");
    }
}

@Getter
@AllArgsConstructor
public enum TestEnum {

    A("A"),
    B("B"),
    C("C"),
    ;

    private final String code;
}

```

看输出觉得没问题，于是没有继续验证，实际业务代码中是Map\<TestEnum, Object\>

```auto
public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        Map<TestEnum, Apple> map = new HashMap<>(3);
        map.put(TestEnum.A, new Apple("one"));
        map.put(TestEnum.B, new Apple("two"));
        map.put(TestEnum.C, new Apple("three"));
        for (Map.Entry<TestEnum, Apple> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
        }
        System.out.println("--------------------------------------------");
    }
}

@Data
@AllArgsConstructor
public static class Apple {
    private String name;
}

// 输出
key: C, value: Main.Apple(name=three)
key: A, value: Main.Apple(name=one)
key: B, value: Main.Apple(name=two)

```

可以看到顺序已经不是预想的那样了，此时可以换成LinkedHashMap保证顺序

```auto
public static void main(String[] args) {
    for (int i = 0; i < 10; i++) {
        Map<TestEnum, Apple> map = new LinkedHashMap<>(3);
        map.put(TestEnum.A, new Apple("one"));
        map.put(TestEnum.B, new Apple("two"));
        map.put(TestEnum.C, new Apple("three"));
        for (Map.Entry<TestEnum, Apple> entry : map.entrySet()) {
            System.out.println("key: " + entry.getKey() + ", value: " + entry.getValue());
        }
        System.out.println("--------------------------------------------");
    }
}

// 输出
key: A, value: Main.Apple(name=one)
key: B, value: Main.Apple(name=two)
key: C, value: Main.Apple(name=three)

```

---

<div class="post-metadata">

**Author:** ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
**Post date:** [September 7, 2023, 3:52am UTC](https://www.shiqidu.com/t/topic/802/2 "2023-09-07T03:52:35Z")

</div>

转java了吗？

---

<div class="post-metadata">

**Author:** ![danton](https://www.shiqidu.com/user_avatar/www.shiqidu.com/danton/32/489_2.png) [@danton](https://www.shiqidu.com/u/danton)\
**Post date:** [September 7, 2023, 9:35am UTC](https://www.shiqidu.com/t/topic/802/3 "2023-09-07T09:35:32Z")

</div>

是的，php项目只剩维护和少量迭代

---

<div class="post-metadata">

**Author:** ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
**Post date:** [September 7, 2023, 3:43pm UTC](https://www.shiqidu.com/t/topic/802/4 "2023-09-07T15:43:55Z")

</div>

厉害啊，我也在看Java的视频。
