• Post Reply Bookmark Topic Watch Topic
  • New Topic

List with out duplicates  RSS feed

 
abheeshek reddy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Naturally in java a list will allow duplicates, but if i want the list which shouldn't allow duplicates then what should be the logic?


Thanks in advance
abhee
 
Arun R. S. Chandran
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
friend,

You can use a loop while you are adding values and check your condition
-ARUN
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A better solution than manually checking is to use a Set. A Set doesn't allow duplicates.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you go through the Set interface and its subinterfaces you will find hybrid Sets which preserve insertion order.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Arun R. S. Chandran:
friend,

You can use a loop while you are adding values and check your condition
-ARUN


Do not do this, its a very poor solution. you should always look for something in the API first. The solution is a Set implementation, for example HashSet. You then override the equals, hashCode methods from Object in the class you are adding to the collection. When you try to add something the set ensures its not added if its already there.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So now three people have provided basically the same answer. I think the OP gets it.
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:
So now three people have provided basically the same answer. I think the OP gets it.



:roll: :roll:

Way to encourage forum posters not to bother.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Johnson:
:roll: :roll:

Way to encourage forum posters not to bother.


Gregg Bolinger 1 Campbell Ritchie 2 Tom Johnson 3

So there is no need for the :roll:

 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Johnson:



:roll: :roll:

Way to encourage forum posters not to bother.


Well, yea, when someone already provides the answer, why keep repeating it?
 
Tom Johnson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gregg Bolinger:


Well, yea, when someone already provides the answer, why keep repeating it?


I felt I added further info on top of yours and Campbells replies, namely about what you need to do in order to use the Set correctly (i.e. the equals and hashCode override). I know this is in the API but I thought it would help to point the OP in the right direction. Anyway....I dunno, just never got reprimanded for being too helpful before
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I agree with Tom Johnson: your post did add useful information to what preceded it.
 
abheeshek reddy
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks all, for your precious time.

I am on my work.........
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!