Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #7572
| From | Stanimir Stamenkov <s7an10@netscape.net> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: remove duplicates? |
| Date | 2011-09-05 12:36 +0300 |
| Organization | A noiseless patient Spider |
| Message-ID | <j42569$n2g$1@dont-email.me> (permalink) |
| References | <c299b494-241e-481f-b993-a6272f15b915@j1g2000yqn.googlegroups.com> |
Mon, 5 Sep 2011 01:44:06 -0700 (PDT), /bob/:
> Let's say you have a Vector of String objects. What is the easiest
> way to remove duplicates?
Here what I immediately think of:
List<String> list;
...
Set<String> set = new HashSet<String>();
for (Iterator<String> iter = list.iterator();
iter.hasNext(); ) {
String str = iter.next();
if (!set.add(str)) {
iter.remove();
}
}
If you want to leave the last occurrences of the String elements you
would just iterate the list backwards:
for (ListIterator<String> iter = list.listIterator(list.size());
iter.hasPrevious(); ) {
String str = iter.previous();
...
--
Stanimir
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
remove duplicates? bob <bob@coolgroups.com> - 2011-09-05 01:44 -0700 Re: remove duplicates? RedGrittyBrick <RedGrittyBrick@spamweary.invalid> - 2011-09-05 10:23 +0100 Re: remove duplicates? Stanimir Stamenkov <s7an10@netscape.net> - 2011-09-05 12:36 +0300 Re: remove duplicates? rossum <rossum48@coldmail.com> - 2011-09-05 12:46 +0100 Re: remove duplicates? Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-09-05 08:38 -0400 Re: remove duplicates? Patricia Shanahan <pats@acm.org> - 2011-09-05 05:51 -0700 Re: remove duplicates? Roedy Green <see_website@mindprod.com.invalid> - 2011-09-05 11:07 -0700
csiph-web