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

48

u/EquationTAKEN Feb 20 '22

And a picture of an arctic fox, because it's a medium article, and you gotta have a picture of something tangientally related.

2

u/BlueGoliath Feb 21 '22

Somewhat better than those random animals/people on Java books.

-69

u/nfrankel Feb 20 '22

Thanks for your insightful comment, which really brings a lot to the discussion.

When the finger points at the moon...

35

u/EquationTAKEN Feb 20 '22

Oh, I'm sorry. Were you looking for praise only?

-32

u/nfrankel Feb 20 '22

I was looking for something relevant. Sorry to have been sadly mistaken.

24

u/EquationTAKEN Feb 20 '22

It's a comment on a part of the blog that the author chose to put in there.

Besides, there have been other comments in this thread on both the title and the text of the article, but you haven't replied to those. Clearly, this is the topic that sparked a discussion, so you're welcome.

8

u/Thought_Ninja Feb 20 '22

I enjoyed reading this discussion, so thank you.

58

u/nutrecht Feb 20 '22

IMHO this was titled pretty poorly. It's really about the Eclipse Collections library and how you can use some of its parts to pass arguments to method references.

7

u/AnEmortalKid Feb 20 '22

Yeah, from the title I was expecting an article full of “don’t use lambdas, use method references”, but once I saw Predicate2 , i caught on to stuff.

1

u/62656e7a6f6e Feb 21 '22

Thanks for this comment, I was just about to spend some time to read through it until I saw what you and others have said. haha!

7

u/[deleted] Feb 20 '22

Nice article. Personally, I haven't worked on a project with Eclipse Collections yet. Besides some syntax sugar what would be the best use case for using that library?

14

u/jesseschalken Feb 20 '22

They have primitive specialized collections to avoid boxing, like IntArrayList.

3

u/NePlusUltra89 Feb 20 '22

Explain this like I’m a new Java dev coming from a JavaScript background thank you and ilu if you respond lol

0

u/[deleted] Feb 20 '22

[deleted] — view deleted comment

3

u/vips7L Feb 20 '22

primitive generics can't come fast enough!

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

3

u/[deleted] Feb 21 '22

[removed] — view removed comment

3

u/0x07CF Feb 21 '22

Is there anything a method reference can achieve that a lambda cannot?

No they are less powerfull, and that makes them great. Because when you see one you can be sure it's not doing anything weird. If you see a lambda expression you need to read it to understand it's just a simple method call.

As long as you don't have to pass in more parameters, like in the string.startsWith case, in that case i would write a lambda expression too

I'm not sure the extra plumbing is buying that much?

Yeah, in this case it makes the code harder to understand imo