使用 aMap
将您的独特客户与您的用户一起编译,然后遍历地图并格式化您String
的 s。
使用您的输入作为的示例String[]
:
public static void main(String[] args) {
final String[] strings = {"customer1 user1",
"customer1 user2",
"customer2 user3",
"customer3 user4",
"customer3 user5"};
final Map<String, Set<String>> concatentatingMap = new HashMap<String, Set<String>>() {
@Override
public Set<String> get(Object key) {
Set<String> strings = super.get(key);
if (strings == null) {
strings = new LinkedHashSet<>();
put((String) key, strings);
}
return strings;
}
};
for (final String s : strings) {
final String[] split = s.split("\\s++");
concatentatingMap.get(split[0]).add(split[1]);
}
final MessageFormat messageFormat = new MessageFormat("script.bat -c {0} -u {1}");
for (final Entry<String, Set<String>> entry : concatentatingMap.entrySet()) {
final String customer = entry.getKey();
final StringBuilder usersBuilder = new StringBuilder();
final Iterator<String> iter = entry.getValue().iterator();
while (iter.hasNext()) {
usersBuilder.append(iter.next());
if (iter.hasNext()) {
usersBuilder.append(" ");
}
}
System.out.println(messageFormat.format(new Object[]{customer, usersBuilder.toString()}));
}
}
输出:
script.bat -c customer3 -u user4 user5
script.bat -c customer2 -u user3
script.bat -c customer1 -u user1 user2