lemy
lemy

Reputation: 5

How to convert java stream collect to scala

Java code

objRefs = objRefs.stream().filter(objRef -> DataUtil.isNotEmpty(objRef)).collect(Collectors.toCollection(LinkedHashSet::new));

How to convert it to scala? Following code compile error

objRefs = objRefs.stream().filter(objRef => DataUtil.isNotEmpty(objRef)).collect(Collectors.toCollection(LinkedHashSet::new));

Upvotes: 0

Views: 102

Answers (1)

Martijn
Martijn

Reputation: 12102

LinkedHashSet::new is not valid scala. Do you mean to call the constructor of LinkedHashSet? If so, use new LinkedHashSet(_)

The solution you've chosen is translating java to scala. If you do that, there is little value in using scala at all, and you might as well use java.

A more idiomatic solution would be

objRefs.stream().filter(DataUtil.isNotEmpty).asScala(Set)

Upvotes: 2

Related Questions