• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

How to eliminate duplicates

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i want to eliminate duplicates from a List of certain Objects (for example Instances of the class Person).
The easyest way would be to override equals and hashCode and use a Set.

Now i want to use only some fields of the Person-Instance - for example the last name.
At another place i want to use both - first and last name.

How can i do that? I want to write something like a comparator that i can use to eliminate duplicates from a List.

I know i can do this with for(){} but i think a set would be better.

I hope you understand what i mean - my english isnt the best :-)

Thanks

- Micha
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

From my understanding, List based collections are itself not designed to hold unique elements. So there should be no specific method for doing this. Though you implement comparable or comparator, you have no specific implemenation to avoid duplicates. You can implement compareTo method, see if result is 0 and then remove manually which obviously involves for loop.

hth,
Larsen.
 
micha koern
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Larsen Raja wrote:Hi,

From my understanding, List based collections are itself not designed to hold unique elements. So there should be no specific method for doing this. Though you implement comparable or comparator, you have no specific implemenation to avoid duplicates. You can implement compareTo method, see if result is 0 and then remove manually which obviously involves for loop.

hth,
Larsen.



Thanks for your reply.
Collection-Classes have an addAll()- Method. So it is possible to put all the Elements from a List into a Set.
Now i 'd like to say the compiler: "Hey- take this List and insert each element only one time by this given <Comparator-Thing>"
I know- the comparator it self doesnt work- but which can i take?

A loop is possible for sure, but I thougt there would be a nice/elegant way by using Sets.
 
Sheriff
Posts: 24744
59
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a question about Java programming and not a question about one of the certification exams, right? So let's move it to a better forum...
 
Larsen Raja
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
here is a suggestion, but i dnt knw how far this wld wrk. /if addAll can be used to add list elements to a just created set, you wld just override equals method in class whose objs are inserted into list.
 
Paul Clapham
Sheriff
Posts: 24744
59
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good start, but unfortunately Micha needs at least two different versions of equals(). And of course a class can't have two versions of equals().

And yes, a Comparator isn't the right thing. You need an "Equalator" instead, except that Java doesn't have such a thing. You could consider writing a wrapper class, a class which contains a Person object and which implements equals() based on its Person object and the Person object of another instance of the wrapper. Like this, if I'm not making sense:
 
Sheriff
Posts: 21824
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Larsen Raja wrote:but i dnt knw how far this wld wrk. ... wld ... objs


Please UseRealWords.
 
Marshal
Posts: 66537
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you use Java7, there is an Objects class which might have such methods in. Maybe. It might not; I haven’t looked.
You can write your own Equaliser (!!) interface quite easily.
 
Marshal
Posts: 3838
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, if I had to remove duplicities from a list, I'd probably do:
Am I being too lazy?

Chances are this would perform significantly better than removing duplicates in a for loop (especially for large lists). Plus it is definitely less lines of code, therefore less total bugs in my application. If the order of elements in the list is not important, then a simple HashSet would do.

In any case, the list probably should be a set right from the start, shouldn't it?

Sorry for being so dumb.

If the real deal is to eliminate duplicates based on Comparator, not the equals method, then a TreeSet with given comparator should do. Not as efficient as a hashset, but probably still better than hand-coded for loops.

In any case, the problem seems to be poorly defined, as it is pure chance which of two different persons who happen to have the same last name will remain in the list.
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:And yes, a Comparator isn't the right thing. You need an "Equalator" instead, except that Java doesn't have such a thing.


Yes, when is Java going to add that? It would be a darn useful addition to a Comparator...except I think I might call it an 'Identifier'

Winston
 
Campbell Ritchie
Marshal
Posts: 66537
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Equaliser has much more sinister undertones.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Equaliser has much more sinister undertones.


Yeah, that was a great sig. I suppose you're too young to remember him in 'Callan' (even better, in my opinion).

Winston
 
Campbell Ritchie
Marshal
Posts: 66537
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Winston Gutkowski wrote: . . . I suppose you're too young to remember . . .

Unwarranted flattery will get you anywhere. I don’t remember watching Callan, however.

Shall we get this thread back on topic now?
 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!