Headertab

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, 27 April 2016

How to convert Arraylist to Set in java


package samset;

import java.util.ArrayList;
import java.util.Hashset;

public class ConvertArrayList{

public static void main(String ar[])
{
// Create a ArrayList and add some data
ArrayList<String> myList=new ArrayList<String>();
myList.add("One");
myList.add("Nine");
myList.add("Three");
myList.add("Four");
myList.add("One");
myList.add("Six");
myList.add("Seven");
myList.add("Two");
myList.add("Nine");
myList.add("Ten");

System.out.println("Before using hashset data"+myList.size());
System.out.println(myList);

//Create a HashSet and add above ArrayList
HashSet myset=new HashSet(myList);

System.out.println("After using hashset data"+myset.size());
System.out.println(myset);

}

}

Output :-

Before using hashset data - 10
["One","Nine","Three","Four","One","Six","Seven","Two","Nine","Ten"]
After using hashset data -  8
["One","Two","Three","Four","Six","Seven","Nine","Ten"]



Enjoy......