gpt4 book ai didi

Java8 : About order of Map elements returned by Collectors. 分组依据

转载 作者:行者123 更新时间:2023-11-30 06:45:44 26 4
gpt4 key购买 nike

想就遇到的例子修改的下面一段代码求教

我想知道当输入列表元素序列发生变化时,生成的 Map 中的输出序列是否会发生变化。但是,似乎所有情况下的输出都是相同的(我在 IDE 上反复运行该程序)。我可以就生成的 Map 元素的顺序是否可以预期寻求建议?使用枚举或字符串时,序列(按字母顺序/非字母顺序?)是否会有任何差异,或者元素序列只是随机的?

public class TestCountry {

public enum Continent {ASIA, EUROPE}

String name;
Continent region;

public TestCountry (String na, Continent reg) {
name = na;
region = reg;
}

public String getName () {
return name;
}

public Continent getRegion () {
return region;
}

public static void main(String[] args) {
List<TestCountry> couList1 = Arrays.asList (
new TestCountry ("Japan", TestCountry.Continent.ASIA),
new TestCountry ("Italy", TestCountry.Continent.EUROPE),
new TestCountry ("Germany", TestCountry.Continent.EUROPE));

List<TestCountry> couList2 = Arrays.asList (
new TestCountry ("Italy", TestCountry.Continent.EUROPE),
new TestCountry ("Japan", TestCountry.Continent.ASIA),
new TestCountry ("Germany", TestCountry.Continent.EUROPE));

Map<TestCountry.Continent, List<String>> mapWithRegionAsKey1 = couList1.stream ()
.collect(Collectors.groupingBy (TestCountry ::getRegion,
Collectors.mapping(TestCountry::getName, Collectors.toList())));

Map<String, List<Continent>> mapWithNameAsKey1 = couList1.stream ()
.collect(Collectors.groupingBy (TestCountry::getName,
Collectors.mapping(TestCountry::getRegion, Collectors.toList())));

Map<TestCountry.Continent, List<String>> mapWithRegionAsKey2 = couList2.stream ()
.collect(Collectors.groupingBy (TestCountry ::getRegion,
Collectors.mapping(TestCountry::getName, Collectors.toList())));

Map<String, List<Continent>> mapWithNameAsKey2 = couList2.stream ()
.collect(Collectors.groupingBy (TestCountry::getName,
Collectors.mapping(TestCountry::getRegion, Collectors.toList())));

System.out.println("Value of mapWithRegionAsKey1 in couList1: " + mapWithRegionAsKey1);
System.out.println("Value of mapWithNameAsKey1 in couList1: " + mapWithNameAsKey1);

System.out.println("Value of mapWithRegionAsKey2 in couList2: " + mapWithRegionAsKey2);
System.out.println("Value of mapWithNameAsKey2 in couList2: " + mapWithNameAsKey2);
}
}

/*
* Output:
Value of mapWithRegionAsKey1 in couList1: {ASIA=[Japan], EUROPE=[Italy,
Germany]}
Value of mapWithNameAsKey1 in couList1: {Japan=[ASIA], Italy=[EUROPE],
Germany=[EUROPE]}
Value of mapWithRegionAsKey2 in couList2: {ASIA=[Japan], EUROPE=[Italy,
Germany]}
Value of mapWithNameAsKey2 in couList2: {Japan=[ASIA], Italy=[EUROPE],
Germany=[EUROPE]}
*/

最佳答案

Collectors.groupingBy() 当前返回一个 HashMap。这是一个实现细节,可能会在未来的版本中发生变化。

HashMap 的条目没有排序,但它们迭代的顺序(当打印 HashMap 时)是确定的,并且不依赖于插入订单。

因此,无论输入 List 中元素的顺序如何,您都会看到相同的输出。不过,您不应该依赖该顺序,因为它是一个实现细节。

如果您关心输出 Map 中条目的顺序,您可以修改代码以生成 LinkedHashMap(默认顺序为插入顺序)或一个 TreeMap(对键进行排序的地方)。

顺便说一句,如果您将输入更改为

    List<TestCountry> couList1 = Arrays.asList (
new TestCountry ("Japan", TestCountry.Continent.ASIA),
new TestCountry ("Italy", TestCountry.Continent.EUROPE),
new TestCountry ("Germany", TestCountry.Continent.EUROPE));

List<TestCountry> couList2 = Arrays.asList (
new TestCountry ("Germany", TestCountry.Continent.EUROPE),
new TestCountry ("Italy", TestCountry.Continent.EUROPE),
new TestCountry ("Japan", TestCountry.Continent.ASIA)
);

您将在输出中得到不同的顺序:

Value of mapWithRegionAsKey1 in couList1: {ASIA=[Japan], EUROPE=[Italy, Germany]}
Value of mapWithNameAsKey1 in couList1: {Japan=[ASIA], Italy=[EUROPE], Germany=[EUROPE]}
Value of mapWithRegionAsKey2 in couList2: {ASIA=[Japan], EUROPE=[Germany, Italy]}
Value of mapWithNameAsKey2 in couList2: {Japan=[ASIA], Italy=[EUROPE], Germany=[EUROPE]}

这是因为各个输出 List 的元素(在当前实现中是 ArrayList)是根据插入顺序排序的,这取决于关于您输入的 List 的顺序。

关于Java8 : About order of Map elements returned by Collectors. 分组依据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48438516/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com