【发布时间】:2018-02-03 14:21:27
【问题描述】:
我一直在编写以下内容以尝试更好地理解哈希映射。我有一个由分隔符 | 分隔的原始项目列表。目标是确保最后我有一个 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]
我怎样才能在最后实现这个期望的输出?
【问题讨论】:
-
您希望为每个客户创建一个单独的日志列表,但您正在创建一个日志列表。这怎么可能?
-
考虑使用Guava ListMultiMap。
-
@JBNizet 有道理...
标签: java list arraylist hashmap hashset