0

I have a Class ArrayList so I have to delete duplicates Keywords when their Autor are same, but not when theses are different. The follow code remove duplicates OK only in the first Index (i=0), then it doesn't remove anything.

Thanks you!

Example:

Here I have an example :

1 A PPP

2 A EEE

3 B AAA

4 B LL

5 A CCC

2 A EEE

5 A CCC

In this cases I don't want to remove anyline because "A" has a different parent (2 and 5).

        int size = ls.size();
    int duplicates = 0;

    // not using a method in the check also speeds up the execution
    // also i must be less that size-1 so that j doesn't
    // throw IndexOutOfBoundsException
    for (int i = 0; i < size - 1; i++) {
        for (int j = i + 1; j < size; j++) {

                    if(ls.get(j).getKeywords().equals(ls.get(i).getKeywords()) && ls.get(j).getAutor().equals(ls.get(i).getAutor()) ){
                        duplicates++;
                        ls.remove(j);}


            // decrease j because the array got re-indexed
            j--;
            // decrease the size of the array
            size--;
        } // for j
    } // fo
backLF
  • 135
  • 2
  • 3
  • 12

2 Answers2

0

A best approch for this problem is create list of non duplicates. So declare another list and while iterating on first list check whether item is present in that list if not then only add that item. Below is the sample source code.

Note that I have deliberately used loop and equals to simulate your conditions.

public static void main(String[] args) throws InterruptedException {
    List<Integer> list = new ArrayList<Integer>();

    for (int i = 0; i < 3; i++) { //Add some duplicates
        list.add(new Integer(4));
        list.add(new Integer(5));
        list.add(new Integer(6));
    }
    List<Integer> newList = new ArrayList<Integer>();
    for (Integer first : list) {
        boolean contains = false;//if this flag is false after iteration then item will be added

        for (Integer copy : newList)
            if (first.equals(copy)) {// You will have to specify your condition here
                contains = true;
                break;
            }
        if(!contains)
            newList.add(first);//add item if it was not present
    }
    System.out.println(list);
    System.out.println(newList);

}

Output:

[4, 5, 6, 4, 5, 6, 4, 5, 6] <-- List with duplicates
[4, 5, 6] <-- List with no duplicates
Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72
  • This won't work if you don't know what elements will be there in the list. It will only work if the list will always hold the same values. – Chetan Kinger Oct 13 '12 at 17:26
  • @bot I don't know where the question of not working come from. It will always work. Iterations are such that it only depends on equals method if you add something else condition there it will work on that condition. – Amit Deshpande Oct 13 '12 at 17:30
  • You don't understand what I am saying. Your approach requires that you create a list containing the possible items that can appear in another list. This means that you need to know the items that will appear in the second list before hand. What do you do when you don't know what items will appear in the list? I could add a and b today and c and d tomorrow. Your program will require the user to provide a list containing no duplicates. You use this list to remove duplicates from another list. If the user has to input a list with no duplicates, then what's the program for? – Chetan Kinger Oct 13 '12 at 17:36
  • I am sorry. I guess you are right. I jumped to a conclusion before seeing your code. My bad – Chetan Kinger Oct 13 '12 at 17:43
  • Items that will appear in second list will depend on the condition to for elements to exist. Like in op case he can simply change equals with his condition and he will get the list based on that condition – Amit Deshpande Oct 13 '12 at 17:46
0

You can group you list by key and postprocess to remove dublicates:

Implementing GroupBy using Google guava Multimap and Function

EDIT:

Another interesting way is to implement equals method and using hashset (or like), see:

Remove duplicates from a list

Community
  • 1
  • 1
Anton Bessonov
  • 9,208
  • 3
  • 35
  • 38