Removing duplicates without overriding hash method

Posted by Javi on Stack Overflow See other posts from Stack Overflow or by Javi
Published on 2010-04-20T11:28:21Z Indexed on 2010/04/20 11:33 UTC
Read the original article Hit count: 441

Filed under:
|
|
|
|

Hello,

I have a List which contains a list of objects and I want to remove from this list all the elements which have the same values in two of their attributes. I had though about doing something like this:

List<Class1> myList;
....
Set<Class1> mySet = new HashSet<Class1>();
mySet.addAll(myList);

and overriding hash method in Class1 so it returns a number which depends only in the attributes I want to consider.

The problem is that I need to do a different filtering in another part of the application so I can't override hash method in this way (I would need two different hash methods).

What's the most efficient way of doing this filtering without overriding hash method?

Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about list