• 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

Comparator

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


OUTPUT:

[amritsar br, bangalore br, orrisa br, mumbai br]


but it should be
[amritsar br, bangalore br, mumbai br, orrisa br]

what went wrong?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are printing the hs. Try printing ts. It should work.
 
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi veena,

You need to create the equals() and hashcode() in your FullName Class.
This is how the comparator will distinguish between the objects of type FullName.

 
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lalit Mehra wrote: . . . You need to create the equals() and hashcode() in your FullName Class.
This is how the comparator will distinguish between the objects of type FullName. . . .

Nonsense. The Comparator does not use those methods. They may however be required for correct operation of the hash set, so they would have to be correctly overridden, not created.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK
Thanks a lot. I will work on it. Had one more question.
As comparator interface has 2 methods equals() & compare(),but have overridden only compare() still it compiles successfully how is it possible? As we implement any interface all of its methods should be overriden else the class should marked as abstract.

Please correct me if i am wrong
Thanks in advance.
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look very closely at the equals method and see whether you have ever seen anything like it. Or, alternatively, remind yourself of the Comparator API documentation for that method.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK i had i look. I found equals() in object class that will be inherited in every class by default so the above class compiles successfully? I am i correct?
 
Campbell Ritchie
Marshal
Posts: 79178
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is correct. The link I gave you tells you it never causes problems if you don't override equals, but that might improve performance.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks So much.



output:
Sorting by id
[21 bangalore, 21 orrisa, 21 mumbai, 100 amritsar]
Sorting by names
[100 amritsar, 21 bangalore, 21 mumbai, 21 orrisa]


1. Just have look at the code
2. same as previous one but ArrayList class have used
3. it works fine, i mean i get the correct output, but why i am getting wrong output in HashSet

Please tell what rules to follow to sort elements in HashSet



 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

veena bijur wrote:
3. it works fine, i mean i get the correct output, but why i am getting wrong output in HashSet



ItDoesntWorkIsUseless(⇐click). You have to TellTheDetails(⇐click) of exactly what you tried and exactly what results you got.

Note, however, that you can see for yourself that the sort() method does not accept Sets. And if you read the docs for Sets, you'll see that they don't have a predictable order.

Please tell what rules to follow to sort elements in HashSet



The rules to follow for sorting a HashSet are this: Don't use HashSet. You can't sort it. Instead use a SortedSet, such asTreeSet.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Jeff,

Thanks a lot.

1. Have tried to sort user defined object Stud based on id and names using ArrayList class

2. have used comparator interface and overridden compare()



3. and displayed the contents of ArrayList , names displayed in sorted list , same thing i tried with HashSet it was not in sorted order.

One more question:

1. When we need to override hashcode() & equals()







 
Lalit Mehra
Ranch Hand
Posts: 384
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Lalit Mehra wrote: . . . You need to create the equals() and hashcode() in your FullName Class.
This is how the comparator will distinguish between the objects of type FullName. . . .

Nonsense. The Comparator does not use those methods. They may however be required for correct operation of the hash set, so they would have to be correctly overridden, not created.



Ok my mistake as i wrote "create" instead of "override" and i also didn't check that she is sending a hashset into sort() which takes a list.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

veena bijur wrote:
3. and displayed the contents of ArrayList , names displayed in sorted list , same thing i tried with HashSet it was not in sorted order.



That doesn't tell us any more detail than your previous post. But I already addressed that anyway. Please go back and read what I wrote.

1. When we need to override hashcode() & equals()



You need to override equals any time you want to be able to use the standard approach to compare your objects for equality based on their states (contents). You need to override hashCode any time you are going to use your class in a hash-based data structure. And finally, any time you override one of those methods, you should override both. In some cases it may not be strictly necessary to override both, but it's extremely bad practice not to.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jeffy.


ok, Read go it.

i have used Collections.sort() in (ArrayList objects) this takes care of sorting user defined objects based on comparator object based to it.

