How to convert java.util.List to java.util.Set

Leave a Comment

Friday, May 11, 2012


Removing duplicate items from a list is pretty simple in java just convert the list to java.util.Set it will automatically remove the duplicate items and create a set for you.

Syntax
Set<E> alphaSet  = new HashSet<E>(<your List>);

Example
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> alphaList = new ArrayList<String>();
  alphaList.add("A");
  alphaList.add("B");
  alphaList.add("C");
  alphaList.add("A");
  alphaList.add("B");
  System.out.println("List values .....");
  for (String alpha : alphaList)
  {
   System.out.println(alpha);
  }
  Set<String> alphaSet = new HashSet<String>(alphaList);
  System.out.println("\nSet values .....");
  for (String alpha : alphaSet)
  {
   System.out.println(alpha);
  }
 }
}

No comments :

Post a Comment