compare 2 ArrayList java

Viewed 88

I have the following arraylist

cd1 = [3,5,7,10,15,16]
cd2 = [4,10,5,8]

The output

[3,7,15,16]

as you can see one has 6 positions and the other 4.

then if they are repeated they should be stored in another ArrayList

Code I carry:

import java.util.ArrayList;

public class inexistentes {
    
     public static ArrayList<Integer> inexistentes(ArrayList<Integer> cd1, ArrayList<Integer> cd2){
        ArrayList<Integer> newList = new ArrayList<>();
        
        for(int i = 0; i < cd1.size(); i++){
            
            if(cd1.contains(i) ){
                newList.add(i);
            }
            
       }
         
         return newList;
     }
}

how can i buy those chains...

I already have a for but I don't know how to implement the if.

3 Answers

You do not need to apply complex logic. Your requirement can be met easily by using the List#removeAll function.

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> cd1 = List.of(3, 5, 7, 10, 15, 16);
        List<Integer> cd2 = List.of(4, 10, 5, 8);
        List<Integer> result = new ArrayList<Integer>(cd1);
        result.removeAll(cd2);
        System.out.println(result);
    }
}

Output:

[3, 7, 15, 16]

However, if you still want to do it your way, do it as follows:

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> cd1 = List.of(3, 5, 7, 10, 15, 16);
        List<Integer> cd2 = List.of(4, 10, 5, 8);
        System.out.println(inexistentes(cd1, cd2));
    }

    public static ArrayList<Integer> inexistentes(List<Integer> cd1, List<Integer> cd2) {
        ArrayList<Integer> newList = new ArrayList<>();

        for (int i = 0; i < cd1.size(); i++) {
            int n = cd1.get(i);
            if (!cd2.contains(n)) {
                newList.add(n);
            }
        }

        return newList;
    }
}

please try this loop

...

    for (int i : cd1) {

        if (!cd2.contains(i)) {

            newList.add(i);

        }

...

it returns [3, 7, 15, 16]

-- edit --

import java.util.ArrayList;

import java.util.Arrays;

public class inexistentes {

public static ArrayList<Integer> inexistentes(ArrayList<Integer> cd1, ArrayList<Integer> cd2) {

    ArrayList<Integer> newList = new ArrayList<>();

    for (int i : cd1) {

        if (!cd2.contains(i)) {

            newList.add(i);

        }

    }

    return newList;
}

public static void main(String[] args) {

    ArrayList cd1 = new ArrayList(Arrays.asList(3, 5, 7, 10, 15, 16));

    ArrayList cd2 = new ArrayList(Arrays.asList(4, 10, 5, 8));

    // [3,7,15,16]

    ArrayList cd3 = inexistentes(cd1, cd2);

    System.out.println(cd3.toString());

}

If you can use Set you can convert cd2 into a set and compare set with cd1. This would be quicker when compared to using contains() from list. At the same time Set uses more memory than List.

private static ArrayList<Integer> inexistentes(List<Integer> cd1, List<Integer> cd2) {
        ArrayList<Integer> newList = new ArrayList<>();
        Set<Integer> cd2Set = new HashSet<Integer>(cd2);
        for (int set1 : cd1) {

            if (!cd2Set.contains(set1)) {
                newList.add(set1);
            }

        }

        return newList;
    }
Related