Collections Section Index | Page 4
How do I retrieve the values of a Hashtable/HashMap in sorted order?
Basically, you can't directly do this. What you can do is get the Collection of values() back from the map and create a sorted collection, or maintain two maps, one in each direction, and keep the...more
How do I convert an old-style Enumeration to something in the Collections Framework?
Prior to Java 1.4, any conversion had to be manually done. With the introduction of 1.4, you can call Collections.list(enumeration) to automatically convert the Enumeration to an ArrayList.more
How can I easily shift the elements in a List / Vector such that all the elements rotate n elements?
The Java 1.4 API adds a rotate() method to the Collections class:
rotate(List list, int distance) that will shift the elements for you.
What's the most optimum way of swapping two elements in a List?
The 1.4 version of Collections has a swap() method to do this for you. However, for earlier version of Java, you can swap two elements w/o an intermediate variable with:
list.set(index1, list.set(...more
What's the purpose of the IdentityHashMap?
The IdentityHashMap uses == for equality checking instead of equals(). This can be used for both performance reasons, if you know that two different elements will never be equals and for preventin...more
How can you get the hash code for an instance of a class if the class overrode hashCode()?
The System class method identityHashCode() allows you to get this information:
int code = System.identityHashCode(anObject);
How do you sort an ArrayList (or any list) of user-defined objects?
Create an implementation of the java.lang.Comparable interface that knows how to order your objects and pass it to
java.util.Collections.sort(List, Comparator).
See also: Sort algorithm on an A...more
How can I retrieve the items in my HashSet / HashMap in the order they were added?
Prior to Java 1.4, you had to manage a separate insertion order list yourself. Starting with Java 1.4, you can use the new LinkedHashMap / LinkedHashSet classes. The iterators you get back from th...more
Does the equals() method of an array do element-level checking?
If you have two arrays in memory with the same elements, and ask first.equals(second), this does not do an element-by-element comparison. Instead, it behaves just like Object's equals() method, es...more
How should I implement object comparisons in a flexible manner? For example, I have a Person class and sometimes I will compare based on name and sometimes I will compare based on age.
Instead of having the Person class implement the Comparable interface, you could delegate the comparing to another class. Perhaps you could have a PersonComparator interface that you could impleme...more
What are the differences between Vector and ArrayList? Which is best to use?
Vector and ArrayList are very similar. Both of them represent a 'growable array', where you access to the elements in it through an index.
ArrayList it's part of the Java Collection Framework, an...more
Where can I learn (more) about Java's support for developing multi-threaded programs?
Check out the jGuru Threads
FAQ.
What are the differences between HashMap and Hashtable?
What are the differences between
HashMap and Hashtable?
In a TreeMap, can I use a sorting algorithm other than the natural sorting for the keys?
You can pass a Comparator to the TreeMap constructor to use a sorting order other than the natural order.
How do you create a multi-dimensional List?
Since the elements of a List are objects, in order to make the List multi-dimensional, each object in the outer list needs to be a List, too. For instance, ...
List list = new ArrayList(10);
for ...more