How to get distinct values from two list in java

What you will learn here about java

  • How to get distinct values from two list in java

In java it is very easy to get distinct values from two list with the help of collection and stream API. So here we going to see how to get distinct values from two list in java.

How to get distinct values from two list in java

Below sample program to get distinct values from two list in java is given.

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class distinctValues {
    public static void main(String[] args) {
        List<String> list1=new ArrayList<>();
        list1.add("Apple");
        list1.add("Google");
        list1.add("Wipro");
        list1.add("TCS");
        list1.add("Reliance");
        List<String> list2=new ArrayList<>();
        list2.add("Apple");
        list2.add("Google");
        list2.add("HMT");

        list1.addAll(list2);
        list1=list1.stream().distinct().collect(Collectors.toList());

        System.out.println(list1);
    }
}

OUTPUT

[Apple, Google, Wipro, TCS, Reliance, HMT]

You may also like...