What is the best way to convert a Java Map<String, String[]> to a delimited String?
Example using guava:
Map<String, String[]> items = new HashMap<>();
items.put("s1", new String[] {"abc", "def"});
items.put("s2", new String[] {"123", "456"});
System.out.println(Joiner.on(',').withKeyValueSeparator('=').join(items));
Do not flatten the String[]:
s1=**[Ljava.lang.String;@2e98be86**,s2=[Ljava.lang.String;@b676908
Thanks.
CodePudding user response:
You can do something like this:
Map<String, String[]> items = new HashMap<>();
items.put("s1", new String[] {"abc", "def"});
items.put("s2", new String[] {"123", "456"});
System.out.println(items.entrySet().stream().map(es -> es.getKey() "=" String.join(",", es.getValue())).collect(
Collectors.toList()));
CodePudding user response:
You could do this using streams:
items.entrySet().stream().map(e -> e.getKey() "=" Stream.of(e.getValue()).collect(Collectors.joining(","))).collect(Collectors.joining(","));