2012-12-08 39 views
0

我有一个散列表mapLinkedLists在每个元素(实施单独的链接,如解释here)。我怎么可以添加新的节点到表中的每个LinkedList元素?添加项到散列表LinkedList

我令人头大我的脑子,我不认为
map.get(index).add(new Object);
会的工作,只是因为哈希表的get()方法获取密钥,而不是周围的其他方式的价值...

回答

0

map.get(index).add(new Object);

只要index是关键,而不是一个计数器,并且您的链表被存储在这个键上,那么这个语句就可以工作。

与存储在可通过计数器访问的连续位置的数组不同,(Key,Value)对存储数据。

0

假设

Map<KeyClass,List<Item>> map = new HashMap<KeyClass,List<Item>>(); 

尝试

KeyClass key = ...; 
List<Item> list = map.get(key); 
if (list == null) 
{ 
    list = new LinkedList<Item>(); 
    map.put(key,list); 
} 
list.add(...whatever...);