转换 Map到分隔字符串

发布于 01-11 06:31 字数 462 浏览 4 评论 0原文

将 Java Map转换为最佳方法是什么?到一个分隔字符串?

使用番石榴的示例:

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));

不要压平 String[]:

s1=**[Ljava.lang.String;@2e98be86**,s2=[Ljava.lang.String;@b676908

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金橙橙2025-01-18 06:31:19

你可以这样做:

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()));

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()));
九歌凝2025-01-18 06:31:19

您可以使用流来做到这一点:

items.entrySet().stream().map(e -> e.getKey() + "=" + Stream.of(e.getValue()).collect(Collectors.joining(","))).collect(Collectors.joining(","));

You could do this using streams:

items.entrySet().stream().map(e -> e.getKey() + "=" + Stream.of(e.getValue()).collect(Collectors.joining(","))).collect(Collectors.joining(","));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文