Path: csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: DeDup an ArrayList Date: Fri, 5 Aug 2016 08:58:15 -0700 Organization: A noiseless patient Spider Lines: 158 Message-ID: References: <21r7qb1srdvnieuufjquie2qrfujso6l7p@4ax.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 5 Aug 2016 15:58:15 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="fc10d1b501433caa198830294d826923"; logging-data="2308"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ffjIss+y8InIBGf+c6kOt" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 In-Reply-To: Cancel-Lock: sha1:UhQdQu4hfayXiuCfWyxzKXxJx2Q= Xref: csiph.com comp.lang.java.programmer:35208 On 8/5/2016 08:37, Daniel Pitts wrote: > On 8/4/16 6:35 PM, Roedy Green 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. >> > > Stream version: > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > import java.util.*; > import java.util.stream.Collectors; > > public class Dedup { > > public static void main(String[] args) { > final List list = new ArrayList<>(Arrays.asList( > "First", > "Second", > "first", > "Third", > "third", > "second", > "Last")); > > > final List result = > list.stream() > .collect( > Collectors.groupingBy( > String::toLowerCase, > LinkedHashMap>::new, > Collectors.toList() > ) > ) > .entrySet() > .stream() > .map(Map.Entry::getValue) > .map(l -> l.get(0)) > .collect(Collectors.toList()); > > result.forEach(System.out::println); > } > } > > > ************************************************************************************************ > > > In-place non-stream version: > > -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= > > > > import java.util.*; > > public class Dedup { > > public static void main(String[] args) { > final List list = new ArrayList<>(Arrays.asList( > "First", > "Second", > "first", > "Third", > "third", > "second", > "Last")); > > final Set exists = new HashSet<>(); > for (final Iterator i = list.iterator(); i.hasNext();) { > if (!exists.add(i.next().toLowerCase())) { > i.remove(); > } > } > > list.forEach(System.out::println); > } > } > > ************************************************************************************************ > I like your solutions Daniel. If I have some time I'm going to create a huge file with dups and try a speed test. My inelegant solutions below: import java.util.*; import java.util.stream.*; public class test { public static void main(String... args) { List list1 = Arrays.asList( "hello","world","wOrld","onE","two","one","three","ONE","TWO"); List list2 = new ArrayList<>(); for (String str1 : list1) { boolean flag = true; for (String str2 : list2) if (str1.equalsIgnoreCase(str2)) { flag = false; break; } if (flag) list2.add(str1); } for (String str : list2) System.out.println(str); } } import java.util.*; import java.util.stream.*; public class test1 { public static void main(String... args) { List list1 = Arrays.asList( "hello","world","wOrld","onE","two","one","three","ONE","TWO"); List list2 = new ArrayList<>(); List list3 = list1.stream(). filter(s -> check(s,list2)). collect(Collectors.toList()); list3.forEach(System.out::println); } static boolean check(String str,List list) { boolean flag = true; for (String str1 : list) { if (str.equalsIgnoreCase(str1)) { flag = false; break; } } if (flag) list.add(str); return flag; } } -- Knute Johnson