~ Mansukh
http://plainoldjavaobject.blogspot.in
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.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. . . .
veena bijur wrote:
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
Campbell Ritchie wrote:
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.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. . . .
http://plainoldjavaobject.blogspot.in
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.
1. When we need to override hashcode() & equals()
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.
PrashanthKumar Vuthuru wrote: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.
PrashanthKumar Vuthuru wrote:The End Output is
G1
G10
G2
G3
But i need output like G1 G2 G3 G10
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
veena bijur wrote:
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?
Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |