Here is a very simple and efficient way to retrieve unique items from a
All you need to do is put all the items in the
java.util.List into a
java.util.Set and then create a new
Set. Since a
Set does not allow duplicate entries, the new
List created would only have the unique items.
Below is an example:
//the initial List
ArrayList list = new ArrayList();
list.add("ID_2); //duplicate entry - needs to be removed
//convert the List into a Set
Set set = new HashSet(list);
//create a new List from the Set
ArrayList uniqueList = new ArrayList(set);
Note that the order of the items in the unique list will not be the same as those in the original.
To maintain the order of the initial items use a
java.util.LinkedHashSet instead of a
To sort the items in the
java.util.List use a
For further details, have a look here.