aspose file tools*
The moose likes Java in General and the fly likes Comparator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparator" Watch "Comparator" New topic
Author

Comparator

veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67


OUTPUT:

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


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

what went wrong?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

You are printing the hs. Try printing ts. It should work.


~ Mansukh
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

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.



http://plainoldjavaobject.blogspot.in
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38348
    
  23
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

Joined: May 16, 2011
Posts: 67
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
Sheriff

Joined: Oct 13, 2005
Posts: 38348
    
  23
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

Joined: May 16, 2011
Posts: 67
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
Sheriff

Joined: Oct 13, 2005
Posts: 38348
    
  23
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

Joined: May 16, 2011
Posts: 67
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



Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 16, 2011
Posts: 67
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

Joined: Jun 08, 2010
Posts: 384

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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 16, 2011
Posts: 67
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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?
PrashanthKumar Vuthuru
Greenhorn

Joined: May 14, 2013
Posts: 8
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

Joined: May 16, 2011
Posts: 67


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

Joined: May 14, 2013
Posts: 8
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 14, 2013
Posts: 8
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 14, 2013
Posts: 8
Jeff Verdegan Ok AM New To This Site Sorry Boss
veena bijur
Ranch Hand

Joined: May 16, 2011
Posts: 67
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

Joined: Jan 03, 2004
Posts: 6109
    
    6

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

Joined: May 16, 2011
Posts: 67
Jeff Verdegan,

Thanks a lot.

I got it, now its working.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You're welcome. I'm glad I could help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator