Question

I'm looking for a nice way to remove duplicates from a list.

List<String[]> rowList = new ArrayList();
    rowList.add(new String[]{"1","a", "abc"});
    rowList.add(new String[]{"2","b", "def"});
    rowList.add(new String[]{"3","c", "ghi"});
    rowList.add(new String[]{"4","a", "jkl"});
    rowList.add(new String[]{"5","d", "mno"});
    rowList.add(new String[]{"6","e", "pqr"});
    rowList.add(new String[]{"7","b", "stu"});

From this rwoList, i only want entries: 1,2,3,5 and 6. This means i have only one column of intrest, in this case column 2 (a, b, c,..) This is only an easy example i have to handle hugh amount of tables which have 300 columns and min 300000 rows. Another important point is, that i don't won't loose the orientation within the list.

Note: I receive the data from a csv file.

Was it helpful?

Solution

  List<String[]> rowList = new ArrayList<String[]>();
  rowList.add(new String[]{"1","a", "abc"});
  rowList.add(new String[]{"2","b", "def"});
  rowList.add(new String[]{"3","c", "ghi"});
  rowList.add(new String[]{"4","a", "jkl"});
  rowList.add(new String[]{"5","d", "mno"});
  rowList.add(new String[]{"6","e", "pqr"});
  rowList.add(new String[]{"7","b", "stu"});

  Set<String[]> s = new TreeSet<String[]>(new Comparator<String[]>() {
         @Override
         public int compare(String[] o1, String[] o2) {
                return o1[1].compareTo(o2[1]);
        }
   });

Removing the duplicates by adding to set "s"

   s.addAll(rowList);
   List<Object> res = Arrays.asList(s.toArray());

Printing your result

   for (Object object : res) {
         String[] array = (String[])object;
         System.out.println(array[0]+" "+ array[1] +", "+array[2]);
    }

Output

1 a, abc
2 b, def
3 c, ghi
5 d, mno
6 e, pqr

OTHER TIPS

Make a custom method isContain(List<String[]> rowList, String string)

private static boolean isContain(List<String[]> rowList, String secStr) {
    for (String[] strings : rowList) {
       if(strings[1].equals(secStr)){
           return true;
       }   
    }
    return false;
}

Check this method before add item to List to remove duplicate item, like:

 List<String[]> rowList = new ArrayList();
    String[] sts= new String[]{"1", "a", "abc"};
    boolean contain= isContain(rowList,sts[1]);
    if(!contain){
        rowList.add(sts);
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top