Finding Max with Lambda Expression in Java

47,891

Solution 1

The method Comparator.comparing(…) is intended to create a Comparator which uses an order based on a property of the objects to compare. When using the lambda expression i -> i, which is a short writing for (int i) -> { return i; } here, as a property provider function, the resulting Comparator will compare the values itself. This works when the objects to compare have a natural order as Integer has.

So

Stream.of(1,2,4,3,5).max(Comparator.comparing(i -> i))
.ifPresent(maxInt->System.out.println("Maximum number in the set is " + maxInt));

does the same as

Stream.of(1,2,4,3,5).max(Comparator.naturalOrder())
.ifPresent(maxInt->System.out.println("Maximum number in the set is " + maxInt));

though the latter is more efficient as it is implemented as singleton for all types which have a natural order (and implement Comparable).

The reason why max requires a Comparator at all, is because you are using the generic class Stream which might contain arbitrary objects.

This allows, e.g. to use it like streamOfPoints.max(Comparator.comparing(p->p.x)) to find the point with the largest x value while Point itself does not have a natural order. Or do something like streamOfPersons.sorted(Comparator.comparing(Person::getAge)).

When using the specialized IntStream you can use the natural order directly which is likely to be more efficient:

IntStream.of(1,2,4,3,5).max()
.ifPresent(maxInt->System.out.println("Maximum number in the set is " + maxInt));

To illustrate the difference between “natural order” and a property based order:

Stream.of("a","bb","aaa","z","b").max(Comparator.naturalOrder())
.ifPresent(max->System.out.println("Maximum string in the set is " + max));

this will print

Maximum string in the set is z

as the natural order of Strings is the lexicographical order where z is greater than b which is greater than a

On the other hand

Stream.of("a","bb","aaa","z","b").max(Comparator.comparing(s->s.length()))
.ifPresent(max->System.out.println("Maximum string in the set is " + max));

will print

Maximum string in the set is aaa

as aaa has the maximum length of all Strings in the stream. This is the intended use case for Comparator.comparing which can be made even more readable when using method references, i.e. Comparator.comparing(String::length) which almost speaks for itself…

Solution 2

This function (note -> is for closures and not to be confused with => which is for comparison)

i -> i

just means you need to compare the entire object as it is. i.e. if I have an i you need to compare i

A less trivial example might be

max(Comparator.comparing(i -> -i))

which will give you the minimum or

max(Comparator.comparing(i -> Math.abs(100-i))

gives you a value which is farthest from 100.

max(Comparator.comparing(i -> i.toString()))

which will give you the maximum comparing as a String i.e. "9" > "10" as a string.

Share:
47,891
Kick Buttowski
Author by

Kick Buttowski

Love Java Confused by others ... Fall in love with AngularJS, so my life has some excitement right now ;) In these days, I am working to become MEAN. ;) Very disappointed by the drastic changes that AngularJS will have in version 2, so I decided to try KO ;) after being confused for awhile, I encounter ruby on rails and I am in heaven ;) By the way, My mother gave me 2 characteristics 1. Tenacity 2. Dancing which I use all of them when I am coding <3 Lists of My Jobs So Far with RoR, Bootstrap, JS, and JQuery http://www.cicceropizza.com https://lucia-pizza.herokuapp.com/ http://izakt.herokuapp.com ---> under more improvements http://www.pizzadimarcovalencia.pizza https://todo-app-bykickbuttowski.herokuapp.com https://todo-4more1day-bykickbuttowski.herokuapp.com/ https://survey-pie-chart.herokuapp.com/ ActionCable https://buying-items-byit.herokuapp.com/ e-commerce I do not what happened, but I found myself learning react and now this is my first app https://react-counter-app-by-kick.herokuapp.com/

Updated on July 09, 2022

