awgtek awgtek - 1 year ago 83
Java Question

How to get mapped items of stream into a set

Not sure what's the best (most efficient, performant, and/or fewest characters needed, with preference to the fewest characters) way to effectively get the resulting set of a mapping using Java streams.

I.e. what would effectively replace:

Set<String> groupings = new HashSet<>();
for (Item item : items) {
return groupings;

Should I use map(...) following by a distinct(...)? Looking for solutions to this.

Answer Source

I might have misunderstood you, but it could look like this:

Set<String> groupings =

Since the collector is a set, no duplicates will be allowed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download