Find Unique Elements In List Java

Chapter: Miscellaneous Last Updated: 07-10-2023 07:15:01 UTC

Program:

            /* ............... START ............... */
                
// Using a Set:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class UniqueElementsInList {
    public static void main(String[] args) {
        List<Integer> myList = new ArrayList<>();
        myList.add(1);
        myList.add(2);
        myList.add(2);
        myList.add(3);
        myList.add(4);
        myList.add(4);
        
        Set<Integer> uniqueSet = new HashSet<>(myList);
        
        System.out.println("Unique elements in the list: " + uniqueSet);
    }
}


// Using a LinkedHashSet (Preserving Order):

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class UniqueElementsInList {
    public static void main(String[] args) {
        List<Integer> myList = new ArrayList<>();
        myList.add(1);
        myList.add(2);
        myList.add(2);
        myList.add(3);
        myList.add(4);
        myList.add(4);
        
        Set<Integer> uniqueSet = new LinkedHashSet<>(myList);
        
        System.out.println("Unique elements in the list (preserving order): " + uniqueSet);
    }
}

// Using Java Streams (Java 8 and later):

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

public class UniqueElementsInList {
    public static void main(String[] args) {
        List<Integer> myList = new ArrayList<>();
        myList.add(1);
        myList.add(2);
        myList.add(2);
        myList.add(3);
        myList.add(4);
        myList.add(4);
        
        List<Integer> uniqueList = myList.stream().distinct().collect(Collectors.toList());
        
        System.out.println("Unique elements in the list: " + uniqueList);
    }
}


                /* ............... END ............... */
        

Notes:

  • In first program You can use a Set data structure to store the unique elements from the list because a Set does not allow duplicate values.
  • In second program if you want to maintain the order of elements while removing duplicates, you can use a LinkedHashSet, which is an ordered version of the Set.
  • In last program we have used Java streams to filter out the unique elements from a list:
  • Any of these methods will give you a list or set containing only the unique elements from the original list, depending on your preference for preserving order or not.

Tags

Find unique elements in list java #How to Get Unique Values from ArrayList using Java 8? #Get unique values from ArrayList in Java

Similar Programs Chapter Last Updated
Java Program To Implement A Custom equals() and hashcode() In Java Miscellaneous 07-10-2023
Java Program To Find The Intersection Of Two HashSets Miscellaneous 07-10-2023
Java Program To Remove Duplicate Elements From List Miscellaneous 07-10-2023
Java program to parse a date and time string from a log file and store it in a database Miscellaneous 19-09-2023
Java Program To Print All The Dates In A Month That Fall On A Weekend Miscellaneous 19-09-2023
Java Program To Find Number Of Working Days In A Month Miscellaneous 19-09-2023
Java Program To Calculate Age From Year Of Birth Miscellaneous 16-09-2023
How To Check If Two Strings Are Anagrams In Java Miscellaneous 22-08-2023
Java Program To Make A Snake Game Miscellaneous 15-08-2023
Java Program To Find Repeated Characters Of String Miscellaneous 15-08-2023
String To Array In Java Miscellaneous 11-08-2023
Java Program To Convert Date To String Miscellaneous 11-08-2023
Java Program To Convert String To Date Object Miscellaneous 11-08-2023
Java Program To Find Number Of Days In A Month Miscellaneous 11-08-2023
Java Program To Print First And Last Day Of Month Miscellaneous 11-08-2023
Java Program To Find Leap Year Between Two Dates Miscellaneous 11-08-2023
Java Code To Find Difference Between Two Dates In Years Months And Days Miscellaneous 11-08-2023
Java program to calculate age from year of birth Miscellaneous 29-06-2023
Swap Two Numbers Without Using Third Variable In Java Miscellaneous 02-06-2023
Java Program To Find The Average Of An Array Of Numbers Miscellaneous 02-06-2023
How Do You Find The Factorial Of A Number In Java Miscellaneous 02-06-2023
Java Program That Takes Two Numbers As Input And Prints Their Sum Miscellaneous 27-05-2023
How To Get The Length Of An Array In Java Miscellaneous 27-05-2023
Java Add Element To List Example Miscellaneous 19-05-2023
Java Program To Square All Items In List Miscellaneous 17-05-2023
Java Program To Merge Two Lists Miscellaneous 17-05-2023
How To Reverse A List In Java Miscellaneous 17-05-2023
Java Program To Find Unique Elements In An Array Miscellaneous 14-05-2023
Java Program To List All Elements In List Miscellaneous 30-04-2023
Java Program To Create XML File Miscellaneous 23-04-2023

1 2