Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何转换列表<;V>;进入地图<;K、 列表<;V>>;,使用Java 8流和自定义列表和地图供应商?_Java_Java Stream_Collectors - Fatal编程技术网

如何转换列表<;V>;进入地图<;K、 列表<;V>>;,使用Java 8流和自定义列表和地图供应商?

如何转换列表<;V>;进入地图<;K、 列表<;V>>;,使用Java 8流和自定义列表和地图供应商?,java,java-stream,collectors,Java,Java Stream,Collectors,很容易将列表转换为地图。例如: public Map<Integer, List<String>> getMap(List<String> strings) { return strings.stream() .collect(Collectors.groupingBy(String::length)); } 问题:有没有一种更简单、更简洁或更高效的方法?例如,类似这样的东西(不起作用): 如果我只需要定义列表供应

很容易将
列表
转换为
地图
。例如:

public Map<Integer, List<String>> getMap(List<String> strings) {
   return
      strings.stream()
             .collect(Collectors.groupingBy(String::length));
}
问题:有没有一种更简单、更简洁或更高效的方法?例如,类似这样的东西(不起作用):


如果我只需要定义
列表
供应商,而不需要定义
地图
供应商怎么办?

您可以有以下内容:

public Map<Integer, List<String>> getMap(List<String> strings) {
    return strings.stream().collect(
      Collectors.groupingBy(String::length, HashMap::new, Collectors.toCollection(ArrayList::new))
    );
}

作为补充说明,您可以使用一种通用方法:

public <K, V, C extends Collection<V>, M extends Map<K, C>> M getMap(List<V> list,
        Function<? super V, ? extends K> classifier, Supplier<M> mapSupplier, Supplier<C> collectionSupplier) {
    return list.stream().collect(
        Collectors.groupingBy(classifier, mapSupplier, Collectors.toCollection(collectionSupplier))
    );
}

但是,在代码中直接使用
groupingBy
可能会更简单…

我也遇到过类似的情况。我是这样解决的:

Map<String, List<Object>> map = stringList.stream().collect(Collectors.toMap(str -> str, str -> populateList(str)));

如果您计划创建类似于
map
的地图,则可以使用此解决方案:

Map ds=requestList.stream().collect(
Collectors.groupingBy(TagRequest::getProperty_1,HashMap::new,
Collectors.mapping(TagRequest::getProperty_2,Collectors.toList())
);
如果您计划创建类似于
map
的地图,您可以使用:

Map<String, List<String>> ds= requestList.stream().collect(
    Collectors.groupingBy(TagRequest::getProperty_1, HashMap::new, 
    Collectors.mapping(TagRequest::getProperty_2, Collectors.toSet()))
);
Map ds=requestList.stream().collect(
Collectors.groupingBy(TagRequest::getProperty_1,HashMap::new,
Collectors.mapping(TagRequest::getProperty_2,Collectors.toSet())
);

有一个重载占用了一个地图供应商。流上有一个三个参数的收集方法,可能会引起youBravo的兴趣!这正是我想要的最好的解决方案!当钥匙已经在地图上时,有没有办法解决这种情况?太棒了!某种忍者技术。我今天知道了一些新的东西
public <K, V, C extends Collection<V>, M extends Map<K, C>> M getMap(List<V> list,
        Function<? super V, ? extends K> classifier, Supplier<M> mapSupplier, Supplier<C> collectionSupplier) {
    return list.stream().collect(
        Collectors.groupingBy(classifier, mapSupplier, Collectors.toCollection(collectionSupplier))
    );
}
HashMap<Integer, ArrayList<String>> m = getMap(Arrays.asList("foo", "bar", "toto"),
        String::length, HashMap::new, ArrayList::new);
LinkedHashMap<Integer, LinkedList<String>> m2 = getMap(Arrays.asList("foo", "bar", "toto"),
        String::length, LinkedHashMap::new, LinkedList::new);
Map<String, List<Object>> map = stringList.stream().collect(Collectors.toMap(str -> str, str -> populateList(str)));
private List<Object> populateList(final String str) {
    ...
    ....
    List<Object> list = // dao.get(str);
    return list;
}
Map<String, List<String>> ds= requestList.stream().collect(
    Collectors.groupingBy(TagRequest::getProperty_1, HashMap::new, 
    Collectors.mapping(TagRequest::getProperty_2, Collectors.toList()))
);
Map<String, List<String>> ds= requestList.stream().collect(
    Collectors.groupingBy(TagRequest::getProperty_1, HashMap::new, 
    Collectors.mapping(TagRequest::getProperty_2, Collectors.toSet()))
);