ArrayList as HashMap keys
Hi All,
I have a HashMap that uses an ArrayList of Integer objects as it's keys (i.e. the HashMap is declared to be HashMap<ArrayList><Integer>,Object o> ). I remove the integers from the ArrayList one by one as required, until the ArrayList is empty. When The ArrayList is empty I want to remove the entry from the HashMap.
The sample program below demonstrates the problem. The program knows the key is an empty ArrayList, however it is unable to remove it, instead returning null, which from the documentation would indicate that there is no mapping for the key. How can I remove the object?
import java.util.*;
publicclass HashMapTest{
public HashMap<ArrayList><Integer>,ArrayList<String>> hashmap;
publicstaticvoid main(String[] args){
new HashMapTest();
}
/*Setup initial values, and the remove integers*/
public HashMapTest(){
hashmap =new HashMap<ArrayList><Integer>,ArrayList<String>>();
ArrayList<Integer> dependancies =new ArrayList<Integer>();
dependancies.add(1);
dependancies.add(2);
ArrayList<String> tasks =new ArrayList<String>();
tasks.add("TASK 1");
tasks.add("TASK 2");
hashmap.put(dependancies,tasks);
ArrayList<Integer> dependancies2 =new ArrayList<Integer>();
dependancies2.add(2);
dependancies2.add(3);
ArrayList<String> tasks2 =new ArrayList<String>();
tasks2.add("TASK 3");
tasks2.add("TASK 4");
hashmap.put(dependancies2,tasks2);
//NOW RETURN JOB 1;
System.out.println(hashmap);
removeInt(hashmap,2);
removeInt(hashmap,1);
System.out.println(hashmap);
hashmap.remove(new ArrayList<Integer>());
System.out.println(hashmap);
}
publicvoid removeInt(HashMap<ArrayList><Integer>, ArrayList<String>> m,int jobReturned){
int mapsize = m.size();
Integer myInt = jobReturned;
HashMap map = m;
Iterator<Map.Entry><ArrayList><Integer>,ArrayList<String>>> keyValuePairs1 = map.entrySet().iterator();
for (int i = 0; i < mapsize; i++)
{
Map.Entry<ArrayList><Integer>,ArrayList<String>> entry = keyValuePairs1.next();
ArrayList<Integer> key = entry.getKey();
ArrayList<String> value = entry.getValue();
if(key.contains(myInt)){
key.remove(myInt);
}
if(key.isEmpty()){
System.out.print("Key is: ");
System.out.println(key);
System.out.println("Returned at removal" + map.remove(key));
}
}
}
}
All help gratefully received,
TIA
Adam

