我正在寻找Stream
List
到Map
的方式,我可以在其中指定密钥。
例如说我有两个列表:
List<String> one = Arrays.asList("1","2","3");
List<String> two = Arrays.asList("100","200","300");
我正在寻找一种方法Stream
将他们变成Map<String, List<String>>
{
"below 100" : ["1","2","3"],
"above 100" : ["100","200","300"]
}
非常感谢任何帮助。到目前为止,这是我提出的:
Stream.of(
one.stream(),
.collect(Collectors.toMap("below 100", ??)
,
two.stream()
.collect(Collectors.toMap("above 100", ??)
)
.reduce(Stream::concat)
答案 0 :(得分:1)
我发现了2种类似的方法。在这两种方式中,我们将每个List单独转换为Map&gt;然后加入两个地图。第一个选项更短,使用内联收集器,第二个选项创建一个新的收集器类。
对于这两个选项,我们首先声明列表:
List<String> one = Arrays.asList("1", "2", "3");
List<String> two = Arrays.asList("100", "200", "300");
内联收集器的更短选项:
private BinaryOperator<List<String>> addToList() {
return (list, str) -> {
((ArrayList<String>) list).addAll(str);
return list;
};
}
Map<String, List<String>> map = Stream.of(
// first list
one.stream()
.collect(Collectors.toMap(
l -> "below 100",
// we need List as map value
l -> Stream.of(l).collect(Collectors.toList()),
// merge function
addToList(),
// provide HashMap for result
HashMap::new
// we can't stream collected Map directly, only through EntrySet
)).entrySet(),
// second list
two.stream()
.collect(Collectors.toMap(
l -> "above 100",
l -> Stream.of(l).collect(Collectors.toList()),
addToList(),
HashMap::new
)).entrySet()
)
// extract Entry<String, List<String>>
.flatMap(entrySet -> entrySet.stream())
// convert Entry<String, List<String>> to Map<String, List<String>
.collect(Collectors.toMap(
entry -> entry.getKey(),
entry -> entry.getValue()));
使用自定义收集器的选项:
原始流代码较短,只调用ListToMapCollector而不是实现内联收集器。
Map<String, List<String>> map = Stream
.of(
one.stream()
// use custom ListToMapCollector
.collect(new ListToMapCollector("below 100"))
// we can't stream collected Map directly, only through EntrySet
.entrySet(),
two.stream()
.collect(new ListToMapCollector("above 100"))
.entrySet())
// extract Entry<String, List<String>>
.flatMap(entrySet -> entrySet.stream())
// convert Entry<String, List<String>> to Map<String, List<String>
.collect(Collectors.toMap(
entry -> entry.getKey(),
entry -> entry.getValue()));
在ListToMapCollector中,我在创建时使用了this tutorial:
public class ListToMapCollector implements Collector<String, Map<String,
List<String>>, Map<String, List<String>>>
{
private String mapKey;
public TestCollector(String string)
{
this.mapKey = string;
}
@Override
public Supplier<Map<String, List<String>>> supplier() {
// provide HashMap for result
return HashMap::new;
}
@Override
public BiConsumer<Map<String, List<String>>, String> accumulator() {
return (map, stringValue) -> {
if (!map.containsKey(mapKey))
{
map.put(mapKey, new ArrayList<>());
}
map.get(mapKey).add(stringValue);
};
}
@Override
public BinaryOperator<Map<String, List<String>>> combiner() {
// Needed for parrallel stream, excluded for brevity.
return null;
}
@Override
public Function<Map<String, List<String>>, Map<String, List<String>>> finisher() {
return Function.identity();
}
@Override
public Set<java.util.stream.Collector.Characteristics> characteristics() {
return Collections.singleton(Characteristics.IDENTITY_FINISH);
}
}
答案 1 :(得分:0)
编辑:
Stream.of(
one.stream(),
two.stream()
)
.reduce(Stream::concat)
.collect(Collectors.toMap(s -> {
// if s > 100 then stuff otherwise other stuff
}, Function.identity());
不确定我是否理解你的问题,但你可以尝试这样的事情:
Map map = new HashMap<String, List<String>>();
map.put("Below 100", new ArrayList<String>());
map.put("Above 100", new ArrayList<String>());
List one = Arrays.asList("1","2","3");
List two = Arrays.asList("100","200","300");
one.stream().forEach(s -> map.get("Below 100").add(s));
two.stream().forEach(s -> map.get("Above 100").add(s));
或者,您可以在流功能中添加一些更高级的代码:
one.stream().forEach(s -> {
// if value > 100 put in here,
// Otherwise do stuff...
});