Comments

  • Kick Buttowski
    Kick Buttowski almost 2 years

    This is my code

        List<Integer> ints = Stream.of(1,2,4,3,5).collect(Collectors.toList());
        Integer maxInt = ints.stream()
                                  .max(Comparator.comparing(i -> i))
                                  .get();
    
        System.out.println("Maximum number in the set is " + maxInt);
    

    output:

    Maximum number in the set is 5
    

    I cannot make distingues between two i in below section of my code

    Comparator.comparing(i -> i)
    

    can anyone be kind and explain the difference between two i?

  • Kick Buttowski
    Kick Buttowski almost 10 years
    can you either explain more or provide me a source so I can read about it?
  • Kick Buttowski
    Kick Buttowski almost 10 years
  • Vishy
    Vishy almost 10 years
    => is comparison, -> is a closure.
  • Vishy
    Vishy almost 10 years
    I suggest you look at the examples here docs.oracle.com/javase/tutorial/java/javaOO/…
  • Kick Buttowski
    Kick Buttowski almost 10 years
    how can i print out the two i in console?
  • Vishy
    Vishy almost 10 years
    @KickButtowski There is only one i. The first defines the input, the second refers to the output.
  • Kick Buttowski
    Kick Buttowski almost 10 years
    can you please show me a tutorial that just talked about this issue that I have plz
  • Kick Buttowski
    Kick Buttowski almost 10 years
    how they are the same? if they are the sane why we compare them together?
  • Vishy
    Vishy almost 10 years
    @KickButtowski Do you mean the oracle tutorial on Lambdas I linked to a few comments back? ;) Say these words " -> is NOT a comparison ". I have said this twice now.
  • Kick Buttowski
    Kick Buttowski almost 10 years
    the tutorial just talks about this issue. can you put a diagram in your answer to show the relation between two i i-> i plz
  • Smutje
    Smutje almost 10 years
    Which part of my answer didn't you understand?
  • Kick Buttowski
    Kick Buttowski almost 10 years
    Comparator.comparing expects a function which maps the source object to the value which actually gets compared -
  • Kick Buttowski
    Kick Buttowski almost 10 years
    can you add a diagram to show the relationship of two I i->i
  • Smutje
    Smutje almost 10 years
    the two i are identical - Comparator.comparing expects a function to be applied on every input element and as the function i -> i describes that you take every input and return itself as an output, the values to compare are not transformed.
  • Kick Buttowski
    Kick Buttowski almost 10 years
    so I got this, but what is gonna compare to what? isn't i compare to i? why we need to compare i to i when they are the same thing? can u understand my confusion?
  • Kick Buttowski
    Kick Buttowski almost 10 years
    may be my issue is that I don't think functional?
  • Smutje
    Smutje almost 10 years
    You don't compare i to i, you compare every object of the list to every other using the given function to pre-process the objects to compare. And as your function is i -> i, the values to compare are not pre-processed at all.
  • Kick Buttowski
    Kick Buttowski almost 10 years
    what do you mean about not pre processed?
  • Smutje
    Smutje almost 10 years
    To shorten the discussion: You used an example too complex for beginners, the code to get the maximum value from a stream of int can be written as ints.stream().max(Comparator.naturalOrder()).get(); - maybe this will help you understand more as it doesn't contain any lambda but a provided function to compare.
  • Vishy
    Vishy almost 10 years
    @KickButtowski If you can think of a diagram which would help, I will add it. It really is very simple, in fact it could hardly be simpler and still be a closure. Don't assume it is doing more than it is.
  • Kick Buttowski
    Kick Buttowski almost 10 years
    digram like to show one to one relation ship in the set. you know what happened? I had a example that use track -> track.getName , it is kind of make sense but i->i is hard to digest.
  • Vishy
    Vishy almost 10 years
    @KickButtowski it's almost too simple to see what point there is in it being there. It is hard to find the purpose of something effectively does nothing by design.
  • Holger
    Holger almost 10 years
    Just recall that i -> i is a short writing for (int i) -> { return i; }. By looking at the longer form it should become clear what happens…
  • Kick Buttowski
    Kick Buttowski almost 10 years
    @holger so I got it but can you explain what happen to i inside the function so at the end we end up with i as our return value?
  • Holger
    Holger almost 10 years
    @Kick Buttowski: nothing happens to i. The specified function is only used to determine the ordering property for the Comparator. If the specified function returns just the value itself it implies that the natural order of the values is used. This is obsolete as Comparator.naturalOrder() would do the same. But if you return -i instead it would reverse the order. And if you use i -> Integer.bitCount(i) it would sort int numbers by their bit count.
  • Kick Buttowski
    Kick Buttowski almost 10 years
    @Holger see your explanation is so clear that even my mother can understand it. Could you please post up your explanation with enough detail so I can accept as my answer? please explain it in the way that novice programmers in this case can understand the point like compact tutorial :)
  • Kick Buttowski
    Kick Buttowski almost 10 years
    @Holger so this natural ordering is kind of redundant here?
  • Kick Buttowski
    Kick Buttowski almost 10 years
    what do you mean natural order? can you break it down with a sample?what do you mean it compares value to itself? can u break it down with a sample?
  • Kick Buttowski
    Kick Buttowski almost 10 years
    so in this case i -> i is kind of redundant ?
  • Holger
    Holger almost 10 years
    Yes, as said, it does the same as Comparator.naturalOrder(). So for IntStream you can even omit the Comparator and achieve the same.
  • Kick Buttowski
    Kick Buttowski almost 10 years
    i posted my answer as a question, so could you take a look at it and share your thought with me? stackoverflow.com/questions/24399181/…