过滤两次Lambda Java [英] Filter Twice Lambda Java

查看:280
本文介绍了过滤两次Lambda Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表如下1,2,3,4,5,6,7 br>

预期结果必须

 1 23 45 6 7

我知道如何做到7



我的结果

 1 23 45 6

我想知道如何输入7。



我添加了 i - >我== objList.size() - 1 到我的过滤器,但它不给我想要的



任何人都有任何想法如何slove这个?
代码:

 列表结果= IntStream.range(0,objList.size() -  1)
.filter(i - > i%2 == 0 || i - > i == objList.size() - 1)
.mapToObj(i - > objList.get(i)+ + objList.get(i + 1))
.collect(Collectors.toList());
result.forEach(i - > System.out.print(+ i));


解决方案

您的第一个问题是 IntStream.range(X,Y)。该方法期望在开始时包含的范围,并且在结束时排除。由于您作为范围的结尾通过 objList.size() - 1 (6)),输入列表(6)中最后一个字符串的索引为排除在 IntStream 之外,并且位于此索引(7)的字符串未显示在您的输出中。

  static IntStream range(int startInclusive,
int endExclusive)

返回从startInclusive(包含)到endExclusive(排除)的顺序排序IntStream

API注意:

可以使用for循环按顺序生成递增值的等价序列,如下所示:


for(int i = startInclusive; i< endExclusive; i ++){...}

来源



将其更改为 IntStream.range(0,objList.size()) p>

<另一个问题是 .filter(i - > i%2 == 0 ||我 - > i == objList.size() - 1)将不会编译,因为它需要一个单一的lambda表达式,并且不需要 ||我 - >无论如何,我== objList.size() - 1 部分(自数组的最后一个索引6,已经是偶数)。



当然这还不行。如果您修复了这些问题,当i = 6时, objList.get(i + 1)将给您一个 ArrayIndexOutOfBoundsException 异常。



我对最终问题的解决可能不是最优雅的解决方案,但这是我在这个晚上编译和工作的第一件事。 p>

修正我们终于得到(在最后一行添加了一些小的格式之后):

  import java.util.Arrays; 
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import java.util.List;

public class LambdaTest
{
public static void main(String [] args)
{
列表< String> objList = Arrays.asList(1,2,3,4,5,6,7);
列表< String> result = IntStream.range(0,objList.size())
.filter(i - >(i%2)== 0)
.mapToObj(i - >(i< objList。 size() - 1)?(objList.get(i)++ objList.get(i + 1)):objList.get(i))
.collect(Collectors.toList());
result.forEach(i - > System.out.print(\+ i +\));
}
}

我尝试过这个代码 here ,并得到预期的输出。


I have a list as follows "1", "2", "3", "4", "5", "6", "7"
and Expected outcome must be

"1 2" "3 4" "5 6" "7"

I figure it out how to do till 7

My outcome

"1 2" "3 4" "5 6"

I am wondering to know how I can type 7 as well.

I added i -> i == objList.size()-1 to my filter yet it does not give what I want

anybody has any idea how to slove this? Code:

List result = IntStream.range(0, objList.size()-1)
            .filter( i -> i % 2 == 0 || i -> i == objList.size()-1)
            .mapToObj(i -> objList.get(i) + "" + objList.get(i + 1))
            .collect(Collectors.toList());      
    result.forEach(i -> System.out.print(" " + i ));

解决方案

Your first probelm is with IntStream.range(x,y). This method expects a range which is inclusive at the start and exclusive at the end. Since you passed objList.size()-1 (6 in your case) as the end of the range, the index of the last String in your input list (6) was excluded from the IntStream and the String located at this index ("7") didn't appear in your output.

static IntStream range(int startInclusive,
                       int endExclusive)

Returns a sequential ordered IntStream from startInclusive (inclusive) to endExclusive (exclusive) by an incremental step of 1.

API Note:

    An equivalent sequence of increasing values can be produced sequentially using a for loop as follows:


         for (int i = startInclusive; i < endExclusive ; i++) { ... }

(Source)

Change it to IntStream.range(0, objList.size()).

Another problem is that .filter( i -> i % 2 == 0 || i -> i == objList.size()-1) won't compile, since it expects a single lambda expression, and you don't need the || i -> i == objList.size()-1 part anyway (since the last index of the array, 6, is already even).

Of course that still won't work. If you fix those problems objList.get(i + 1) will give you an ArrayIndexOutOfBoundsException exception when i = 6.

My fix to the final problem is probably not the most elegant possible solution, but that's the first thing I came up with that compiled and worked at this late hour.

Fixing that we finally get (after some minor formatting added in the last line) :

import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Collectors;
import java.util.List;

public class LambdaTest     
{
    public static void main (String[] args)
    {
        List<String> objList = Arrays.asList("1", "2", "3", "4", "5", "6", "7");
        List<String> result = IntStream.range(0, objList.size())
            .filter( i -> (i % 2) == 0)
            .mapToObj(i -> (i<objList.size()-1)?(objList.get(i) + " " + objList.get(i + 1)):objList.get(i))
            .collect(Collectors.toList());      
        result.forEach(i -> System.out.print("\"" + i + "\" "));
    } 
}

I tried this code here and got the expected output.

这篇关于过滤两次Lambda Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