Monday, December 28, 2009

Method to remove duplicate values in Array lists

/**
* Method to remove duplicate values in Array lists
* @param arrayList- The list from which the duplicates needs to be removed
* @return ArrayList
* @author karthikeyan_a
* @created 11-May-2009
*/
public ArrayList removeDuplicates(ArrayList arrayList) {

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

//Assign the HashSet to a new ArrayList
ArrayList resultArrayList = new ArrayList(hashSet) ;

//Ensure correct order, since HashSet doesn't
Collections.sort(resultArrayList);

return resultArrayList;
}

No comments:

Post a Comment