4 different Java programs to convert a list to set

Java program to convert a list to set:

This program will show you how to convert a list to set in Java. I will explain different ways with examples.

Method 1: Iterative approach to convert a list to set:

This is the simplest way to do the conversion. We will iterate over the list elements and add these to a set. The program will create an empty set at the start of the program.

import java.util.*;

public class Main {

    public static <E> Set<E> listToSet(List<E> list) {
        Set<E> resultSet = new HashSet<>();

        for (E e : list)
            resultSet.add(e);

        return resultSet;
    }

    public static void main(String[] args) {
        List<Integer> givenList = Arrays.asList(1, 2, 3, 4, 5, 6, 5, 3, 2);
        Set<Integer> resultSet = listToSet(givenList);

        System.out.println(resultSet);
    }
}

In this program,

  • The listToSet method is used to convert a list to set. It takes one list as its parameter, converts it to a set and returns that.
    • It creates a HashSet, resultSet to hold the results.
    • The for loop iterates over the elements of the list and add these to the resultSet.
  • Since this is a generic method, we can use it with lists of different data types.
  • The last line is printing the final set.

It will print the below result:

[1, 2, 3, 4, 5, 6]

This is the set conversion of the list givenList. It removes the duplicates.

Method 2: By using addAll:

The addAll method adds all the elements of a collection to a set. It takes only one parameter, the collection to add.

boolean addAll(Collection c)

Here, c is the collection to add to the set.

It returns one boolean value. It returns true if the set is changed due to this operation.

Exceptions:

It might throw these exceptions:

  • UnsupportedOperationException: If the set doesn’t support this operation.
  • ClassCastException: If the class of an element of the collection is different.
  • NullPointerException: If the collection is null, if it contains null elements or if the set can’t include null elements.
  • IllegalArgumentException: If it fails to add any element of the collection to the set.

Let’s change the above example to use addAll:

import java.util.*;

public class Main {

    public static <E> Set<E> listToSet(List<E> list) {
        Set<E> resultSet = new HashSet<>();

        resultSet.addAll(list);

        return resultSet;
    }

    public static void main(String[] args) {
        List<Character> givenList = Arrays.asList('a', 'e', 'i', 'o', 'u', 'e', 'i');
        Set<Character> resultSet = listToSet(givenList);

        System.out.println(resultSet);
    }
}

It will print similar result.

[a, e, u, i, o]

Method 3: With stream:

The stream() and collect() methods can be used to convert a list to a stream and convert it back to a set.

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

public class Main {

    public static <E> Set<E> listToSet(List<E> list) {
        return list.stream().collect(Collectors.toSet());
    }

    public static void main(String[] args) {
        List<Character> givenList = Arrays.asList('a', 'e', 'i', 'o', 'u', 'e', 'i');
        Set<Character> resultSet = listToSet(givenList);

        System.out.println(resultSet);
    }
}

Output: Java convert list to set

Method 4: By using the constructor call:

We can use the HashSet constructor and pass the list to the constructor to convert it to a set.

import java.util.*;

public class Main {

    public static <E> Set<E> listToSet(List<E> list) {
        return new HashSet<>(list);
    }

    public static void main(String[] args) {
        List<Character> givenList = Arrays.asList('a', 'e', 'i', 'o', 'u', 'e', 'i');
        Set<Character> resultSet = listToSet(givenList);

        System.out.println(resultSet);
    }
}

It is simpler than the previous three examples.

You might also like: