Saturday, July 3, 2021

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 like converting a List to a Set. It's 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 the Collection framework in Java, copying collections from one to another is extremely easy. Since every Collection class implements a Collection interface that defines the 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 actually a 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 another 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 the 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 a copy of the 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);
   
     
    }  
 
}

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


In this example, both collections i.e. List and Set will print the same values because there are no duplicates. The size of both List and Set will also be the 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 an ordered collection while Set is known as an 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 the 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)
That's all about in this example of converting a List to Set in Java. You can use this technique to convert any type of List like ArrayList, LinkedList, or Vector to convert Set like HashSet or TreeSet in Java. 

4 comments:

  1. I am genuinely delighted to glance at this web site posts which contains lots of
    helpful facts, thanks for providing these information.

    ReplyDelete
  2. Hey exceptional blog! Does running a blog similar to this require a large amount
    of work? I have virtually no knowledge of computer programming however I had been hoping to start my own blog in the near future.
    Anyways, should you have any ideas or techniques for new blog owners please share.
    I understand this is off topic nevertheless I simply had
    to ask. Appreciate it!

    ReplyDelete
    Replies
    1. Yes, it required a lot of hard work, you need to constantly learn and share things.

      Delete
  3. I was curious if you ever considered changing the page layout of your website?
    Its very well written; I love what youve got to say.
    But maybe you could a little more in the way of content so people could connect with it better.
    Youve got an awful lot of text for only having one or two images.
    Maybe you could space it out better?

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.