Collections Section Index | Page 4
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
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.
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
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
The System class method identityHashCode() allows you to get this information: int code = System.identityHashCode(anObject);
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
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
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
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
Check out the jGuru Threads FAQ.
What are the differences between HashMap and Hashtable?
You can pass a Comparator to the TreeMap constructor to use a sorting order other than the natural order.
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