|
Commons Collections example source code file (pick.xml)
The Commons Collections pick.xml source code<?xml version="1.0" encoding="ISO-8859-1"?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <document> <properties> <title>Choosing a collection <author email="dev@commons.apache.org">Commons Documentation Team </properties> <body> <section name="Choosing a collection"> <p> Commons-Collections and the Java Collections Framework provide a wide variety of collections to choose from. This choice can be bewildering, so this document seeks to help you choose. </p> <script> <![CDATA[ function showHide(showObj,hideObj) { if (document.getElementById) { // DOM3 = IE5, NS6 document.getElementById(showObj).style.display='block'; document.getElementById(hideObj).style.display='none'; } else { if (document.layers) { // Netscape 4 document.layers[showObj].display='block'; document.layers[hideObj].display='none'; } else { // IE 4 eval("document.all."+showObj+".style.display='block'"); eval("document.all."+hideObj+".style.display='none'"); } } } function showMapMain() { showHide('mapmain','collectionmain'); } function showMap() { showHide('map','bidimap'); } function showBidiMap() { showHide('bidimap','map'); } function showCollectionMain() { showHide('collectionmain','mapmain'); } ]]> </script> <!-- ========================================================================= --> <h3>Collection or Map <!-- ========================================================================= --> <p> Do you want to store a map of key-value pairs? Then use a <a href="#" onclick="showMapMain()">Map. For example you could decode country codes, where 'FR' maps to 'France' and 'GB' maps to 'United Kingdom'.<br /> Or do you want to store a simple collection of elements? Then use a <a href="#" onclick="showCollectionMain()">Collection. For example you could store types of tree, 'Oak', 'Pine' and 'Birch'.<br /> </p> <!-- ========================================================================= --> <div id="mapmain" style="display:none"> <h3>Maps <p> Do you want to be able to access the key from the value as well as the value from the key? Then use a <a href="#" onclick="showBidiMap()">BidiMap. For example you could convert country codes to text <i>and text to country codes, allowing you to convert 'FR' to 'France' and 'France' to 'FR'. |
<td>Uses two HashMap objects internally. This implementation is a useful general purpose BidiMap that can store any object. The objects are stored in any order, and the order may change over time.
</tr>
<tr>
<td>DualTreeBidiMap
<td>Uses two TreeMap objects internally which means that keys and values must implement Comparable, unless a comparator is supplied. If considering this class, the TreeBidiMap is usually a better choice. However, this is the only implementation of the SortedBidiMap interface.
</tr>
<tr>
<td>TreeBidiMap
<td>Dedicated implementation that requires keys and values to implement Comparable, unless a comparator is supplied. This is an efficient implementation using a data structure that avoids duplicating data. The keys and values are held and viewed in sorted order, and the class implements the OrderedBidiMap interface.
</tr>
</table>
</p>
</div>
<!-- ========================================================================= -->
<div id="map" style="display:none">
<h3>Map interface
<p>
The <a href="http://java.sun.com/j2se/1.4/docs/api/java/util/Map.html">Map interface and implementations in both the JDK and Commons Collections allow you to lookup data from a key to a value. This is one of the most powerful interfaces in the JDK, however it is very difficult to implement. These are the available implementations:
<table>
<tr>
<td>HashMap (JDK)
<td>This map is the most commonly used and fastest implementation. It is suitable for most situations and can store any object. Objects are stored in any order, and the order may change over time.
</tr>
<tr>
<td>HashedMap
<td>The Commons Collections hash map implementation which is very similar in design to HashMap, but also supports easy iteration via a MapIterator. This implementation is also designed to be subclassed. Use this map if you want to use the extra iterator, or to subclass. Otherwise, there is no advantage (or disadvantage) compared to the JDK HashMap.
</tr>
<tr>
<td>IdentityMap
<td>This map operates exactly as per HashedMap but compares keys and values using == not .equals().
</tr>
<tr>
<td>IdentityHashMap (JDK)
<td>This map operates exactly as per HashMap but compares keys and values using == not .equals().
</tr>
<tr>
<td>TreeMap (JDK)
<td>This map ensures that the keys are always sorted, and iteration order is consistent. All keys must implement Comparable, unless a comparator is supplied. This map should be used if you need the map to be sorted.
</tr>
<tr>
<td>LinkedMap
<td>This map maintains the order that each object is added to the map. When the map is viewed via an iterator, the insertion order will be seen. This map should be used if you need to retain the insertion order.
</tr>
<tr>
<td>LinkedHashMap (JDK)
<td>This map, available from JDK 1.4, maintains the order that each object is added to the map. When the map is viewed via an iterator, the insertion order will be seen. Commons collections' LinkedMap provides the same functionality for JDK1.2 onwards and provides a MapIterator.
</tr>
<tr>
<td>ListOrderedMap
<td>This map decorates another map to maintains the order that each object is added to the map. The order is maintained using a list. This map should be used if you need to retain the insertion order but you also need the special features of another map.
</tr>
<tr>
<td>LRUMap
<td>This map places a maximum size limit on the map and removes the least recently used (LRU) key-value when it is full. This map should be used for caches where you want to limit the maximum memory size used, and are happy with the least recently used algorithm for removals.
</tr>
<tr>
<td>ReferenceMap
<td>This map enables keys and values to be garbage collected whilst still held in the map. This can be useful for building memory sensitive caches. This map should be used for caches where you want to allow garbage collection from the map.
</tr>
<tr>
<td>WeakHashMap (JDK)
<td>This map stores each key-value pair with a key that can be garbage collected. This can be useful for building memory sensitive caches. ReferenceMap provides the same functionality but with much more flexibility.
</tr>
<tr>
<td>ReferenceIdentityMap
<td>This map operates exactly as per ReferenceMap but compares keys and values using == not .equals().
</tr>
<tr>
<td>SingletonMap
<td>This map is restricted to storing one key-value pair. It may contain no more than and no less than one pair. It provides a MapIterator. This map should be used if you want to return one key-value pair and must use the Map interface.
</tr>
<tr>
<td>Flat3Map
<td>This map is optimised to store one, two or three key-value pairs and outperforms HashMap at these sizes. For size four and above performance is about 5% slower than HashMap. This map also has good garbage collection characteristics when below size four. It provides a MapIterator. This map should be used if are 99% sure that the map will be size three or less.
</tr>
<tr>
<td>StaticBucketMap
<td>This map is optimised to work well in a heavily loaded multi-threaded environment. It provides synchronization internally, locking on a per 'bucket' basis, where the buckets are fixed at construction time. You should use this class if you can predict the size of the map and you are working in a very intensive multi-threaded environment.
</tr>
</table>
</p>
</div>
</div>
<!-- ========================================================================= -->
<div id="collectionmain" style="display:none">
<h3>Collections
<p>
Do you want to access each element by index? Then use a <a href="#" onclick="showList()">List.
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.