Where as Collections.sort() doesnt accept Set.

So the way i have implemented is totally different.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

veena bijur wrote:
ok, Read go it.

i have used Collections.sort() in (ArrayList objects) this takes care of sorting user defined objects based on comparator object based to it.

Where as Collections.sort() doesnt accept Set.

So the way i have implemented is totally different.



So do you still have a question? Or are you just telling us that you worked out what you need by a different approach?
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys HashSet Importance


HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table for storage. As most readers likely know, a hash table stores information by using a mechanism called hashing. In hashing, the informational content of a key is used to determine a unique value, called its hash code. The hash code is then used as the index at which the data associated with the key is stored. The transformation of the key into its hash code is performed automatically-you never see the hash code itself. Also, your code can't directly index the hash table. The advantage of hashing is that it allows the execution time of basic operations, such as add( ), contains( ), remove( ), and size( ), to remain constant even for large sets.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i mean to say i have worked on it and got it.



1. Have look at the above code
2. Have used TreeSet to sort user defined objects based on id and names.
3. OUTPUT:

Sorting by names
[100 amritsar, 21 bangalore, 213 o, 212 orrisa]

Sorting by id
[101 amritsar]

4. why id's not getting sorted only one id is getting displayed?

5. i need to override equals() and hashcode() to check the equality of user defined objects based on all of its data members complusory?

Please tell what mistake have made in coding







 
PrashanthKumar Vuthuru
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys I Have A Question Can Any One Answer

G1,G3,G2,G10 elements i Addeed In Treeset & I Implemented Comparator Interface Everything is ok.At The End Output is


G1
G10
G2
G3


But i need output like G1 G2 G3 G10
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PrashanthKumar Vuthuru wrote:In hashing, the informational content of a key is used to determine a unique value, called its hash code.



No, it's not unique.

The hash code is then used as the index at which the data associated with the key is stored.



It's used to determine the index. It almost never becomes the index itself.

The transformation of the key into its hash code is performed automatically-you never see the hash code itself.



You do if you call key.hashCode(). But unless you're writing a hash-based data structure, you usually don't have any reason to do that.

 
PrashanthKumar Vuthuru
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Veena bijur According to my knowledge am posting this Program let me know do you have any queries???

[MODERATOR ACTION: Removed full code solution]
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

PrashanthKumar Vuthuru wrote:The End Output is

G1
G10
G2
G3



Do you understand why?

But i need output like G1 G2 G3 G10



If you understand why you got the output you got, then you should be able to start on a procedure for your compare() method to follow.
 
PrashanthKumar Vuthuru
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff Verdegan Ok AM New To This Site Sorry Boss
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Prashant guide me where i went wrong

1. Have look at the above code
2. Have used TreeSet to sort user defined objects based on id and names.
3. OUTPUT:

Sorting by names
[100 amritsar, 21 bangalore, 213 o, 212 orrisa]

Sorting by id
[101 amritsar]

4. why id's not getting sorted only one id is getting displayed?

5. i need to override equals() and hashcode() to check the equality of user defined objects based on all of its data members complusory?

Please tell what mistake have made in coding



Please help me.

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

veena bijur wrote:
Sorting by id
[101 amritsar]

4. why id's not getting sorted only one id is getting displayed?



Look at your ID Comparator. Don't you see a few things that are very wrong?


If you can't spot what's wrong, then explain in English what you think this means, and how it fits into Comparator's rules


Then do the same for the next two blocks.



5. i need to override equals() and hashcode() to check the equality of user defined objects based on all of its data members complusory?



No. You define equality for a class based on whatever makes sense to you in terms of the design and semantics of that class. THen you define hashCode() to produce a well-distributed has that is consistent with equals() according to the rules described in hahsCode()'s docs.
 
veena bijur
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff Verdegan,

Thanks a lot.

I got it, now its working.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome. I'm glad I could help.
reply
    Bookmark Topic Watch Topic
  • New Topic