Win a copy of Spring in Action (5th edition) this week in the Spring 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Stream min and max methods  RSS feed

 
Ranch Hand
Posts: 66
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Im learning for the OCP exam and came across min and max method on stream. Both methods take the exact same operator but still they yield different results. I do not understand why. In the below code im comparing integers the exact same way. Obviously, I am missing something because from my (incorrect) perspective it seem exact similar methods and therefore would seems two methods are just given different names. The fact that the results are different shows that I am incorrect, but what am I missing?

 
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Bart,

you do determine the max and min in a somewhat involved way, but there is nothing wrong with your code, and you are missing nothing.
The Comparator to determine the max and min is in both cases the same (whether i > j or not does not depend on the qurestion if you want to have the bigger or the smaller of the two). The only difference is that you invoke either the 'max' method or the 'min' method.
 
Bart Boersma
Ranch Hand
Posts: 66
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Piet,

Thanks for your reply.

My comparators are indeed both the same. So how does the min() method decide to take the minimum and the max() method to take the maximum?

Regards
 
Piet Souris
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know how Java implemented this, but there are aeveral methods thinkable. For instance, to determine the min of a list, you could have:

or a Stream version of this, and likewise for the max.

Or you could use Stream.reduce, where the reducing is either the max or the min of the two operands.

If you need some additional practice for the OCPJP, have a look at the summaryStatistics method of the IntStream. That method delivers you the max and min at once. You do have to convert your List<Integer> to an IntStream, using the mapToInt method.
 
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the naturalOrder Comparator because Integer implements Comparable. I always prefer Arrays#stream to Arrays#asList.
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I said Integer min, I forgot the return type of the min() method.
That should of course read Optional<Integer> min
 
Bart Boersma
Ranch Hand
Posts: 66
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks both for your clarifying answers
 
Piet Souris
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome.

Just one thing I forgot to mention:

in your Comparator you use

Note that o1 - o2 can overflow the range of an int, for instance if o1 = Integer.MAX_VALUE and o2 is Integer.MIN_VALUE. So safer is to use something like return Integer.compare(o1, o2) or the classic

or elegantly what Campbell suggests.
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I shall let you work out why PS' comparator can go horribly wrong for Integers. Search Rob Spooer's posts about three weeks ago for the talk by (??) Stuart Marks at Devoxx Belgium 2017.
 
Bart Boersma
Ranch Hand
Posts: 66
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep I get it: say integer1 is 1000 and integer2 is -1000 the comparator would return 0 meaning the integer values are the same which they are clearly not. Just one question: where does PS stand for?
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Bart Boersma wrote:Yep I get it: say integer1 is 1000 and integer2 is -1000 the comparator would return 0 . . . .

No.

Just one question: where does PS stand for?

Piet Souris
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To avoid confusion: identify the logic error in the following compare() method, assuming it compiles normally:-
 
Bart Boersma
Ranch Hand
Posts: 66
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have not been able to identify the logic error ..
 
Piet Souris
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Bart,

my mistake. Since you are comparing two Integers (and thus NOT two ints) 'o1 == o2' will probably be false, since o1 and o2 are probably two different objects, even though their value may be the same. So I should have said:
Check it for yourself:

 
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:identify the logic error in the following compare() method, assuming it compiles normally


I like my IDE. That's a damn hard to figure out as your eye gets badly trained by '<'.
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
CR, BB, PS, just assigned to all a cow, now that's something I'll remember for long long time. Really I was looking hard and long enough until IDE helped me to figure out.
 
Piet Souris
Master Rancher
Posts: 3001
105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Liutauras!

Usually I check my replies, when it comes to code, in my IDE before replying, and the first time I do not test it, I go wrong.... that'll teach me! And with Campbell, you stand no chance of getting away with it    
 
Bart Boersma
Ranch Hand
Posts: 66
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah I should have noticed taking into account I did OCA last year. That shows how fast knowledge gets rusty! Thanks all.
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:. . . you are comparing two Integers (and thus NOT two ints) . . .

Spot on If you made the parameters ints, you would force unboxing and that problem wouldn't apply. At least I think it wouldn't.

And thank you for the cow whoever it was
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . like my IDE. . . .

I know you like IntelliJ; does it give a warning there?
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote:. . . something I'll remember for long long time. . . ..

I remembered that video I mentioned earlier. Links on Rob's post. That problem was addressed in the first half of the talk. Cpmarator#naturalOrder() is discussed in the second half.
 
Liutauras Vilda
Marshal
Posts: 6257
420
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Liutauras Vilda wrote:. . . like my IDE. . . .

I know you like IntelliJ; does it give a warning there?


Yes, moreover, IDE kindly suggested to assess myself whether I want to continue in the field since I couldn't figure out myself. Anyway, at the end it even suggested how to fix problem with couple of keystrokes
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Yesterday, I wrote:. . .

The following will work correctly but cannot be extended to Float/Doiuble because of problems with NaN.I learnt all this from Stuart Marks' talk.
 
Campbell Ritchie
Marshal
Posts: 61741
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Liutauras Vilda wrote: . . . Yes, moreover, IDE . . . even suggested how to fix problem with couple of keystrokes

That's impressive.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!