我创建了一个哈希映射,它将组合了三个参数的唯一键分组,即customer,sc和admin。我想创建一个唯一的密钥列表,其中包含附加的服务器列表。我已经实现了以下内容:
public static void main(String[] args) {
String items = "customer1^sc1^admin1|server1~" +
"customer1^sc1^admin1|server2~" +
"customer1^sc1^admin1|server3~" +
"customer2^sc1^admin1|server1~" +
"customer3^sc1^admin1|server3~" +
"customer3^sc1^admin1|server2~";
// Set up raw data
List<String> splitItems = Arrays.asList(items.split("\\s*~\\s*"));
// Display raw data
System.out.println("Raw List: " + items);
// Create a hash map containing customer name as key and list of logs as value
HashMap<String, List<String>> customerHashMap = new HashMap<>();
// Loop through raw data
for (String item : splitItems) {
// Create new lists. One for customers and one for logs
// List<String> customerList = new ArrayList<>();
List<String> logList;
String list[] = item.split("\\|");
String customer = list[0];
String log = list[1];
logList = customerHashMap.get(customer);
if (logList == null){
logList = new ArrayList<>();
customerHashMap.put(customer, logList);
}
logList.add(log);
// System.out.println(logList);
}
// Print out of the final hash map. Customer "a" should only have "a" logs, customer "b" with "b", etc.
System.out.println("");
List<String> hashMapList = new ArrayList<String>();
Iterator it = customerHashMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pair = (Map.Entry) it.next();
String output = pair.getKey() + "|" + pair.getValue().toString();
hashMapList.add(output);
it.remove();
}
String hashMapResultString = hashMapList.toString();
String hashMapResultFormatted = hashMapResultString.replaceAll("[\\[\\]]", "");
System.out.println(hashMapResultFormatted);
}
原始列表: customer1^sc1^admin1|server1~customer1^sc1^admin1|server2~customer1^sc1^admin1|server3~customer2^sc1^admin1|server1~customer3^sc1^admin1|server3~customer3^sc1^admin1|server2~
哈希地图字符串:
customer2^sc1^admin1|server1, customer3^sc1^admin1|server3, server2, customer1^sc1^admin1|server1, server2, server3
我现在想使用哈希映射来创建一个将被进一步解析的字符串(不要问lol)。所以我将哈希映射的键和值设置为一个字符串,用独特的分隔符|
分隔它们。问题是,因为密钥是List<String>
,所以当打印时,如果其值是包含多个项目的列表,则key
无法确定每个新customer3^sc1^admin1|server3, server2,
的开头。紧接着是customer1^sc1^admin1|server1, server2, server3
。我需要一个分隔符来区分它们。
我的理想输出如下:
customer2^sc1^admin1|server1~customer3^sc1^admin1|server3, server2~customer1^sc1^admin1|server1, server2, server3~...
我怎样才能做到这一点?
更新
这是我最终发现对我的特定问题有用的答案:
StringBuilder s = new StringBuilder();
for (Map.Entry<String, List<String>> entry : customerHashMap.entrySet()) {
s.append(entry.getKey() + "|");
List<String> list = entry.getValue();
for (String item : list) {
if (item != list.get(list.size() - 1)) {
s.append(item + "^");
} else {
s.append(item);
}
}
s.append("~");
}
System.out.println(s.toString());
答案 0 :(得分:1)
您可以遍历地图的条目集:
StringBuilder s = new StringBuilder();
for(Map.Entry<String,List<String>> entry : map.entrySet()) {
s.append(entry.getKey() + "\n");
List<String> list = entry.getValue();
for(String item : list) {
s.append(" " + item + "\n");
}
}
return s.toString();
为了更清楚的示例,我输出的格式与您要求的格式不同,但这说明了如何使用列表值的映射。在根据您的需求进行调整时,请查看java.util.StringJoiner
和相关的Collectors.joining()
;它可能很有用。
Streams在这里很方便:
String encoded = map.entrySet().stream()
.map( entry -> entry.getValue().stream()
.collect(Collectors.joining("^"))
+ "|" + entry.getKey())
.collect(Collectors.joining("~"));
这里发生的是:
Entry<String,List<String>
的流entry -> ...
将每个条目转换为val1^v2^v3^...^valN|key
形式的字符串,即我们将Stream<Entry<>>
映射为Stream<String>
。collect()
使用~
作为分隔符将字符串流连接成一个字符串。