Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.

aviboots(AT)netvision.net.il

Instant Grammar Checker - Correct all grammar errors and enhance your writing
BlueHost Web Hosting

Bonsai Boy of New York Bonsai Trees and Accessories

Website Domains Names & Hosting | Domain.com

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Liquid Web Cloud VPS Hosting

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

13,285 questions

18,078 answers

573 users

How to convert a Set to List in Java

3 Answers

0 votes
import java.util.*; 

public class MyClass {
    public static <T> List<T> convertSetToList(Set<T> set) { 
        List<T> list = new ArrayList<>(); 
  
        for (T t : set) 
            list.add(t); 

        return list; 
    } 
    public static void main(String args[]) {
        Set<String> hset = new HashSet<String>(); 
 
        hset.add("java"); 
        hset.add("c++"); 
        hset.add("php"); 
        hset.add("nodejs"); 
        hset.add("python"); 

		List<String> list = convertSetToList(hset); 

		System.out.println(list);
    }
}
 
 
 
/*
run:
 
[c++, python, java, php, nodejs]
 
*/

 




answered May 18 by avibootz
0 votes
import java.util.*; 
import java.util.stream.Collectors;

public class MyClass {
    public static <T> List<T> convertSetToList(Set<T> set) { 
        return set.stream().collect(Collectors.toList());
    } 
    public static void main(String args[]) {
        Set<String> hset = new HashSet<String>(); 
 
        hset.add("java"); 
        hset.add("c++"); 
        hset.add("php"); 
        hset.add("nodejs"); 
        hset.add("python"); 

		List<String> list = convertSetToList(hset); 

		System.out.println(list);
    }
}
 
 
 
/*
run:
 
[c++, python, java, php, nodejs]
 
*/

 




answered May 18 by avibootz
0 votes
import java.util.*; 

public class MyClass {
    public static <T> List<T> convertSetToList(Set<T> set) { 
        return new ArrayList<>(set); 
    } 
    public static void main(String args[]) {
        Set<String> hset = new HashSet<String>(); 
 
        hset.add("java"); 
        hset.add("c++"); 
        hset.add("php"); 
        hset.add("nodejs"); 
        hset.add("python"); 

		List<String> list = convertSetToList(hset); 

		System.out.println(list);
    }
}
 
 
 
/*
run:
 
[c++, python, java, php, nodejs]
 
*/

 




answered May 18 by avibootz
...