gpt4 book ai didi

java - 从填充的 HashMap 中检索到的 Arraylist 不断返回一个空的 arraylist

转载 作者:行者123 更新时间:2023-11-29 07:16:02 25 4
gpt4 key购买 nike

我有一个 hashmap,我正在插入一个数组列表作为 while 循环中的值。在迭代期间,如果 hashmap 已经包含一个键,我想检索已经存储的 arraylist,向其附加新数据并将其放回 hashmap。

我现在遇到的问题是,当我检查 hashmap 是否包含一个值并且它确实包含一个值时,返回的数组列表的大小为 0,就好像我以前从未输入过任何东西一样。

final HashMap<String, ArrayList<String>> map = new HashMap<String, ArrayList<String>>();
String status = "";
String channel = "";

while (daily.next()) {
while (avrg.next())
if (avrg.getString(1).equals(daily.getString(1)) && avrg.getString(2).equals(daily.getString(2))) {
final Object pstmnt = null;
final Object pstmnt2 = null;
final float thresholdValue = calcThreshold(pstmnt, pstmnt2, daily.getString(1));
channel = daily.getString("client_name");
if (daily.getFloat(3) > avrg.getFloat(3) + thresholdValue * avrg.getFloat(3)) {
status = "HIGHER";
}
else if (daily.getFloat(3) < avrg.getFloat(3) - thresholdValue * avrg.getFloat(3)) {
status = "LOWER";
}
else {
status = "Normal";
}
final PrintStream out;
out.println(channel);
out.println(thresholdValue);
out.println(status);
if (map.containsKey(channel)) {
out.println("map contained key");
final ArrayList<String> temp = new ArrayList<String>();
temp.addAll(map.get(channel));
out.println("previous size: " + temp.size());

temp.add(daily.getString("event"));
temp.add(Float.toString(daily.getFloat(3)));
temp.add(Float.toString(avrg.getFloat(3)));
temp.add(Float.toString(thresholdValue * 100));
temp.add(status);
out.println("current size: " + temp.size());
map.put(channel, temp);
out.println("array added to map");
temp.clear();
System.out.println("done");

}

else {
final ArrayList<String> temp = new ArrayList<String>();
out.println("new key created");
temp.add(daily.getString("event"));
temp.add(Float.toString(daily.getFloat(3)));
temp.add(Float.toString(avrg.getFloat(3)));
temp.add(Float.toString(thresholdValue * 100));
temp.add(status);

System.out.println(temp.size());
map.put(channel, temp);
System.out.println("array added to map");
}
}
avrg.beforeFirst();
}

我真的不确定为什么会这样。任何帮助,将不胜感激!

最佳答案

问题就在这里:

map.put(channel, temp);
out.println("array added to map");
temp.clear();

temp.clear() 清除您刚刚添加到 map 的相同列表。由于 temp 即将超出范围,该语句没有任何用处,可以删除。

关于java - 从填充的 HashMap 中检索到的 Arraylist 不断返回一个空的 arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9570647/

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