Java collect函数给出了循环推理错误

编程入门 行业动态 更新时间:2024-10-22 18:39:21
本文介绍了Java collect函数给出了循环推理错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当键入以下代码时,我在groupingBy函数的参数上得到循环推理错误:

When typing the following code I get a "cyclic inference" error on the argument for the groupingBy function:

Map<String, User> mapByEmail = users.stream().collect(Collectors.groupingBy(User::getEmail));

我发现这令人困惑,因为以下不会导致任何问题:

I find this confusing because the following does not cause any problem:

users.stream().collect(Collectors.groupingBy(User::getEmail));

且这两者都不是:

List<User> listByEmail = users.stream().collect(Collectors.groupingBy(User::getEmail)).values().stream().reduce(null, (a,b)-> a=b);

所以问题是,什么是循环推理,我该如何避免呢?

So the question is, what is a cyclic inference and how can I avoid it?

编辑感谢您的回答。经过进一步研究后,我发现需要通过以下方式减少我的结果:

EDIT Thanks for the answers. After further research I found out that I need to reduce my result by doing the following:

Map<String, User> mapByEmail = users.stream().collect(Collectors.groupingBy(User::getEmail, Collectors.reducing(new User(),(a,b)-> a=b)));

推荐答案

问题是结果类型不正确。它应该是 Map< String,List< User>> :

The problem is that your resulting type is incorrect. It should be Map<String, List<User>>:

Map<String, List<User>> mapByEmail = users.stream() .collect(Collectors.groupingBy(User::getEmail));

错误消息看起来令人困惑,但代码中确实存在错误。

The error message looks confusing, but there's actual an error in your code.

更多推荐

Java collect函数给出了循环推理错误

本文发布于:2023-10-31 06:04:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:出了   函数   错误   Java   collect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!