Collections Section Index | Page 3
How can I convert a Collection to an Array then back to a Collection?
The Collection interface provides a mechanism to turn a Collection into an Array using the methods <T> T[] toArray(T[] a) or Object[] toArray(). The first method will return a Array contain...more
What happens if two threads perform a get of one hashmap at the same time?
[I know a HashMap object is not synchronized if not done explicitly, so in a case where you have to write and read data in a hashmap from different threads you need to synchronize it (or use anoth...more
How do I save properties settings with the Properties class?
Try this:
Properties prop = new Properties();
FileOutputStream output = new FileOutputStream("Test.properties");
prop.store(output,"my testproperties");
output.flush();
output.close();
You'll ne...more
How do I load property settings with the Properties class?
java.util.Properties objects can load values from a file using the method load(InputStream).
Here is the code you need:
Properties props = new Properties();
props.load(new FileInputStream("prope...more
Where can I find an example of the Java 1.4 LinkedHashSet and LinkedHashMap classes?
There is an article at IBM's developerworks: Magic with Merlin: Maintaining insertion order that demonstrates their usage.more
Is Vector's clone method thread-safe?
Sure it is, since it is a Vector which is thread-safe.
How can I add an array of objects to a collection?
First you need to convert the array to a Collection. This can be done with Arrays.asList(objectArray). Once you have the array as a List, you can add it to another Collection with theCollection.ad...more
How can I go through an Iterator mulitple times?
There is no direct support for this. You'll need to create your own caching mechanism. For instance, as you go through the Iterator the first time, add the elements to a LinkedList. Then, you can ...more
What's new to the Collections Framework in Java 1.4?
There are three new implementations:
LinkedHashSet
LinkedHashMap
IdentityHashMap
One marker interface:
RandomAccess
And six new utility methods for the Collections class:
rotate(List list, ...more
How do I traverse a map backwards?
Just keep getting the last key and the head map before it:
if (!map.isEmpty()) {
Object last = map.lastKey();
boolean first = true;
do {
if (!first) {
System.out.print(", ");
...more
How do I traverse a sorted set backwards?
Just keep getting the last element and the head set before it:
if (!set.isEmpty()) {
Object last = set.last();
boolean first = true;
do {
if (!first) {
System.out.print(", ");
...more
How can I use two iterators to go through a collection?
Just get a separate iterator for each loop:
Collection l = ...;
for(Iterator i = l.iterator(); ...) {
for(Iterator j = l.iterator();...) {
}
}
more
When will Java have support for type-safe collections?
It looks like JSR 14 will make it into the 1.5 releae of Java some time around 2003.
Where can I find performance comparisons between the different collection implementations?
An interresting benchmark about Java collections has been done by Bruce Eckel's in his (online) book Thinking in Java. You'll find them in Chapter 9.more
How can I add a Collection to another Collection?
The java.util.Collection interface includes an addAll(Collection c) method to add one collection to another.