【问题标题】:Add in System.out::print , i am not able to understand it [duplicate]添加 System.out::print ,我无法理解它[重复]
【发布时间】:2020-12-24 09:41:07
【问题描述】:
map.values().stream().distinct().forEach(System.out::print);

我无法在上面的代码中添加逗号以在哈希表的值之间添加逗号

【问题讨论】:

    标签: java java-8 hashmap system.out


    【解决方案1】:

    如果我理解正确,那么您想将地图的所有值存储到逗号分隔的字符串中,以便您可以使用 String.join(",",list);

    上面的第二个参数是你的地图值的字符串列表

    【讨论】:

      【解决方案2】:

      你可以这样做:

      import java.util.Map;
      import java.util.stream.Collectors;
      
      public class Main {
          public static void main(String[] args) {
              // An example map
              Map<Integer, String> map = Map.of(1, "One", 2, "Two", 3, "Three");
      
              // Join the values using comma as the delimiter
              String values = map.values().stream().distinct().collect(Collectors.joining(","));
      
              // Print
              System.out.println(values);
          }
      }
      

      输出:

      Three,Two,One
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-26
        • 1970-01-01
        • 2014-03-23
        • 2017-11-23
        • 1970-01-01
        • 2022-01-04
        相关资源
        最近更新 更多