Java的8个流,如何减少或收“破发”,没有抛出运行时异常?

纳博科夫:

这个问题已经被问的一个方面forEach

评论(答案被接受后):我接受@nullpointer的答案,但它是正确的只是在我的代码示例的情况下,而不是在大约减少的突破能力一般的问题..

问题:

但有一个方式reducecollect以“破发”过早,没有通过所有的流元素回事?(这意味着我需要积累的状态,而迭代,所以我用reducecollect)。

总之:我需要遍历流(元素是整数,从小下令大)的所有元素,但看看到2个相邻元素并加以比较,如果它们之间的差值小于1时,我需要“破发”和一站式“积累状态”,我需要返回最后传递的元素。

变异投掷RuntimeException和变种通过外部状态-对我不好。

代码示例与评论:

public class Solution {

public int solution(int[] A) {

    Supplier<int[]> supplier = new Supplier<int[]>() {
        @Override
        public int[] get() {
            //the array describes the accumulated state:
            //first element in the array , if set > 0, means  - the result is achieved, we can stop iterate over the rest elements
            //second element in the array will represent the "previous element" while iterating the stream
            return new int[]{0, 0};
        }
    };

    //the array in accumulator describes the accumulated state:
    //first element in the array , if set > 0, means  - the result is achieved, we can stop iterate over the rest elements
    //second element in the array will represent the "previous element" while iterating the stream
    ObjIntConsumer<int[]> accumulator = new ObjIntConsumer<int[]>() {
        @Override
        public void accept(int[] sett, int value) {
            if (sett[0] > 0) {
                ;//do nothing, result is set
            } else {
                if (sett[1] > 0) {//previous element exists
                    if (sett[1] + 1 < value) {
                        sett[0] = sett[1] + 1;
                    } else {
                        sett[1] = value;
                    }
                } else {
                    sett[1] = value;
                }
            }
        }
    };

    BiConsumer<int[], int[]> combiner = new BiConsumer<int[], int[]>() {
        @Override
        public void accept(int[] sett1, int[] sett2) {
            System.out.println("Combiner is not used, we are in sequence");
        }
    };

    int result[] = Arrays.stream(A).sorted().filter(value -> value > 0).collect(supplier, accumulator, combiner);
    return result[0];
}


/**
 * We have an input array
 * We need order it, filter out all elements that <=0 (to have only positive)
 * We need find a first minimal integer that does not exist in the array
 * In this example it is 5
 * Because 4,6,16,32,67 positive integers array is having 5 like a minimum that not in the array (between 4 and 6)
 *
 * @param args
 */
public static void main(String[] args) {
    int[] a = new int[]{-2, 4, 6, 16, -7, 0, 0, 0, 32, 67};
    Solution s = new Solution();
    System.out.println("The value is " + s.solution(a));
}

}

也:

给定一个数组作为输入,在我看来,你要找的是这样的:

int stateStream(int[] arr) {
    return IntStream.range(0, arr.length - 1)
            .filter(i -> arr[i + 1] - arr[i] > 1) // your condition
            .mapToObj(i -> arr[i])
            .findFirst() // first such occurrence
            .map(i -> i + 1) // to add 1 to the point where the cehck actually failed
            .orElse(0); // some default value
}

或者划痕,而将其转换为一个排序和筛选值列表如下:

