Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #35208

Re: DeDup an ArrayList

From Knute Johnson <nospam@rabbitbrush.frazmtn.com>
Newsgroups comp.lang.java.programmer
Subject Re: DeDup an ArrayList
Date 2016-08-05 08:58 -0700
Organization A noiseless patient Spider
Message-ID <no2d2n$284$1@dont-email.me> (permalink)
References <21r7qb1srdvnieuufjquie2qrfujso6l7p@4ax.com> <no2bs0$u7g$1@dont-email.me>

Show all headers | View raw


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<String> list = new ArrayList<>(Arrays.asList(
>             "First",
>             "Second",
>             "first",
>             "Third",
>             "third",
>             "second",
>             "Last"));
>
>
>         final List<String> result =
>             list.stream()
>                 .collect(
>                     Collectors.groupingBy(
>                         String::toLowerCase,
>                         LinkedHashMap<String, List<String>>::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<String> list = new ArrayList<>(Arrays.asList(
>             "First",
>             "Second",
>             "first",
>             "Third",
>             "third",
>             "second",
>             "Last"));
>
>         final Set<String> exists = new HashSet<>();
>         for (final Iterator<String> 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<String> list1 = Arrays.asList(
          "hello","world","wOrld","onE","two","one","three","ONE","TWO");
         List<String> 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<String> list1 = Arrays.asList(
          "hello","world","wOrld","onE","two","one","three","ONE","TWO");
         List<String> list2 = new ArrayList<>();

         List<String> list3 = list1.stream().
          filter(s -> check(s,list2)).
          collect(Collectors.toList());

         list3.forEach(System.out::println);

     }

     static boolean check(String str,List<String> list) {
         boolean flag = true;
         for (String str1 : list) {
             if (str.equalsIgnoreCase(str1)) {
                 flag = false;
                 break;
             }
         }
         if (flag)
             list.add(str);
         return flag;
     }
}




-- 

Knute Johnson

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


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