Java 将列表流成一个集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32608125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:50:49 来源:igfitidea点击:
stream list into a set
提问by Robbo_UK
I am looking to refactor how I have used a stream in some of my code. The first example is how I currently have done it. The second example is what im trying to make it look like.
我希望重构我在某些代码中使用流的方式。第一个例子是我目前是如何做到的。第二个例子是我试图让它看起来像什么。
Set<String> results = new HashSet<String>();
someDao.findByType(type)
.stream()
.forEach(t-> result.add(t.getSomeMethodValue()) );
Could it look something like this? If so how do I make it do it?
它看起来像这样吗?如果是这样,我该如何让它做到这一点?
Set<String> results = someDao.findByType(type)
.stream()
.collect( /* ?? no sure what to put here */ );
采纳答案by Eran
Use Collectors.toSet
:
使用Collectors.toSet
:
Set<String> results = someDao.findByType(type)
.stream()
.map(ClassName::getValue)
.collect(Collectors.toSet());