Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35198
| Path | csiph.com!aioe.org!.POSTED!not-for-mail |
|---|---|
| From | Arne Vajhøj <arne@vajhoej.dk> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: DeDup an ArrayList |
| Date | Thu, 4 Aug 2016 22:41:18 -0400 |
| Organization | Aioe.org NNTP Server |
| Lines | 50 |
| Message-ID | <no0ucg$1sid$1@gioia.aioe.org> (permalink) |
| References | <21r7qb1srdvnieuufjquie2qrfujso6l7p@4ax.com> <slrnnq7t3d.456.avl@login.logic.tuwien.ac.at> |
| NNTP-Posting-Host | 7SK6yTFfqwlIPNBLl3l7Qg.user.gioia.aioe.org |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Complaints-To | abuse@aioe.org |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 |
| X-Notice | Filtered by postfilter v. 0.8.2 |
| Xref | csiph.com comp.lang.java.programmer:35198 |
Show key headers only | View raw
On 8/4/2016 10:05 PM, Andreas Leitgeb wrote:
> Roedy Green <inquiry@mindprod.com> wrote:
>> This looks like trivial task, but every time I work on it, it keeps
>> ballooning in complexity.
>>
>> I wish to deDup an ArrayList. Any two strings equalIgnoreCase are
>> considered dups. One should be removed. The order of the items should
>> be otherwise be undisturbed.
>>
>> Part of the trouble is removing an element from an ArrayList makes you
>> lose track of where you are.
>
> Copy to a LinkedHashMap (using the toUpperCase()'d string as key, and
> the original String as value), and finally copy back to a new ArrayList...
>
> The LinkedHashMap will take care of maintaining the ordering, except
> of course, where dups are left out.
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;
public class ListDeDup {
public static void main(String[] args) {
List<String> lst = new ArrayList<>();
lst.add("Aa");
lst.add("Bb");
lst.add("Cc");
lst.add("cC");
lst.add("bB");
lst.add("aA");
System.out.println(lst);
List<String> res = lst.stream().collect(Collectors.toMap(o ->
o.toLowerCase(), o -> o, (o1,o2) -> o1,
LinkedHashMap::new)).values().stream().collect(Collectors.toList());
System.out.println(res);
}
}
> Not sure if hashing on the uppercase version of a String is strictly
> equivalent to pairwise equalIgnoreCase, though. Maybe not, maybe close
> enough, maybe exactly same... The performance advantage (from n-squared
> to roughly n) might be worth a little slack in some corner cases.
There is always the "Turkish problem", but ...
Arne
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
DeDup an ArrayList Roedy Green <inquiry@mindprod.com> - 2016-08-04 18:35 -0700
Re: DeDup an ArrayList Andreas Leitgeb <avl@logic.at> - 2016-08-05 02:05 +0000
Re: DeDup an ArrayList Arne Vajhøj <arne@vajhoej.dk> - 2016-08-04 22:41 -0400
Re: DeDup an ArrayList Lew <lewbloch@gmail.com> - 2016-08-05 15:38 -0700
Re: DeDup an ArrayList Andreas Leitgeb <avl@logic.at> - 2016-08-06 12:10 +0000
Re: DeDup an ArrayList Roedy Green <inquiry@mindprod.com> - 2016-08-04 19:06 -0700
Re: DeDup an ArrayList Eric Douglas <e.d.programmer@gmail.com> - 2016-08-05 05:16 -0700
Re: DeDup an ArrayList Andreas Leitgeb <avl@logic.at> - 2016-08-05 15:30 +0000
Re: DeDup an ArrayList Eric Douglas <e.d.programmer@gmail.com> - 2016-08-05 10:10 -0700
Re: DeDup an ArrayList Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-05 13:51 -0400
Re: DeDup an ArrayList Eric Douglas <e.d.programmer@gmail.com> - 2016-08-05 12:16 -0700
Re: DeDup an ArrayList Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-08-05 15:38 -0400
Re: DeDup an ArrayList Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-08-06 00:47 +0200
Re: DeDup an ArrayList Arne Vajhøj <arne@vajhoej.dk> - 2016-08-05 09:02 -0400
Re: DeDup an ArrayList Jan Burse <janburse@fastmail.fm> - 2016-08-05 12:52 +0200
Re: DeDup an ArrayList Jan Burse <janburse@fastmail.fm> - 2016-08-05 12:53 +0200
Re: DeDup an ArrayList Jan Burse <janburse@fastmail.fm> - 2016-08-05 13:10 +0200
Re: DeDup an ArrayList Jan Burse <janburse@fastmail.fm> - 2016-08-13 12:26 +0200
Re: DeDup an ArrayList Daniel Pitts <usenet@coloraura.com> - 2016-08-05 08:37 -0700
Re: DeDup an ArrayList Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-08-05 08:58 -0700
Re: DeDup an ArrayList Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-08-05 09:47 -0700
Re: DeDup an ArrayList Knute Johnson <nospam@rabbitbrush.frazmtn.com> - 2016-08-05 19:56 -0700
Re: DeDup an ArrayList Daniel Pitts <usenet@coloraura.com> - 2016-08-06 09:18 -0700
Re: DeDup an ArrayList Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-08-06 01:04 +0200
Re: DeDup an ArrayList Roedy Green <inquiry@mindprod.com> - 2016-08-05 16:31 -0700
csiph-web