猿问

自定义对象上的流过滤器给定范围的多维数组

最近几天,我尝试了 Java 8 和 lambda 表达式。实现起来更清晰、更清晰、更有趣,但是我一直困惑如何在给定范围内迭代多维数组以找到第一次出现的非空元素。例如,这是我的数组:


MyObject[][] array = new MyObject[][]; //this array is never full objects are placed at random places

正如评论暗示我试图找到第一次出现或非空对象,让我们说


array[0-5][irrelevant] 

       or

array[irrelevent][3-9]

到目前为止我得到的最接近的是:


MyObject obj = Arrays.stream(grid.grid)

                .flatMap(IntStream.range(0,2)) //here it must work for any dimension given any range

                .filter(array -> array != null)

                .findFirst()

                .orElse(null); 

显然,这不会编译,因为它不是一个 Integer 元素,而是一个自定义对象。非常感谢任何帮助。


红糖糍粑
浏览 130回答 3
3回答

繁星coding

我们可以使用以下语法来做到这一点:MyObject findFirst = Arrays.stream(array).flatMap(Arrays::stream)                        .collect(Collectors.toList())                        .subList(0, 3) // observe this line                        .stream()                        .filter(e -> e != null).findFirst().orElse(null);这里我们将二维数组转换为一个listusing flatMap,然后用 usesubList来指定要搜索的索引的开始和结束。要指定范围,您需要将值传递给 subList(...)

jeck猫

虽然 Nicholas K 的答案适用于水平切片,但它不适用于垂直切片。这是一个完全符合OP想要的答案。为了清楚起见,我已经编写了传统的(使用 for 循环)方式来执行此操作,以确认这就是 OP 打算完成的操作。然后,我使用流完成了它。它适用于水平和垂直切片。public static void main(String[] args) {&nbsp; &nbsp; // Sample data&nbsp; &nbsp; Object[][] array = new Object[5][10];&nbsp; &nbsp; array[1][5] = "this is it";&nbsp; // This is the first non-null object&nbsp; &nbsp; array[4][7] = "wrong one";&nbsp; &nbsp;// This is another non-null object but not the first one&nbsp; &nbsp; // Define range (start=inclusive, end=exclusive)&nbsp; &nbsp; int iStart = 0, iEnd = array.length, jStart = 3, jEnd = 9; // array[irrelevant][3-9]&nbsp; &nbsp; //int iStart = 1, iEnd = 3, jStart = 0, jEnd = array[0].length; // array[1-3][irrelevant]&nbsp; &nbsp; // Doing it the traditional way&nbsp; &nbsp; Object firstNonNull = null;&nbsp; &nbsp; outerLoop:&nbsp; &nbsp; for (int i = iStart; i < iEnd; i++)&nbsp; &nbsp; &nbsp; &nbsp; for (int j = jStart; j < jEnd; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (array[i][j] != null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstNonNull = array[i][j];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break outerLoop;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; assert firstNonNull != null;&nbsp; &nbsp; assert firstNonNull.equals("this is it");&nbsp; &nbsp; // Doing it with Java 8 Streams&nbsp; &nbsp; firstNonNull = Arrays.asList(array)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subList(iStart, iEnd)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatMap(row -> Arrays.asList(row)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .subList(jStart, jEnd)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(Objects::nonNull))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .orElse(null);&nbsp; &nbsp; assert firstNonNull != null;&nbsp; &nbsp; assert firstNonNull.equals("this is it");}

GCT1015

MyObject&nbsp;obj&nbsp;=&nbsp;Arrays.stream(array) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.flatMap(Arrays::stream) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.filter(Objects::nonNull) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.findFirst() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.orElse(null);
随时随地看视频慕课网APP

相关分类

Java
我要回答