【发布时间】:2017-01-17 15:28:33
【问题描述】:
我有一组这样的字符串
A_2007-04, A_2007-09, A_Agent, A_Daily, A_Execute, A_Exec, B_Action, B_HealthCheck
我想输出为:
Key = A, Value = [2007-04,2007-09,Agent,Execute,Exec]
Key = B, Value = [Action,HealthCheck]
我正在使用 HashMap 来执行此操作
pckg:{A,B}
count:total no of strings
reports:set of strings
我使用的逻辑是嵌套循环:
for (String l : reports[i]) {
for (String r : pckg) {
String[] g = l.split("_");
if (g[0].equalsIgnoreCase(r)) {
report.add(g[1]);
dirFiles.put(g[0], report);
} else {
break;
}
}
}
我得到的输出为
Key = A, Value = [2007-04,2007-09,Agent,Execute,Exec]
如何获得第二把钥匙? 有人可以为此提出逻辑吗?
【问题讨论】:
-
pckg的值是多少?
-
首先,您的变量名称令人困惑。你应该给他们起有意义的名字。二、
pckg的类型是什么?在reports[i]的 for 循环中循环pckg变量似乎很奇怪@ -
用 P_Action 和 P_HealthCheck,你不能得到 Key = B, Value = [Action,HealthCheck],你只能用 Key = P 得到它,所以 pckg 应该是:{A,P} 而不是 {A ,B}