Comparing Lists in Java?

Asked

Viewed 54 times

-1

How to compare two Lists and add different record in a third list??

Ex: List 1(1,2,3,4) List 2(1,2,3) List 3.add(4);

  • It depends on the result you want. Which is?

  • 4

    It has to be list or it can be set (Set)? In the set the elements do not repeat, in the list yes.

1 answer

3


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

public class Teste {
    public static void main(String[] args) {
        List<Integer> lista1 = Arrays.asList(1,2,3,4);
        List<Integer> lista2 = Arrays.asList(1,2,3);

        //clona lista 1
        List<Integer> lista3 = new ArrayList<Integer>(lista1);

        // remove todos os intems da lista 2
        lista3.removeAll(lista2);

        System.out.println(lista3);

    }
}
  • Vlw For the help of Marciano.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.