2012년 11월 22일 목요일

How to sort ArrayList, removing duplicate items

 ArrayList arrayList1 = new ArrayList();
    
  
    
  //Create a HashSet which allows no duplicates
  HashSet hashSet = new HashSet(arrayList1);

  //Assign the HashSet to a new ArrayList
  ArrayList arrayList2 = new ArrayList(hashSet) ;
    
  //Ensure correct order, since HashSet doesn't
  Collections.sort(arrayList2);
    
  for (Object item : arrayList2)
    System.out.println(item);

댓글 없음:

댓글 쓰기