• 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

removing duplicates from the list?

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to eliminate duplicate objects from the list.


i know a1,a2,a3 objects are different. but a1,a2 are having the same values(instance). i want to eliminate this kind of objects from my list.
please let me know how to do this?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way to eliminate duplicates is by using Set.

Set s = new HashSet(list_object);

But make sure you override the equals and hashCode methods in class A with all three members x,Y,z;
 
Ranch Hand
Posts: 153
Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you should be knowing that List is made allow the duplicates i.e any thing Object is allowed to add in the List and Set is made to take the only unique values. As the guys suggested here chose your option either you eliminate the duplicates or use Set

I hope that helps.
 
rama rajesh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Srinivas Kollaparthi wrote:The way to eliminate duplicates is by using Set.

Set s = new HashSet(list_object);

But make sure you override the equals and hashCode methods in class A with all three members x,Y,z;



thanks .

i heard that answer override hashcode and equals method. but i dont know how to override these methods.
please provide me the code.
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

rama rajesh wrote:i heard that answer override hashcode and equals method. but i dont know how to override these methods.
please provide me the code.



In Collections, the items are put into buckets.
The hashCode determines which bucket item goes into.

Set doesn't allow duplicate items.
So, if there is an item already in the bucket and you try to add new item with same hashCode, the new item is discarded.
I'm giving the code below, but understand before using it.

Add the following member functions to the A class

Just read => DontBeACodeMill <= and => LetThemDoTheirOwnHomework <=

The hashCode() returns the unique int value for an item.
Two equal items have same hashCode, so you can say multiply variables x, y, z

Choose the hashCode function efficiently.
If you multiply x, y, z in the function, objects [10 20 30] and [20 10 30] may be counted as equal, which you don't want.

Your implementation of equals() method must satisfy the properties of equivalence relation (Reflexive, Symmetric, Transitive, Consistent and Null Comparison)
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Trivikram Kamat wrote:

I'm giving the code below, but understand before using it.



Please do not post complete code.
 
rama rajesh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Trivikram Kamat wrote:

rama rajesh wrote:i heard that answer override hashcode and equals method. but i dont know how to override these methods.
please provide me the code.



In Collections, the items are put into buckets.
The hashCode determines which bucket item goes into.

Set doesn't allow duplicate items.
So, if there is an item already in the bucket and you try to add new item with same hashCode, the new item is discarded.
I'm giving the code below, but understand before using it.

Add the following member functions to the A class

Just read => DontBeACodeMill <= and => LetThemDoTheirOwnHomework <=

The hashCode() returns the unique int value for an item.
Two equal items have same hashCode, so you can say multiply variables x, y, z

Choose the hashCode function efficiently.
If you multiply x, y, z in the function, objects [10 20 30] and [20 10 30] may be counted as equal, which you don't want.

Your implementation of equals() method must satisfy the properties of equivalence relation (Reflexive, Symmetric, Transitive, Consistent and Null Comparison)



thansk a lot. now it is working fine. everyone say something what to do. i know what to do , but unable to do. once again thanks a lot.
reply
    Bookmark Topic Watch Topic
  • New Topic