int stateStream(int[] arr) {
    List<Integer> list = Arrays.stream(arr)
            .boxed().sorted()
            .filter(value -> value > 0)
            .collect(Collectors.toList());
    return IntStream.range(0, list.size() - 1)
            .filter(i -> list.get(i + 1) - list.get(i) > 1)
            .mapToObj(list::get)
            .findFirst()
            .map(i -> i + 1)
            .orElse(0);
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有什么需要抛出运行时异常

来自分类Dev

仅当抛出运行时异常时,才发现没有线程绑定请求

来自分类Dev

如何使@NotNull抛出运行时异常?

来自分类Java

什么时候抛出运行时异常?

来自分类Java

构造函数可以抛出运行时异常吗?

来自分类Dev

asyncio 抛出运行时错误并忽略异常

来自分类Java

聚合Java 8流中的运行时异常

来自分类Dev

为什么Contextmanager抛出运行时错误“ throw()之后生成器没有停止”?

来自分类Dev

C++ 纯虚函数调用不会抛出运行时异常?

来自分类Javascript

为什么[[]] [0] ++可以工作,但是[] ++会抛出运行时异常?

来自分类Dev

可拆分类抛出运行时异常,将其编组为未知类型

来自分类Dev

实体框架核心2.0 HasColumnType抛出运行时异常

来自分类Dev

opencv linemod抛出运行时错误

来自分类Dev

编译 index.cshtml 抛出运行时错误

来自分类Dev

Model.fit_generator抛出运行时错误:

来自分类Java

为什么会出现这种使用泛型不会抛出运行时或编译时的异常?

来自分类Java

我的应用抛出运行时异常并崩溃了。我必须添加一些东西吗?

来自分类Dev

构建通用应用的发行版时,为什么我的Xamarin PCL会抛出运行时异常?

来自分类Java

如何验证所有自己抛出的运行时异常覆盖的Javadoc?

来自分类Dev

监视ThreadPoolExecutor抛出的运行时异常

来自分类Java

MongoDB的运行时异常抛出的NoClassDefFoundError

来自分类Dev

有没有办法找出运行时运行可执行文件的位置?

来自分类Dev

哪些编程语言没有运行时异常?

来自分类Java

抛出一个异常,如果流没有结果

来自分类Dev

使用Java8运行时如何在流中获取新的用户输入

来自分类Java

将Y添加到Collection <X>,将Y添加为X的instanceof。为什么不抛出运行时异常?

来自分类Java

将Y添加到Collection X中,将Y添加为X的instanceof。为什么不抛出运行时异常?

来自分类Dev

为什么用Install4j 6构建的macOS安装程序会在JRE 10中抛出运行时异常?

来自分类Dev

如何吞下 IOException 作为自定义运行时异常 Java 8

Related 相关文章

  1. 1

    有什么需要抛出运行时异常

  2. 2

    仅当抛出运行时异常时,才发现没有线程绑定请求

  3. 3

    如何使@NotNull抛出运行时异常?

  4. 4

    什么时候抛出运行时异常?

  5. 5

    构造函数可以抛出运行时异常吗?

  6. 6

    asyncio 抛出运行时错误并忽略异常

  7. 7

    聚合Java 8流中的运行时异常

  8. 8

    为什么Contextmanager抛出运行时错误“ throw()之后生成器没有停止”?

  9. 9

    C++ 纯虚函数调用不会抛出运行时异常?

  10. 10

    为什么[[]] [0] ++可以工作,但是[] ++会抛出运行时异常?

  11. 11

    可拆分类抛出运行时异常,将其编组为未知类型

  12. 12

    实体框架核心2.0 HasColumnType抛出运行时异常

  13. 13

    opencv linemod抛出运行时错误

  14. 14

    编译 index.cshtml 抛出运行时错误

  15. 15

    Model.fit_generator抛出运行时错误:

  16. 16

    为什么会出现这种使用泛型不会抛出运行时或编译时的异常?

  17. 17

    我的应用抛出运行时异常并崩溃了。我必须添加一些东西吗?

  18. 18

    构建通用应用的发行版时,为什么我的Xamarin PCL会抛出运行时异常?

  19. 19

    如何验证所有自己抛出的运行时异常覆盖的Javadoc?

  20. 20

    监视ThreadPoolExecutor抛出的运行时异常

  21. 21

    MongoDB的运行时异常抛出的NoClassDefFoundError

  22. 22

    有没有办法找出运行时运行可执行文件的位置?

  23. 23

    哪些编程语言没有运行时异常?

  24. 24

    抛出一个异常,如果流没有结果

  25. 25

    使用Java8运行时如何在流中获取新的用户输入

  26. 26

    将Y添加到Collection <X>,将Y添加为X的instanceof。为什么不抛出运行时异常?

  27. 27

    将Y添加到Collection X中,将Y添加为X的instanceof。为什么不抛出运行时异常?

  28. 28

    为什么用Install4j 6构建的macOS安装程序会在JRE 10中抛出运行时异常?

  29. 29

    如何吞下 IOException 作为自定义运行时异常 Java 8

热门标签

归档