流列表成一组 [英] stream list into a set
本文介绍了流列表成一组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望重构一些代码中使用流的方式.第一个例子是我目前的做法.第二个例子是我试图使它看起来像什么.
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 */ );
推荐答案
使用Collectors.toSet
:
Set<String> results = someDao.findByType(type)
.stream()
.map(ClassName::getValue)
.collect(Collectors.toSet());
这篇关于流列表成一组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文