Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

MAX value in stream

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

I was doing the enthuware mock test and came accross the below code. I understand line 1 and 3 return the largest number that is 9. I cannot understand why line 2 and 4 return 3 can someone please help me understand the logic behind this code. Been breaking my head to understand this!!.



Thanks in advance
 
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If we had an IntStream, the use of .max() is very straightforward.

I would be tempted to get an IntStream out of the array directly using this method:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/Arrays.html#stream(int%5B%5D)

And then it is very easy to get the max, as I am sure you will agree.

I guess I just like staying with primitives and arrays and primitive streams when the problem would allow us to.

But here, they decided to first turn the Array of ints to a List<Integer> with wrapper types, not necessarily what I would have done, but that is given.

So we don't have an IntStream, we have a Stream<Integer>.

Now, what does the .max() method on a Stream of this type want as a parameter?

They are really testing to see if you know THAT:

https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/stream/Stream.html#max(java.util.Comparator)

We see it wants a Comparator.  What does a Comparator do?

It takes its two arguments.  If the first one should precede the second, it returns a negative number.
If the first argument is to be considered equal to the second argument, it returns zero.
If the first argument should come after the second argument according to the comparator, it returns a positive number, right?

So let's see what those goofy "Comparator" lambdas or method references that we pass in do:
(a, b)->a>b?a:b)
Integer::max

Well, they take two int values (or Integer which can be un-boxed to int) and return an int (or an Integer which can be un-boxed to int) so they are legal.  But what do they do for us?
Instead of returning us a negative number if a should come first, 0 if they should be treated equally, or a positive number if b should come first, they return the larger of the two values in each case.

This is technically legal, but as you can see makes no sense whatsoever as the logic for a comparator.

So they represent a common mistake that you might make if you weren't thinking, or were rushing, and which would not be caught by the compiler because it is legal despite being illogical.

The mistake is in the category of:

double weeklySalary = hourlyRate - hoursWorked;  // syntactically valid and can execute but definitely not what we mean!

If we weren't dealing with Integer/int quantities which can be auto-boxed/un-boxed to each other these confused attempts would not even have compiled.
But we had the bad luck to be working with int/Integer, so it compiles but makes no sense.
 
Jesse Silverman
Saloon Keeper
Posts: 1728
63
Eclipse IDE Postgres Database C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it is easier to see that those lines are wrong than that they will always return the first value.

Do you see why that happens?  Can you explain it in a sentence?
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priyanka,

So in both cases, the problem is that the Comparator you are using is causing this confusion. So the max method takes a Comparator and uses that to compare each object with the other. The comparator is supposed to compare the two numbers, return a positive value is the first one is bigger or a negative value if the second one is bigger. So something like this:


But your comparator always returns the biggest value, so whenever it compares two numbers, it will always thinks the first number is bigger. So it will do the following:

Arrays.asList(3,4,6,9,2,5,7);
1. Compare 3 to 4, comparator will return 4, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
2. Compare 3 to 6, comparator will return 6, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
3. Compare 3 to 9, comparator will return 9, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
4. Compare 3 to 2, comparator will return 3, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
5. Compare 3 to 5, comparator will return 5, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.
6. Compare 3 to 7, comparator will return 7, since it is a positive number the Stream thinks the first number i.e. 3 is bigger.

So due to the comparator you are passing, the stream thinks the first number is the biggest.
 
Priyanka Leo
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ahhh now i get it!!! i was breaking my head about this!! thanks!!!
 
reply
    Bookmark Topic Watch Topic
  • New Topic