• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

better understanding of the code (1Z0-819)

 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, I would like to better understand how the codes below work, especially this part  :






my problem is with the sorting.
thanks
 
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tiam,

stream.sorted takes either no parameter, in which case the elements of the stream must implement Comparable and the sorting is done on that basis, or it takes a Comparator which then is used on the sorting.

In your code-snippets there are two Comparators mentioned:

1) a Comparator<String> 'lengthComp' that compares strings on the basis of their lengths
2) a Comparator<String> in the form of a method-reference.

lengthComp is given with a lambda-expression:

and the second Comparator

is in fact this Comparator:

and it uses the natural ordering of Strings.
If you look at the api of the Comparator-class, you see that if you have the Comparators c1 and c2, then

is also a Comparator, that sorts its elements first according to c1, and in case two elements are equal with respect to this Comparator, another comparison is done based on the Comparator c2.

So,

is a Comparator that first sorts the elements on the basis of their lengths, and in case these lengths are the same, the elements are then sorted according to their natural ordering.

So, the list ["bb", "b", "aa", "a"] will become, after sorting with that Comparator: ["a", "b", "aa", "bb"]
If we sort that list only on the basis of the lengthComp-comparator, we would get: ["b", "a", "bb", "aa"]
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I forgot to mention that comparing things on the basis of the difference of their lengths is not always safe. For instance, Integer.MAX_INT - Integer.MIN_INT
does not fit in an integer, and may cause wrong sorting results. In this case, string.length() is safe, however.

Two alternatives to stringComp are:
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:I forgot to mention that comparing things on the basis of the difference of their lengths is not always safe. For instance, Integer.MAX_INT - Integer.MIN_INT
does not fit in an integer, and may cause wrong sorting results. In this case, string.length() is safe, however.

Two alternatives to stringComp are:



first of all, thank you for your concise explanations. i understand the difference perfectly. i would like to take this opportunity to ask you two questions:
What is the difference between the comparable interface and the comparator interface?

Why can't we implement the comparable interface with the following :



on the other hand this compiles :



the compareTo method is the abstract method of comparable, not comparator.

I am training on java and I want to understand these two interfaces

thanks
 
Piet Souris
Bartender
Posts: 5465
212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tiam,

the way I always remember it is as follows:

first: in maths, a function f(x, y) is a function of two parameters. If we fix a value for y, say y = Y, then we can say that g(x) = f(x, Y) is a function that takes one parameter.

If you look at the api of the Comparator-interface, you see that the method to implement is:

and so this is a BiFunction, taking two parameters, and returns an int.

If we look at Comparable, we see this abstract method:

and so this is a Function, taking one parameter and returning an int.

If you do:

then you define a Comparable with a function that takes two parameters, s1 and s2. And that is incorrect.

If we fix a String, say S = "Hello Tiam", then

is a function of only one parameter (like in the f(x, Y) that I mentioned above). And so:

is also correct. Maybe this is even more clear:
 
Tiam Bezalel
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Piet Souris wrote:hi Tiam,

the way I always remember it is as follows:

first: in maths, a function f(x, y) is a function of two parameters. If we fix a value for y, say y = Y, then we can say that g(x) = f(x, Y) is a function that takes one parameter.

If you look at the api of the Comparator-interface, you see that the method to implement is:

and so this is a BiFunction, taking two parameters, and returns an int.

If we look at Comparable, we see this abstract method:

and so this is a Function, taking one parameter and returning an int.

If you do:

then you define a Comparable with a function that takes two parameters, s1 and s2. And that is incorrect.

If we fix a String, say S = "Hello Tiam", then

is a function of only one parameter (like in the f(x, Y) that I mentioned above). And so:

is also correct. Maybe this is even more clear:



it's clearer to me now with the mathematical example. if the sorting is to be done on the basis of a single element, we can use comparable but if the sorting is to be done on the basis of several elements, we can use comparator.
what is the preferred method? sorting on the basis of a single element or sorting on the basis of several elements?

I'm continuing my research.

thanks
 
Piet Souris
Bartender
Posts: 5465
212
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorting is done by comparing repeatedly two elements, until all elements are sorted.

If you use stream.sorted without an argument, then it assumed that the elements are Comparable (otherwise it will not compile), and java uses
s1.compareTo(s2) to compare the elements s1 and s2.

If you supply a comparator, then java will use comparator.compare(s1, s2) to compare s1 and s2, even if s1 and s2 are comparable.
 
But how did the elephant get like that? What did you do? I think all we can do now is read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic