A set is simply a collection that can contain no duplicates so it sounds perfect for you.
It is also very simple to implement. For example:
Set<String> mySet = new HashSet<String>();
This would provide you a set that can hold Objects of type String.
To add to the set is just as simple:
mySet.add("My first entry!");
By definition of a set, you can add whatever you want and never run into a duplicate.
Have fun!
EDIT : If you decide you are dead-set on using an ArrayList, it is simple to see if an object is already in the list before adding it. For example:
public void addToList(String newEntry){
if(!myList.contains(newEntry))
myList.add(newEntry);
}
Note: All my examples assume you are using String objects but they can easily be swapped to any other Object type.
HashSet
, just like you've read. – LeadershipArrayList
and aHashSet
with the same elements. You can use anArrayList
to keep the elements in the order you want, and aHashSet
to check whether an element is already present. (The check will be quicker than searching theArrayList
, but maintaining two collections will slow things down.) Sometimes this is the right way to do things. – Leek