gpt4 book ai didi

java - 拆分 Map 的键并使用新键和关联值创建新 Map

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:34 25 4
gpt4 key购买 nike

我有一个 ListObjects它们中的每一个都包含一个 X、Y 值 ( Doubles ) 和一个名称 String .由于我有多个与坐标对应的同名条目,我想将每个名称链接到所有坐标,我设法通过创建一个 Map 来做到这一点。类型 <String, List<Object>>

输出示例:

One             [[0.1,0.1,0.1],[0.2,0.3,0.4]]
One,Two [[0.1,0.1] ,[0.4,0.5]]
One,Two,Three [[0.1,0.1] ,[0.6,0.7]]

我现在要做的是把有逗号,的名字分开并实现如下结果:

One   [[0.1,0.1,0.1,0.1,0.1,0.1],[0.2,0.3,0.4,0.5,0.6,0.7]]
Two [[0.01,0.01,0.01,0.01] ,[0.4,0.5,0.6,0.7]]
Three [[0.01,0.01] ,[0.6,0.7]]

我的有效代码是:

Map<String, List<Coordinates>> newList = coordinateList.stream()
.collect(Collectors.groupingBy(Coordinates::getName));

创建第一个 Map .

为了完成第二部分,我尝试了以下各种组合,但没有成功:

newList.entrySet().stream()
.flatMap(entry -> Arrays.stream(entry.getKey().split(","))
.map(s -> new AbstractMap.SimpleEntry<>(s, entry.getValue())))
.collect(Collectors.groupingBy(Map.Entry::getKey,
Collectors.flatMapping(entry -> entry.getValue().stream(), Collectors.toList())));

我得到的错误是:

The method flatMapping((<no type> entry) -> {}, Collectors.toList()) is undefined for the type Collectors

请注意,通过更改 flatMappingmapping有效但不能解决问题。

我尝试的代码的另一个变体是:

Map<String, List<Object>> collect1 = map.entrySet().stream()
.flatMap(entry -> Arrays.stream(entry.getKey().split(","))
.map(s -> new AbstractMap.SimpleEntry<>(s, entry.getValue())))
.collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toList())))
.entrySet().stream()
.flatMap(entry -> entry.getValue().stream()
.flatMap(Collection::stream)
.map(o -> new AbstractMap.SimpleEntry<>(entry.getKey(), o)))
.collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, toList())));

仍然不走运,我经常收到错误 Cannot infer type argument(s) for <R> flatMap(Function<? super T,? extends Stream<? extends R>>)

有什么想法吗?

最佳答案

看来您没有使用 java9。如果您使用的是 Java9,则使用 Collectors.flatMapping 的前一种方法会起作用。但我仍然看不出在这里创建新的 Map.Entry 有任何意义。看看这个解决方案。

private static final Pattern COMMA_DELIMITER = Pattern.compile(",\\s*");

Map<String, Set<Coordinate>> nameToCoordinates = coordinates.stream()
.flatMap(
c -> COMMA_DELIMITER.splitAsStream(c.getName())
.map(n -> new Coordinate(n, c.getX(), c.getY())))
.collect(Collectors.groupingBy(Coordinate::getName, Collectors.toSet()));

对于每个坐标,获取它的名称并使用 , 分隔符将其拆分,然后为每个此类名称标记创建一个具有新名称和 x、y 坐标的新坐标。然后只需使用 groupingBy 收集器收集它。由于您只需要保留不同的 Coordinate 值,因此您必须覆盖 Coordinate 类中的 equals 和 hashCode 方法。这是它的样子。

@Override
public int hashCode() {
int result = name.hashCode();
result = 31 * result + Double.hashCode(x);
result = 31 * result + Double.hashCode(y);
return result;
}

@Override
public boolean equals(Object obj) {
return obj instanceof Coordinate && ((Coordinate) obj).getX() == x
&& ((Coordinate) obj).getY() == y
&& ((Coordinate) obj).getName().equals(name);
}

出于某种原因,如果你真的需要使用第一步创建的中间映射,那么你的解决方案将是这样的。

newList.values().stream().flatMap(Collection::stream)
.flatMap(
c -> COMMA_DELIMITER.splitAsStream(c.getName())
.map(n -> new Coordinate(n, c.getX(), c.getY())))
.collect(Collectors.groupingBy(Coordinate::getName, Collectors.toSet()));

这是输出。

{One=[[name=One, x=0.1, y=0.2], [name=One, x=0.1, y=0.4], [name=One, x=0.1, y=0.3], [name=One, x=0.1, y=0.5], [name=One, x=0.1, y=0.6], [name=One, x=0.1, y=0.7]], Two=[[name=Two, x=0.1, y=0.4], [name=Two, x=0.1, y=0.5], [name=Two, x=0.1, y=0.6], [name=Two, x=0.1, y=0.7]], Three=[[name=Three, x=0.1, y=0.6], [name=Three, x=0.1, y=0.7]]}

关于java - 拆分 Map 的键并使用新键和关联值创建新 Map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54753105/

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