Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the advantage of using method references in this case?

I have two variables as follows:

List<Basket> bigBasket = new ArrayList<>(); Basket basket = new Basket();

I wrote the following code to add a relationship between items in bigBasket and basket:

for (Fruit specialFruit : bigBasket.get(0).getFruitInBasket()) {
    for (Fruit fruit : basket.getFruitInBasket()) {
        specialFruit.addRelationship(fruit);
    }
}

Now this is all fine but IntelliJ inspected the code and offered to improve it with the using a method reference so I got the following:

for (Fruit specialFruit : bigBasket.get(0).getFruitInBasket()) {
   basket.getFruitInBasket().forEach(specialFruit::addRelationship);
}

So the above has fewer lines but what is the actual advantage of it? I am not entirely clued up on the benefits of Java 8 features so that's probably why I don't really understand what's happening.

In my opinion, the "improved" version of code is not very readable in the sense of immediately understanding what's happening vs a standard for loop assuming you don't have much knowledge about Java 8 features.

Could someone explain the benefits of the "improved" code vs the standard for loop?

EDIT: Removed incorrect references to lambdas. The code is just using method references - just a lingo mistake on my part!

like image 243
user2844485 Avatar asked Feb 19 '15 10:02

user2844485


People also ask

What type of methods can be pointed by method references?

A method reference to a static method. A method reference to an instance method of an arbitrary type. A method reference to an instance method of an existing object. Constructor reference.

What are we using lambdas and method reference for?

You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name.

Are method references faster?

With Kotlin's sequence, using lambda and method reference have roughly the same performance. However, with Java 8 streams, using lambda is much faster than method reference. One would tend to think that using method reference should be faster than lambda because there is no additional closure.

Can we use method reference without functional interface?

Of course, here one thing is equally important to keep in mind that Method references & Lambda expressions both can only be used in context of Functional Interfaces. In the above code snippet, Interface A, is a functional interface.


1 Answers

I see no advantage in the "improved" code in terms of performance. Both snippets would iterate over the entire basket.getFruitInBasket() Collection, and perform the exact same action for each element.

Preferring one way over the other is simply a matter of taste. In my opinion, the Java 8 version expresses the fact that you perform a single action on all elements of the Collection more clearly (assuming you are used to method references).

BTW, there's no lambda expression in your code, just a method reference.

like image 84
Eran Avatar answered Oct 27 '22 00:10

Eran