Collections Section Index | Page 7
In order to get all the keys for a Hashtable, you use the keys() method to get an Enumeration or the keySet() method to get a Set. If you are using Java 2, and can use the collections framework, w...more
The Collections class has six methods to help out here: unmodifiableCollection(Collection c) unmodifiableList(List list) unmodifiableMap(Map m) unmodifiableSet(Set s) unmodifiableSortedMap(Sorted...more
To go through all the elements of a HashMap, or any class that implements the Map interface, call the entrySet() or keySet() methods than loop through what is returned. The entrySet() returns a gr...more
The Hashtable class uses an internal (private) class named Entry to hold the key-value pairs. All entries of the Hashtable are stored in an array of Entry objects with the hash value of the key se...more
If speed is of the essence, do not use a Vector. Instead, use an ArrayList. All accesses to a Vector are synchronized, whether you need it or not. If you know you arent accessing the structu...more
Is there a way to create a homogenous collection in Java? How do I make a collection where all the elements within it are a specific data type?
You can wait for Generics to be added to Java or... You'd have to build one yourself. Basically, you're creating a class that is a Proxy (Gang Of Four design pattern) around the actual Collectio...more
What collection works best for maintaining a family tree, where each node can have multiple parents and multiple children, spouse relationships, etc.?
With Collections by themselves, there isn't one. What you're asking for is a Labeled Directed Graph (A directed graph where the graph edges have labels describing the relationship). A rather com...more
How can I save/load application settings that I would normally use .ini files or the Windows Registry?
You can use Java property files for storing settings for an application. Use the java.util.Properties class, which includes a load and store method. You can also use a PropertyResourceBundle, but ...more
You can't. Each of the Map and List interfaces define a remove(Object o) method. Each method returns a different type (Map returns an Object while List returns a boolean). Because the compiler doe...more
The Arrays class in java.util provides a series of sort() methods for sorting arrays. If the array is an array of primitives or an array of a class that implements Comparable then you can just cal...more
How do I get the list of system properties that tell me things like which version of Java a user is running and their platform-specific line separator?
The System.getProperties() method will return the standard property set. However, in untrusted applets, you can only ask for specific properties, as in System.getProperty("java.version").more
You cannot. It is part of the security architecture of the Java runtime to ensure never accessing invalid memory space.
To avoid getting an ArrayIndexOutOfBoundsException, you can check for the array length from either the length instance variable or using reflection and calling java.lang.reflect.Array.getLength(),...more
If you have an array of primitives or an array of equivalent objects that implement the Comparable interface, all you need to do is call the sort() method of the java.util.Arrays class. If the cla...more
With the Collections Framework, the new implementations are all unsynchronized by default. If you need synchronized access, you must synchronize things yourself. The Collections class offers a wra...more