Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

array, arraylist, vector

 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i want to search an array or arraylist to remove duplicate entries
is there a method to do that or i have to make a search process for it?
[ March 01, 2006: Message edited by: shuini gustav ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by shuini gustav:
i want to search an array or arraylist to remove duplicate entries
is there a method to do that or i have to make a search process for it?

[ March 01, 2006: Message edited by: shuini gustav ]


final List<E> uniqueEntries = newArrayList<E>(new HashSet<E>(theList));
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tony Morris:


final List<E> uniqueEntries = newArrayList<E>(new HashSet<E>(theList));


gee thanks.
but i really am new to this and i do not understand HashSet as yet
what's <E> ? will that statement automatically remove all the duplicate entries in my arraylist or array?
 
Bernard Sigmund Gustav
Ranch Hand
Posts: 170
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the tip
i looked that up and decided to implement a TreeSet instead.

i did that with this line which automatically removes all duplicate entries:
ArrayList catlist = new ArrayList( new TreeSet(category) );

thanks
 
Renu Radhika
Ranch Hand
Posts: 243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but treeSet will sort it also.you should use it only if you want unique elements in a sorted way
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What may be likely is that you want subsequent duplicates removed from the list of elements, but not have the ordering of the elements altered. In that case, LinkedHashSet will do the job:


Don't get me started about generics... I used to think the free dogma generated by the empty category could be our foundation. I was off by an infinity of monads!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic