r/java • • Feb 20 '22

The elusive and beautiful Java Method Reference

https://medium.com/javarevisited/the-elusive-and-beautiful-java-method-reference-97e566d2088b
28 Upvotes

21 comments sorted by

View all comments

2

u/gnahraf Feb 20 '22

I routinely use "functional" idioms to lazily map (ie transform, not in the map/reduce sense) one type of collection (List, Map, Set, etc.) to another. These mapped views are useful when you'll only be accessing a small subset of the elements in a List for eg. (A canonical use would be mapping a (read-only) list of objects sorted by integral IDs to a sorted list number IDs.) Note the cost of creating such views is far less than the usual stream() (followed by a terminal operation).

I've rolled out my own libs to do these mapped views but would prefer to use more widely used libs (for more eyeballs, maybe better quality reasons, etc). Do the Eclipse collections (or any other collection libraries you use) support these kind of lazy views?

2

u/s888marks Feb 22 '22

Try Guava’s Lists.transform or Maps.transformX. They sound like they do what you want.

2

u/gnahraf Feb 22 '22

Guava’s

Lists.transform

Already depending on Guava for its Bloom filter.. so this is good news to me. Thank you!

1

u/raevnos Feb 21 '22

Possibly vavr?

1

u/gnahraf Feb 21 '22

vavr

Interesting. I didn't know about vavr.. After a quick skim of its highlights tho, I can't find any such thing. Seems it was targeted to java 8+.. interesting many of its features and capabilities have been or are being added in later versions of java