com.quadrique.jbuildingblocks.core.dataStructure
Class JbbHashMapWithMultipleValuesPerKey<K,V>

java.lang.Object
  extended by com.quadrique.jbuildingblocks.core.dataStructure.JbbHashMapWithMultipleValuesPerKey<K,V>
Type Parameters:
K - the datatype for the Keys
V - the datatype for the Values

public class JbbHashMapWithMultipleValuesPerKey<K,V>
extends Object

This data structure is similar to a HashMap except that the data associated to any given key is a list of values (an ArrrayList instance).
The actual differences are:

This class can be seen as a generalisation of the Histogram class in the sense that a set of related values can be grouped together based on a given key.

For instance:

HashMapWithMultipleValuesPerKey<Integer,String> map = new HashMapWithMultipleValuesPerKey<Integer,String>();
map.add(1,"1_red");
map.add(2,"2_yellow");
map.add(3,"3_red");
map.add(2,"2_green");
map.add(3,"3_yellow");
map.add(2,"2_purple");
map.add(2,"2_blue");

System.out.println("\nThe number of keys is: " + map.getNbOfKeys());

System.out.println("\nThe keys are: ");
for(int key : map.keySet())
{
System.out.println("- \"" + key + "\"");
}

for(int key : map.keySet())
{
System.out.println("\nThe value(s) for the key \"" + key + "\" are:");
for(String value : map.get(key))
{
System.out.println("- \"" + value + "\"");
}
}

The corresponding execution results are:


The number of keys is: 3

The keys are:
- "2"
- "1"
- "3"

The value(s) for the key "2" are:
- "2_yellow"
- "2_green"
- "2_purple"
- "2_blue"

The value(s) for the key "1" are:
- "1_red"

The value(s) for the key "3" are:
- "3_red"
- "3_yellow"


Constructor Summary
JbbHashMapWithMultipleValuesPerKey()
           
 
Method Summary
 void add(K key, V value)
           
 ArrayList<V> get(K key)
           
 int getNbOfKeys()
           
 Set<K> keySet()
           
 ArrayList<V> remove(K key)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JbbHashMapWithMultipleValuesPerKey

public JbbHashMapWithMultipleValuesPerKey()
Method Detail

add

public void add(K key,
                V value)
Parameters:
key -
value -

getNbOfKeys

public int getNbOfKeys()
Returns:
the number of keys in the map

keySet

public Set<K> keySet()
Returns:
the set of keys in the map

get

public ArrayList<V> get(K key)
Parameters:
key -
Returns:
list with values

remove

public ArrayList<V> remove(K key)


Copyright © 2001-2008 Quadrique Corporation. All Rights Reserved.