43

我希望能够将 a 转换List为 a HashMap,其中键是 the 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“他们应该对元素进行操作”帮助很大:)。实际上,这正是我想要的

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)));
}
4

3 回答 3

88

您可以使用该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(Collectors.groupingBy(Function.identity()));
}

如果您想要更大的灵活性(例如映射值并返回 Set 而不是 List),您始终可以使用groupingBy带有更多参数的方法,如 javadoc 中指定的:

Map<City, Set<String>> namesByCity = people.stream().collect(Collectors.groupingBy(Person::getCity, mapping(Person::getLastName, toSet())));
于 2015-07-16T14:09:03.347 回答
25

您在代码中定义的函数不正确,因为它们应该对列表的元素进行操作key,而的元素不是s。valueMap

以下代码适用于我:

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定义如何从列表元素中创建一个键(保持原样),第二个参数定义如何创建一个值(ArrayList使用单个元素创建一个)。

于 2014-07-23T17:57:55.603 回答
8

感谢所有的帮助家伙!@izstas“他们应该对元素进行操作”帮助很大:)。实际上,这正是我想要的

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)));
}
于 2014-07-24T13:53:08.970 回答