The complete source code is given below:
import java.util.ArrayList; import java.util.HashSet; public class ConvertFromArrayListToSetExample { public static void main(String[] args) { // create an ArrayList object ArrayList arrayList = new ArrayList(); // Add elements to Arraylist arrayList.add("1"); arrayList.add("2"); arrayList.add("3"); arrayList.add("2"); arrayList.add("1"); System.out.println("Arraylist contains : "); for (Object e : arrayList) { System.out.println(e); } /** * Constructs a new set containing the elements in the specified * collection. **/ HashSet numberSet = new HashSet(arrayList); System.out.println("HashSet contains : "); for (Object e : numberSet) { System.out.println(e); } } }
The output is:
Arraylist contains :
1
2
3
2
1
HashSet contains :
3
2
1
No comments:
Post a Comment