Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is there a class in java.util which...?

 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the order you want can be defined for Comparable or Comparator, then you could use a TreeSet.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or if you want to keep thing in the order they were originally inserted, you can use a LinkedHashSet.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic