Java 流:与供应商和累加器一起收集

Java 流:与供应商和累加器一起收集,java,java-8,java-stream,Java,Java 8,Java Stream,我一直在尝试将列表转换为HashMap。 我使用以下代码作为参考: String result = list.parallelStream().collect(StringBuilder::new, (response, element) -> response.append(" ").append(element), (response1, response2) -> response1.append(",").append(resp

我一直在尝试将列表转换为HashMap。 我使用以下代码作为参考:

String result = list.parallelStream().collect(StringBuilder::new,
            (response, element) -> response.append(" ").append(element),
            (response1, response2) -> response1.append(",").append(response2.toString()))
            .toString();
当我在eclipse中编写下面的不完整语句并在处执行ctrl+space时


已经提到了,但是,这方面运气不太好。

将列表转换为地图?
Collectors.toMap(..)
可能是您所需要的

javadocs有几个很好的例子:

Map=list1.stream().collect(()->newhashmap(),(r,s)->r.put(s.getString(),s),(r,s)->r.putAll(s));

根据你的评论,我想你可以做得这么简单

List<Choice> choices = new ArrayList<>();

Map<String, Integer> map = choices.stream().collect(Collectors.toMap(Choice::getString, Choice::getID);

如果您想更多地了解
供应商、累加器、合路器
,那么您应该先写得更清楚一点

假设您有这样一个类:

static class Foo {
    private final int id;

    private final String s;

    public Foo(int id, String s) {
        super();
        this.id = id;
        this.s = s;
    }

    public int getId() {
        return id;
    }

    public String getS() {
        return s;
    }

    @Override
    public String toString() {
        return "" + id;
    }
}
如果您知道在这种情况下,
s
是唯一的,您可以简单地执行以下操作:

HashMap<String, Foo> result = Arrays.asList(new Foo(1, "a"), new Foo(2, "b"))
            .stream()
            .collect(HashMap::new, 
                    (map, foo) -> map.put(foo.getS(), foo), 
                    HashMap::putAll);

    System.out.println(result); // {a=1, b=2}

是的,我知道这个解决方案,但我想走这条路去探索事物。我自己找到了答案。Thx Map=list1.stream().collect(()->new HashMap(),(r,s)->r.put(s.getString(),s),(r,s)->r.putAll(s))@RajeevAkotkar发布答案而不是评论。完成。也发布答案。请阅读,然后相应地编辑您的问题。根据说明删除图像您是否理解自己的问题?清单包含什么?你想获得什么?解释你的输入和你期望作为输出的内容,而不是解释你的代码。我的输入是一个选择对象列表(int id,String String),我想将其转换为一个映射,其中String作为键,Object作为值,这是正确的,但正如我已经知道的解决方案中的一个commnet中所提到的,但是我试图探索各种选择,即没有不同的可能解决方案。你为什么知道?但是,我不确定我是否正确理解了您的问题,但映射的值应该是
整数
选项
?你说的“object as value”,其中Integer和Choice都是对象。应该是一个选项,hashmap的键应该是String,value应该是一个Choice对象。这是一个相当简单的问题,我已经找到了解决方案。我唯一的限制是我想用一种特殊的方式来实现它。为什么不在累加器中使用
map.computeIfAbsent(foo.getS(),new ArrayList()).add(foo)
?您的组合器可以更简洁地写成
right.forEach((k,v)->left.merge(k,v,(l1,l2)->{l1.addAll(l2);return l1;})
@FedericoPeraltaSchaffner我有点错过了(
computeifassent
):)尤其是因为它也出现在文档中。。。谢谢。
Map<String, Choice> map = choices.stream().collect(Collectors.toMap(Choice::getString, Function.identity());
static class Foo {
    private final int id;

    private final String s;

    public Foo(int id, String s) {
        super();
        this.id = id;
        this.s = s;
    }

    public int getId() {
        return id;
    }

    public String getS() {
        return s;
    }

    @Override
    public String toString() {
        return "" + id;
    }
}
HashMap<String, Foo> result = Arrays.asList(new Foo(1, "a"), new Foo(2, "b"))
            .stream()
            .collect(HashMap::new, 
                    (map, foo) -> map.put(foo.getS(), foo), 
                    HashMap::putAll);

    System.out.println(result); // {a=1, b=2}
 HashMap<String, List<Foo>> result = Arrays.asList(
            new Foo(1, "a"),
            new Foo(2, "b"),
            new Foo(3, "a"))
            .stream()
            .parallel()
            .collect(HashMap::new,
                    (map, foo) -> {
                        map.computeIfAbsent(foo.getS(), s -> new ArrayList<>()).add(foo);
                    },
                    (left, right) -> {
                        for (HashMap.Entry<String, List<Foo>> e : right.entrySet()) {
                            left.merge(
                                    e.getKey(),
                                    e.getValue(),
                                    (l1, l2) -> {
                                        l1.addAll(l2);
                                        return l1;
                                    });
                        }
                    });

    System.out.println(result); // {a=[1, 3], b=[2]}