How to convert a Collection to List in Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

public class MyClass {
    public static void main(String args[]) {
        Collection<String> collection = new HashSet<>();
        
        collection.add("java");
        collection.add("c");
        collection.add("c++");
        collection.add("python");

        List<String> list = new ArrayList<>(collection);
        
        System.out.println(list);
    }
}
        
        
        
        
/*
run:

[c++, python, java, c]
        
*/

 



answered Oct 25, 2023 by avibootz
...