How to Convert HashSet to a List/ArrayList

In this post we will how convert a hashset to an arraylist . We will pass hashset as argument in constructor of ArrayList.

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

public class HashSetToList {

public static void main(String[] args) {

Set<String> hs = new HashSet<String>();
hs.add("java");
hs.add("php");
hs.add("html");
hs.add("javascript");
hs.add("mysql");

//print hashset
System.out.println("hashset = "+hs);

List<String> list = new ArrayList<String>(hs);

System.out.println("list from hashset = "+list);

}

}

Output:

hashset = [javascript, php, html, java, mysql]

list from hashset = [javascript, php, html, java, mysql]

Here we have learn java program to convert a hashset to an arraylist.

Read More:                                                                                                                       A Guide to HashSet
 A Guide To ArrayList
 Java Doc – HashSet

Leave a Reply

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

− 5 = 3