Java – stream lists are grouped

I'm looking for refactoring how to use streams in some of my code The first example is my current practice The second example is that I try to make it look like

Set<String> results = new HashSet<String>();

someDao.findByType(type)
            .stream()
            .forEach(t-> result.add(t.getSomeMethodValue()) );

Does it look like this? If so, what should I do?

Set<String> results = someDao.findByType(type)
            .stream()
            .collect(  /*  ?? no sure what to put here  */ );

Solution

Using collectors toSet:

Set<String> results = someDao.findByType(type)
        .stream()
        .map(ClassName::getValue)
        .collect(Collectors.toSet());
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>