Re: Table object
Philipp wrote:
Hello,
I'm looking for a collection which behaves like a two column table
(ordered key - value pairs, key not unique). Is there such a thing
either in the JRE or Apache collections?
Thanks Phil
Map<Key, List<Value>> map;
You can use an commons-collection lazy-map to help with it, but commons
doesn't use generics (AFAIK), so you'll get a warning.
Anyway, if you use a LazyMap, you could do this:
map.get(myKey).add(myValue);
The down-side is that lookups will create empty lists, even if you don't
want it to.
I often use the following instead of a lazy list:
public static <K,V> List<V> getReadableList(Map<K,V> map, K key) {
final List<V> list = map.get(key);
if (list == null) { return Collections.emptyList(); }
return list;
}
public static <K,V> List<V> getWritableList(Map<K,V> map, K key) {
List<V> list = map.get(key);
if (list == null) {
list = new ArrayList<V>();
mput.put(key, list);
}
return list;
}
--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>