How to Convert a List to a Set in Java with Example

How to convert a List to Set in Java
Many times we need to convert one collection to another e.g. converting a List to a Set. It'an also an easy way to copy contents from one collection to other in Java like from List to Set or Set to List. Thanks to Collection framework in Java, copying collection from one to other is extremely easy. Since every Collection class implements Collection interface which defines addAll() method, which can be used to create a collection from contents of another collection. In this Java tutorial, we will see an example of how to copy contents from List to Set in Java as part of copying data from one collection to other in Java. This is an actually nice way to convert List to Set in Java. You can use this technique to convert any List implementation to Set in Java.

Java Program to convert List to Set

Java collection tuorial - copy object from List to Set in javaIn this example, we will copy objects from ArrayList to HashSet in Java. This example is just a demonstration by using this technique you can copy elements from one collection to other doesn't matter whether it's an ArrayList, LinkedList or HashSet in Java.

package example;

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

 * Java program example to convert a List to a Set in Java./
 * It also shows way to copy one collection to another
 * Collection in Java

 * You can copy data from List to Set using addAll()
 * method or you can create copy of collection
 * by passing another Collection like ArrayList
 * to its constructor while creating.
 * @author Javin Paul

public class ListtoSet{
    public static void main(String args[]) throws InterruptedException{            
     List<String> teams = Arrays.asList("India" , "Australia" , "England", "Pakistan");
     System.out.println("Original List: " + teams);
     // copying contents from List to Set in Java,
     // remember this will remove duplicates from
     // collection because List supports duplicates but

     //Set does not support duplicates in Java
     Set<String> cricketTeams = new HashSet<String>(teams);
     System.out.println("Copy collection :" + cricketTeams);

Original List: [India, Australia, England, Pakistan]
Copy collection :[Pakistan, England, Australia, India]

In this example, both collection i.e. List and Set will print same values because there are no duplicates. The size of both List and Set will also be same. The only order of both collections will be different because Set doesn't guarantee any ordering while List keeps elements in the order they are inserted into List in Java, that’s why List is also called as ordered collection while Set is known as a unordered collection in Java. Now you should not forget how to copy objects from one collection to another in Java, including List to Set or Set to List, just use addAll() or pass the collection to constructor while creating a copy.

Here is a nice summary of difference between List and Set collection types for your quick reference:

How to convert List to Set in Java

Other Java Collection Tutorials for your reference
  • How to convert an array to String in Java? (answer)
  • The difference between ArrayList and HashMap in Java? (answer)
  • How to convert a list to an array in Java? (answer)
  • The difference between HashSet and TreeSet in Java? (answer)
  • How to convert an ArrayList to a String in Java? (answer)
  • How to convert a Map to a List in Java? (answer)
  • The difference between ArrayList and HashSet in Java? (answer)

Further Learning
Java Fundamentals: Collections
From Collections to Streams in Java 8 Using Lambda Expressions
Grokking Algorithms by Aditya Bhargava
Java Programming Interview Exposed by Makham

No comments:

Post a Comment