This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes removing duplicates from the list? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "removing duplicates from the list?" Watch "removing duplicates from the list?" New topic
Author

removing duplicates from the list?

rama rajesh
Greenhorn

Joined: Jun 22, 2010
Posts: 28
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?
Srinivas Kollaparthi
Greenhorn

Joined: Jan 19, 2008
Posts: 11
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;
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

1. pass your list into HashSet/Set . i.e use Set datastructure :Set aSet = new HashSet(aList);
2. make sure that you override equals and hashCode methods of your A object

if you dont want this approach, then you need to iterate the List and get the object, compare and remove the object which has identical value
<edit>srinivas beaten me </edit>
Malatesh Karabisti
Ranch Hand

Joined: Jul 28, 2010
Posts: 153

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

Joined: Jun 22, 2010
Posts: 28
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.
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
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)

OCPJP6
Sridhar Santhanakrishnan
Ranch Hand

Joined: Mar 20, 2007
Posts: 317
Trivikram Kamat wrote:

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



Please do not post complete code.
rama rajesh
Greenhorn

Joined: Jun 22, 2010
Posts: 28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: removing duplicates from the list?
 
Similar Threads
Sudoku solver help (not brute force)
Applet Serialization
Use of method local inner class being abstract...
Can anybody tell me how to use make use of ArrayLists in my program
multi-dimension array question - dan's exam