Collections Section Index | Page 5
What is the default initial size for a Hashtable / HashMap?
This depends on what version of Java you are using. For JDK 1.2, the size was 101. For JDK 1.3, the size changed to 11.
Where can I find implementations of third-party collection libraries?
The Java Collections Clearinghouse maintains references to different implementations of the Collection Framework interfaces.
How can I implement a priority queue in Java? Has anyone created an open source version?
As referenced from the Java Collections Clearinghouse, you can find a priority queue implemented as part of MIT's FLEX compiler imfrastructure.
Source code is licensed under the GNU GPL.
There is ...more
How does ArrayList increase its capacity?
Unlike Vector where you can specify a capacity increment, ArrayList doesn't support this. Instead, ArrayList will increase capacity by about a half when it runs out of space. The refernece impleme...more
What is the minimum number of key-value pairs for which it makes sense to use a HashMap, as opposed to using a pair of arrays (one for keys, the other for values) with brute-force key searches?
What is the minimum number of key-value pairs for which it makes sense to use a HashMap, as opposed to using a pair of arrays (one for keys, the other for values) with brute-force key searches?Many...more
What is a weak reference and what are they used for?
Normally the Java garbage collector plays safe. It will only free up the memory used by an object when that object can no longer be accessed by the program. Once an object become impossible to rea...more
When I wrap a collection to be read-only or synchronized, why can't I call any of the collection methods via reflection without getting an IllegalAccessException?
When you wrap a collection through the static methods of the Collections class, this creates an instance of a package-private (default access) class. Because you don't have access to these classes...more
Since Properties extends Hashtable, can I use the Hashtable methods to add elements to a Properties list?
Technically speaking you can. However, you have to make sure you only add key-value pairs where both are strings. If you add something other than a String, the listing, loading, and saving methods...more
What is meant by compatible equals() and hashCode() methods?
In order for the Java Collections to work properly (and everything else in Java), the equals() and hashCode() methods must be compatible. Here, compatible means that if equals() reports that two i...more
Why can't I add a collection to itself?
This will cause a stack overflow exception to be generated on calls to methods like toString() and hashCode(), which recursively call the method on the elements of the collection.
How can you retrieve the Hashtable's load factor?
There is no public interface to access the load factor setting.
Some choices you can do to expose this value...
Subclass Hashtable and add a read-only accessor method
Use reflection to access the...more
When did Strings start caching their hash codes?
Starting with the 1.3 release of Java, the java.lang.String class will only calculate the hashcode once, when its first needed. Future calls to hashCode() will return the previously calculated value.more
How to sort the messages in JavaMail?
Within the JavaMail classes there is no support for this. However, once you get the array of messages back from a folder, you can call the Arrays.sort() method in the collections framework to sort...more
How do I make a collection where all the elements within it are a specific data type?
Until there is support for generic types in Java, you'll need to create a collection that overrides the methods that add elements into the collection. In these methods, you would check for the app...more
How do you control growth of vectors when their internal arrays are full?
The vector constructor can include either an initial capacity or a capacity and growth increment. When not specified, the initial size of the vector is 10 and growth will double when necessary. Ot...more