Skip to content
Advertisement

Java : list that contains unique elements in order

Is there a list type in java that stores objects in ascending order and not adds if this object is previously added. I know java maps can do that but I wonder if there is a list type that does what I want. Otherwise I have to override contains, equalsTo and add methods,right?

Advertisement

Answer

So you need a list containing only unique elements? Two options:

  • java.util.LinkedHashSet – preserves the order of insertion, has the set semantics
  • from commons-collections SetUniqueList – allows list operations like get(..) and set(..)
  • from commons-collections ListOrderedSet
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement