【问题标题】:IntStream.range().filter.mapToObj().collect what exactly the meansIntStream.range().filter.mapToObj().collect 究竟是什么意思
【发布时间】:2021-11-19 06:54:14
【问题描述】:

我是 Java 新手,在阅读这些特定的代码行时卡住了:

private static void checkPrice(final List<String> prices) {
    List<String> inputCountryCodes = IntStream.range(0, prices.size())
            .filter(index -> (index % 2 == 0))
            .mapToObj(prices::get)
            .collect(Collectors.toList());

    List<String> distinctInputCountryCodes = inputCountryCodes.stream()
            .distinct()
            .collect(Collectors.toList());
}

谁能解释一下上面代码的para1和para2这些特定的代码行在这里做什么?

【问题讨论】:

    标签: java list dictionary


    【解决方案1】:

    让我们一一来看看:

    List<String> inputCountryCodes = IntStream.range(0, prices.size()).filter(index -> (index % 2 == 0))
                .mapToObj(prices::get)
                .collect(Collectors.toList());
    
    • IntStream 是专门用于 int 类型的流的 Java 接口(用于流、谷歌流或 Java.util.stream
    • range(0, prices.size)static IntStream range(int startInclusive, int endExclusive)
      • ^ 从 0,prices.size() - 1 创建一个内部流
    • prices.size() 是价格长度
    • filter 的英文与 Java 相同。它返回一个由该流中与给定谓词匹配的元素组成的流(删除所有不匹配的元素)
    • index -&gt; index % 2 == 0 是检查元素是否可被 2 整除的 lambda fn(检查是否为 %2 == 0)
    • mapToObj 返回一个对象值 Stream,其中包含应用给定函数的结果。
    • collect 是一个可变的 reduce 函数
    • Collectors.toList() 是扩展 Object 的类,toList 是函数之一。

    这是第 1 段

    
     List<String> distinctInputCountryCodes = inputCountryCodes.stream().distinct()
                .collect(Collectors.toList());
    
    • inputCountryCodes上面得到的字符串列表
    • stream() 将列表变成流
    • distinct 是一个名字,它在流中给出不同的元素。
    • collect(Collectors.toList())已经解释了

    希望能解释一下

    【讨论】:

      【解决方案2】:
      IntStream.range(0, prices.size())
      

      它生成一个从0到奖品列表大小(不包括)的整数流。 因此,假设您的 prices.size() 等于 3,那么 InputStream 将发出 0、1、2

      filter(index -> (index % 2 == 0))
      

      然后它的中间操作将filterInputStream产生的那些数字 基于该数字将使用 2 进行建模。即检查数字是否为偶数。 如果它甚至它会被进一步传递或者被丢弃

      mapToObj(prices::get)
      

      mapToObj 它采用过滤后的数字(偶数)并使用它从prices 获取字符串类型的对象。 所以上面的代码就像

      mapToObj(num -> {
          String item = prices.get(num);
          return item;
      })
      
      collect(Collectors.toList());
      

      mapToObj 收集结果到List&lt;String&gt;

      所以您的List&lt;String&gt; inputCountryCodes 将包含来自prices 的项目,其索引为偶数。

      inputCountryCodes.stream().distinct()
      

      这将从您的inputCountryCodes 中创建一个Stream,其类型为List&lt;String&gt; 然后只取出distinct 的物品。 例如,如果您的 inputCountryCodes 具有 item1item2item2item1 这将导致item1,item2

      collect(Collectors.toList());
      

      然后collect从不同的结果到List&lt;String&gt;

      所以最后你的List&lt;String&gt; distinctInputCountryCodes 将包含来自prices 的项目

      a) whose index is an even number
      
      b) And items are distinct
      

      【讨论】:

        【解决方案3】:

        第一个是这样的:

        List<String> result = new ArrayList<>();
        for(int i = 0; i < prices.size(); i++) {
            if (i % 2 == 0)
                result.add(prices.get(i));
        

        第二个遍历列表并通过将int 装箱到Integer 并在后台使用LinkedHashSet 对象来删除重复项。

        解读

        您可以将range(int a, int b) 视为create a for loop beginning from a to b

        filter(Predicate predicate) 将给定的谓词应用于流并进行相应的过滤。这类似于if(condition) -&gt; the iteration shall pass

        collect(Collectors.toList())collects 迭代,这使得它在列表对象中走到这一步。

        distinct()通过首先装箱基元、将对象传递给集合对象并返回不同的结果来删除重复项。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-03-12
          • 2014-10-29
          • 2016-01-27
          • 2014-12-05
          • 1970-01-01
          • 1970-01-01
          • 2018-07-27
          相关资源
          最近更新 更多