Example

List list = new ArrayList();
...

if (!list.contains(someObject)) {
...
list.add(someObject);
...
}

Solution
In the given example, the List "contains" method will perform a linear search, which can be expensive if the list is large. If changed to a set there will be no need to call contains since Set guarantees uniqueness. Suggested change:


Set set = new HashSet();
...
set.add(someObject);
...


If order of insertion is important use LinkHashSet instead of HashSet. Suggested change:


Set set = new LinkedHashSet();
...
set.add(someObject);
...