を使用しMap
て独自の顧客をユーザーとともにコンパイルし、マップをループして をフォーマットしますString
。
入力を として使用する例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