在Java中使用链接列表的哈希映射 [英] Hash Map with LinkedList in java

查看:269
本文介绍了在Java中使用链接列表的哈希映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



像,如果我把它放在:

  map.put(1,A); 
map.put(1,B);

然后它会自动将A和B添加到链接列表中。当我从地图中检索时,如:

  map.get(1)

我找回包含它们的列表吗?

解决方案

Java没有,但可以使用 MultiMap 来自 Google Guava


一个与Map类似的集合,但可以将多个值与一个关键字相关联。如果使用相同的键但不同的值调用put(K,V)两次,则multimap包含键与两个值的映射。

方法get(K),keySet(),keys(),values(),entries()和asMap()返回作为multimap视图的集合

这篇文章 Multimaps - Google Guava 为您提供了完整的使用方法,以及如何使用 HashMap 使用 List 作为值。


Is there a built in implementation in java for hash map whose values are linked lists?

like, if I put:

 map.put(1, "A");
 map.put(1, "B");

then it automatically add A and B to the linked list. When I retrieve from the map, as:

  map.get(1)

I get back a list containing both of them?

解决方案

Java does not have it but you can use MultiMap from Google Guava.

A collection similar to a Map, but which may associate multiple values with a single key. If you call put(K, V) twice, with the same key but different values, the multimap contains mappings from the key to both values.

The methods get(K), keySet(), keys(), values(), entries(), and asMap() return collections that are views of the multimap

This article Multimaps - Google Guava gives you complete idea about how to use it and also how to do it with HashMap using List as value.

这篇关于在Java中使用链接列表的哈希映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