【问题标题】:Keyset to Set of string [closed]键集到字符串集[关闭]
【发布时间】:2020-01-24 11:28:03
【问题描述】:

我有一个Map<Type, Data>
我们需要将 map 的 keySet 转换为 String 的 LinkedHashSet。
使用 java 8 流最有效的方法是什么。

【问题讨论】:

  • 你有没有尝试过?
  • map.keySet().stream().collect(Collectors.toCollection(LinkedHashSet::new));
  • @HadiJ 流操作不会转换密钥。当不需要转换时,可以使用更简单的new LinkedHashSet<>(map.keySet())

标签: java java-8 java-stream


【解决方案1】:
map.keySet().stream().map(Object::toString).collect(toCollection(LinkedHashSet::new));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 2022-06-15
    • 2021-02-24
    • 1970-01-01
    • 2018-06-06
    相关资源
    最近更新 更多