Java Set to List

In this tutorial we will learn about java set to list conversion . In Order to convert set to list in Java , We have couple of method . We Knew that ArrayList In Java is a ordered collection and allow duplicate element . But TreeSet in Java is avoids duplicates. Let’s see how to convert set to list in java using different approaches .

Convert Set to List In Java

We will discuss different method for converting set to list in java . We can convert set to list in java using below method

1. Java Set to List Using ArrayList or LinkedList Constructor

In this example we will create a set of Strings and add some elements in it using add() method.Now we will initialize ArrayList Using Parameterized Constructor with Set elements .

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

public class SetToList {

	public static void main(String[] args) {
		//create HashSet
		Set<String> set = new TreeSet<String>();

		//add element into Set using add() method
		set.add("Java");
		set.add("Vogue");
		set.add("Welcomes");

		// print Set
		System.out.println("Set Elements : " + set);

		// create list from set
		List<String> list = new ArrayList<String>(set);

		// print element of list
		for (String element : list) {
			System.out.println("List Element is : " + element);
		}
	

	}

}

Output:

Set Elements : [Java, Vogue, Welcomes]
List Element is : Java
List Element is : Vogue
List Element is : Welcomes

2. Convert Set to List Using AddAll() Method

In this example we will convert Set to ArrayList using addAll() method of List . Firstly we created Set of Strings and then add some element into it using add() method . Now we will create list, then add Set elements to it using List’s addAll() method .

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

public class SetToList1 {

	public static void main(String[] args) {

		// create HashSet
		Set<String> set = new TreeSet<String>();

		// add element into Set using add() method
		set.add("Java");
		set.add("Vogue");
		set.add("Welcomes");

		// print Set
		System.out.println("Set Elements : " + set);

		// create list from set
		List<String> list = new ArrayList<String>();
		
		////adding set elements to the list using the addAll() method  
		list.addAll(set);

		// print element of list
		for (String element : list) {
			System.out.println("List Element is : " + element);
		}

	}

}

Output:

Set Elements : [Java, Vogue, Welcomes]
List Element is : Java
List Element is : Vogue
List Element is : Welcomes

3. Convert Set to List Using add() Method

In this approach,we will convert Set to List in java using for loop . We create Set and add elements into it. We also created one list. Now We Iterate Set element Using for-each loop and one by one element of Set will added to list using add() method . Let,s see java program for this approach.

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

public class SetToList2 {

	public static void main(String[] args) {
		// create HashSet
		Set<String> set = new TreeSet<String>();

		// add element into Set using add() method
		set.add("Java");
		set.add("Vogue");
		set.add("Welcomes");

		// print Set
		System.out.println("Set Elements : " + set);

		// create list from set
		List<String> list = new ArrayList<String>();
		
		for (String element : set) {
			//adds element to list
			list.add(element);
		}
		
		System.out.println("List Elements : "+ list);
		
	}

}

Output:

Set Elements : [Java, Vogue, Welcomes]
List Elements : [Java, Vogue, Welcomes]

In this tutorial we have learned java set to list conversion . We have can convert set to LinkedList in java using same above methods for convert set to arraylist in java . you can see more good java TreeSet program for practice .

Reference :

A Guide To TreeSet

Java Docs – TreeSet

Leave a Reply

Your email address will not be published. Required fields are marked *

− 2 = 1