将列表流式传输到集合中
stream list into a set
我希望重构我在某些代码中使用流的方式。第一个例子是我目前是如何做到的。第二个例子是我试图让它看起来像的。
Set<String> results = new HashSet<String>();
someDao.findByType(type)
.stream()
.forEach(t-> result.add(t.getSomeMethodValue()) );
能不能像这样?如果是这样,我该怎么做呢?
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());
我希望重构我在某些代码中使用流的方式。第一个例子是我目前是如何做到的。第二个例子是我试图让它看起来像的。
Set<String> results = new HashSet<String>();
someDao.findByType(type)
.stream()
.forEach(t-> result.add(t.getSomeMethodValue()) );
能不能像这样?如果是这样,我该怎么做呢?
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());