TechieClues TechieClues
Updated date May 21, 2024
This blog explores four different methods, including HashSet, LinkedHashSet, TreeSet, and Java 8 streams with Collectors.

Method 1: Using HashSet

The first method involves using the HashSet class, which is one of the implementations of the Set interface. It stores elements in a hash table and guarantees no duplicate elements. 

import java.util.*;

public class ListToSetConversion {
    public static void main(String[] args) {
        // Create a List with duplicate elements
        List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 5, 3, 6, 7, 1);

        // Convert List to Set using HashSet
        Set<Integer> set = new HashSet<>(list);

        // Display the Set elements
        System.out.println("Method 1 - Using HashSet:");
        for (Integer element : set) {
            System.out.print(element + " ");
        }
    }
}

Output:

Method 1 - Using HashSet:
1 2 3 4 5 6 7

Method 2: Using LinkedHashSet

The second method used the LinkedHashSet class, another implementation of the Set interface. LinkedHashSet maintains the insertion order of elements, which means the elements will be stored in the order they were inserted. 

import java.util.*;

public class ListToSetConversion {
    public static void main(String[] args) {
        // Create a List with duplicate elements
        List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 5, 3, 6, 7, 1);

        // Convert List to Set using LinkedHashSet
        Set<Integer> set = new LinkedHashSet<>(list);

        // Display the Set elements
        System.out.println("Method 2 - Using LinkedHashSet:");
        for (Integer element : set) {
            System.out.print(element + " ");
        }
    }
}

Output:

Method 2 - Using LinkedHashSet:
1 2 3 4 5 6 7

Method 3: Using TreeSet

In this method, we are using the TreeSet class, which is a NavigableSet implementation that stores elements in a sorted order. The elements in a TreeSet are arranged in their natural ordering or according to a specified comparator. 

import java.util.*;

public class ListToSetConversion {
    public static void main(String[] args) {
        // Create a List with duplicate elements
        List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 5, 3, 6, 7, 1);

        // Convert List to Set using TreeSet
        Set<Integer> set = new TreeSet<>(list);

        // Display the Set elements
        System.out.println("Method 3 - Using TreeSet:");
        for (Integer element : set) {
            System.out.print(element + " ");
        }
    }
}

Output:

Method 3 - Using TreeSet:
1 2 3 4 5 6 7

Method 4: Using Stream and Collectors

This method uses the Java 8 streams and the Collectors class to convert a List to a Set. This approach allows for a more concise and functional programming style. 

import java.util.*;
import java.util.stream.Collectors;

public class ListToSetConversion {
    public static void main(String[] args) {
        // Create a List with duplicate elements
        List<Integer> list = Arrays.asList(1, 2, 3, 2, 4, 5, 3, 6, 7, 1);

        // Convert List to Set using Stream and Collectors
        Set<Integer> set = list.stream()
                .collect(Collectors.toSet());

        // Display the Set elements
        System.out.println("Method 4 - Using Stream and Collectors:");
        for (Integer element : set) {
            System.out.print(element + " ");
        }
    }
}

Output:

Method 4 - Using Stream and Collectors:
1 2 3 4 5 6 7

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!