Skip to content
Advertisement

Check if list elements are a continuous range of integers using java stream API [duplicate]

Given var ns = List.of(1,2,3,4) How can one check if the list elements are consecutive using java stream API

It can be done using the following loop:

JavaScript

How can it be done using ns.stream.reduce or other stream method?

Advertisement

Answer

There are many ways to solve this, I would like to create an ordered list and compare it with the initial one, for example:

JavaScript

Or also you can use:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement