我一直在编写以下内容,以尝试更好地理解 HashMap 。我有一个由分隔符 |
分隔的原始项目列表。目标是确保最后我有一个 HashMap,其中每个字母都有一个键,即出现在 |
之前的 a
并附加到每个字母的列表包含相关的“日志”,或者出现在 |
之后的日志,即 a = [asdf a2, asdf a1]。为了清楚起见,我将 a1/a2 放在列表的“日志”部分中。假设当真正执行此代码时,这些提示不会出现。我只想根据初始原始分隔列表将所有“日志”分组到适当的键。
程序:
public static void main(String[] args) {
List<String> items = new ArrayList<>();
// Set up raw data
items.add("a|asdf a2");
items.add("b|asdf b1");
items.add("c|asdf c1");
items.add("c|asdf c2");
items.add("c|asdf c3");
items.add("d|asdf d1");
items.add("a|asdf a1");
items.add("e|asdf e1");
items.add("e|asdf e2");
items.add("e|asdf e3");
items.add("e|asdf e4");
// Display raw data
System.out.println("Raw List: " + items);
// Create a hash map
HashMap<String, List<String>> customerHashMap = new HashMap<>();
// Create new lists. One for customers and one for logs
List<String> customerList = new ArrayList<>();
List<String> logList = new ArrayList<>();
for (String item : items) {
String customer = item.split("\\|", 2)[0];
customerList.add(customer);
String log = item.substring(item.indexOf("|") + 1);
logList.add(log);
// Add to hash map
customerHashMap.put(customer, logList);
}
// Display lists
System.out.println("Customer List: " + customerList);
System.out.println("Log List: " + logList);
System.out.println("Hashmap: " + customerHashMap);
// Print out of the final hash map. Customer a should only have "a" logs, customer b with "b", etc.
System.out.println("");
Iterator it = customerHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
System.out.println(pair.getKey() + " = " + pair.getValue());
it.remove();
}
}
输出:
a = [asdf a2, asdf b1, asdf c1, asdf c2, asdf c3, asdf d1, asdf a1, asdf e1, asdf e2, asdf e3, asdf e4]
b = [asdf a2, asdf b1, asdf c1, asdf c2, asdf c3, asdf d1, asdf a1, asdf e1, asdf e2, asdf e3, asdf e4]
c = [asdf a2, asdf b1, asdf c1, asdf c2, asdf c3, asdf d1, asdf a1, asdf e1, asdf e2, asdf e3, asdf e4]
d = [asdf a2, asdf b1, asdf c1, asdf c2, asdf c3, asdf d1, asdf a1, asdf e1, asdf e2, asdf e3, asdf e4]
e = [asdf a2, asdf b1, asdf c1, asdf c2, asdf c3, asdf d1, asdf a1, asdf e1, asdf e2, asdf e3, asdf e4]\
所需输出:
a = [asdf a2, asdf a1]
b = [asdf b1]
c = [asdf c1, asdf c2, asdf c3]
d = [asdf d1]
e = [asdf e1, asdf e2, asdf e3, asdf e4]
我怎样才能最终达到预期的输出?
最佳答案
List<String> logList = new ArrayList<>();
在循环之前声明,并且该变量永远不会重新分配。
您可以将其用作与 Map
的每个键关联的值。在循环中。
所以 map 的所有键都指向相同的 ArrayList
对象。
您必须创建一个新的 ArrayList
并将其与每个不同键的映射 ( put(key,value)
) 中的键相关联。
这可能会给出:
for (String item : items) {
String customer = item.split("\\|", 2)[0];
customerList.add(customer);
String log = item.substring(item.indexOf("|") + 1);
List<String> logList = customerHashMap.get(customer);
if (logList == null){
logList = new ArrayList<>();
customerHashMap.put(customer, logList);
}
logList.add(log);
}
或者按照 JB Nizet 的建议,使用 Map.computeIfAbsent()
(自 Java 8 起),这样您就不必显式编写不存在检查并声明一个中间局部变量来引用当前的 ArrayList
对象。
for (String item : items) {
String customer = item.split("\\|", 2)[0];
customerList.add(customer);
String log = item.substring(item.indexOf("|") + 1);
customerHashMap.computeIfAbsent(customer, c -> new ArrayList<>()).add(log);
}
关于java - 如何拆分分隔列表并将值重新组合成 HashMap ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45882996/