This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is there a class in java.util which...? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Is there a class in java.util which...?" Watch "Is there a class in java.util which...?" New topic
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
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Is there a class in java.util which...?
 
Similar Threads
Efficient Implmentaion of List and Map
ArrayList class
Array VS. Vector, which is better?
2-d arrays
ArrayList without duplicates