Lets see how to convert the ArrayList to Set in java.
Class name: ListToSet.java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListToSet{
public static void main(String[] args){
List<String> myList = new ArrayList<String>();
myList.add("one");
myList.add("two");
myList.add("three");
//Convert List to Set
Set<String> foo = new HashSet<String>(myList);
System.out.println("Size of new set is: " + foo.size());
}
}
OUTPUT:
Size of new set is: 3
Class name: ListToSet.java
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class ListToSet{
public static void main(String[] args){
List<String> myList = new ArrayList<String>();
myList.add("one");
myList.add("two");
myList.add("three");
//Convert List to Set
Set<String> foo = new HashSet<String>(myList);
System.out.println("Size of new set is: " + foo.size());
}
}
OUTPUT:
Size of new set is: 3