gpt4 book ai didi

java - 给定由 LinkedList 组成的 java hashmap 中的键,如何更新值?

转载 作者:行者123 更新时间:2023-12-02 03:17:11 25 4
gpt4 key购买 nike

这是我的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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com