2015-09-16 3 views
20

私は自分のコードの一部でストリームをどのように使用したかをリファクタリングしています。最初の例は、私が現在行ってきたことです。 2番目の例は、それを見せるようにしようとしているところです。ストリームリストをセットにします

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 */); 
+2

あなたはセットにそれらを収集する前にストリーム要素をマップする必要があります。 'someDao.findByType(type) .stream()。map(TheClass :: getValue).collect(toSet());' –

答えて

28

使用Collectors.toSet

Set<String> results = someDao.findByType(type) 
     .stream() 
     .map(ClassName::getValue) 
     .collect(Collectors.toSet()); 
関連する問題