收集HashMap <string,list <string =“”>&gt; java 8 </string,>

时间:2014-07-23 17:27:43

标签: java list hashmap java-8 collectors

我希望能够将List转换为HashMap,其中密钥为elementName,值为随机列表(在本例中为元素名称) )。所以简而言之,我想要(A->List(A), B->List(B), C-> List(C))。我尝试使用toMap()并将keyMapperValueMapper传递给它,但我收到了编译错误。如果有人可以帮助我,我真的很感激。

谢谢!

public static void main(String[] args) {
    // TODO Auto-generated method stub
    List<String> list = Arrays.asList("A","B","C","D");
    Map<String, List<String>> map = list.stream().map((element)->{
        Map<String, List<String>> map = new HashMap<>();
        map.put(element, Arrays.asList(element));
        return map;
    }).collect(??);
}


Function<Map<String, String>, String> key = (map) -> {
    return map.keySet().stream().findFirst().get();
};

Function<Map<String, String>, String> value = (map) -> {
    return map.values().stream().findFirst().get();
};

=== 这对我有用

感谢所有帮助人员! @izstas&#34;他们应该运用元素&#34;帮了很多:)。实际上这就是我想要的确切

public static void test2 (){
    Function<Entry<String, List<String>>, String> key = (entry) -> {
        return entry.getKey();
    };
    Function<Entry<String, List<String>>, List<String>> value = (entry) -> {
        return new ArrayList<String>(entry.getValue());
    };
    BinaryOperator<List<String>> merge = (old, latest)->{
        old.addAll(latest);
        return old;
    };

    Map<String, List<String>> map1 = new HashMap<>();
    map1.put("A", Arrays.asList("A1", "A2"));
    map1.put("B", Arrays.asList("B1"));
    map1.put("D", Arrays.asList("D1"));

    Map<String, List<String>> map2 = new HashMap<>();
    map2.put("C", Arrays.asList("C1","C2"));
    map2.put("D", Arrays.asList("D2"));

    Stream<Map<String, List<String>>> stream =Stream.of(map1, map2);
    System.out.println(stream.flatMap((map)->{
        return map.entrySet().stream(); 
    }).collect(Collectors.toMap(key, value, merge)));
}

3 个答案:

答案 0 :(得分:39)

您可以使用groupingBy方法管理聚合,例如:

public static void main(String[] args) {
    List<String> list = Arrays.asList("A", "B", "C", "D", "A");
    Map<String, List<String>> map = list.stream().collect(groupingBy(Function.identity()));
}

如果你想要更多的灵活性(例如映射值并返回一个Set而不是一个List),你总是可以使用groupingBy方法和javadoc中指定的更多参数:

Map<City, Set<String>> namesByCity = people.stream().collect(groupingBy(Person::getCity, mapping(Person::getLastName, toSet())));

答案 1 :(得分:18)

您在代码中定义的函数keyvalue不正确,因为它们应该在列表的元素上运行,而您的元素不是{{1 }}第

以下代码适用于我:

Map

List<String> list = Arrays.asList("A", "B", "C", "D"); Map<String, List<String>> map = list.stream() .collect(Collectors.toMap(Function.identity(), Arrays::asList)); 的第一个参数定义了如何从列表元素中创建一个键(保持原样),第二个参数定义了如何创建一个值(使用单个元素创建Collectors.toMap)。

答案 2 :(得分:6)

感谢所有帮助人员! @izstas&#34;他们应该运用元素&#34;帮了很多:)。实际上这就是我想要的确切

public static void test2 (){
    Function<Entry<String, List<String>>, String> key = (entry) -> {
        return entry.getKey();
    };
    Function<Entry<String, List<String>>, List<String>> value = (entry) -> {
        return new ArrayList<String>(entry.getValue());
    };
    BinaryOperator<List<String>> merge = (old, latest)->{
        old.addAll(latest);
        return old;
    };

    Map<String, List<String>> map1 = new HashMap<>();
    map1.put("A", Arrays.asList("A1", "A2"));
    map1.put("B", Arrays.asList("B1"));
    map1.put("D", Arrays.asList("D1"));

    Map<String, List<String>> map2 = new HashMap<>();
    map2.put("C", Arrays.asList("C1","C2"));
    map2.put("D", Arrays.asList("D2"));

    Stream<Map<String, List<String>>> stream =Stream.of(map1, map2);
    System.out.println(stream.flatMap((map)->{
        return map.entrySet().stream(); 
    }).collect(Collectors.toMap(key, value, merge)));
}