作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的HashMap
:
HashMap<String, LinkedList<LinkedList<String>>> partitionMap = new HashMap<String, LinkedList<LinkedList<String>>>();
我知道使用 put 方法可以工作,例如:
hashmap.put(key, hashmap.get(key) + 1);
但在这个例子中我们有Integer
,就我而言,我有一个 LinkedList<LinkedList<String>>>
有没有办法做到同样的事情?
最佳答案
put
仍然工作相同。您可以放一双新的String,LinkedList<LinkedList<String>>
进入 map :
LinkedList<LinkedList<String>> aList = new LinkedList<LinkedList<String>>();
hashmap.put("aString", aList);
或者更简洁
hashmap.put("aString", new LinkedList<>());
然后修改 map 内的链接列表,get
,然后修改它。
hashmap.get("aString").add(new LinkedList<String>());
您无需将修改后的列表放回到 map 中。当您获得对 map 中列表的引用时,您正在修改 map 中当前的列表。
<小时/>顺便说一句,您确定想要 LinkedList<LinkedList<String>>
?访问外部 LinkedList 中的任意 LinkedList 将花费线性时间,这意味着编辑嵌套 LinkedList 之一的内容将花费(最好情况)线性时间,并且可能会像二次方一样糟糕。
我推荐一个ArrayList<List<String>>
相反(或将 List<List<String>>
实例化为 ArrayList<List<String>>
)。这样您就可以在恒定时间内访问任意嵌套列表,并且嵌套列表中使用的特定 List 实现保持打开状态。
关于java - 给定由 LinkedList 组成的 java hashmap 中的键,如何更新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40113092/
我是一名优秀的程序员,十分优秀!