| Author |
Is there a class in java.util which...?
|
Kevin Tysen
Ranch Hand
Joined: Oct 12, 2005
Posts: 255
|
|
In java.util, there is ArrayList, in which you can put more than one reference to the same object, and you can use ArrayList to keep the elements in the order you want to. Also, there is HashSet, in which you cannot put more than one reference to the same object, but HashSet does not keep track of the order of the elements the way the user wants to order them. Is there a class in java which keeps track of the order of elements (the order that the user wants) and permits only one reference to the same object? Or do I have to use ArrayList like this: public void addToArrayList(ArrayList myList, Object myObject){ if (!myList.contains(myObject)){ myList.add(myObject); } } ?
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
|
If the order you want can be defined for Comparable or Comparator, then you could use a TreeSet.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
Jim Yingst
Wanderer
Sheriff
Joined: Jan 30, 2000
Posts: 18670
|
|
|
Or if you want to keep thing in the order they were originally inserted, you can use a LinkedHashSet.
|
"I'm not back." - Bill Harding, Twister
|
 |
 |
|
|
subject: Is there a class in java.util which...?
|
|
|