3

The Files.lines().forEach() method doesn't allow the break. What's the optimal way to read lines in Java 8 in chunks and break when needed?

Update 1: Example for exception.

public class Main {
public static void main(String[] args) {

    try(IntStream s = IntStream.range(0, 5);){
    s.forEach(i -> validate(i));

    }catch(Exception ex){
    System.out.println("Hello World!");    
    }
}
static void validate(int s){

    if(s > 1){            
        //throw new Exception(); ?
    }
    System.out.println(s);
} }
GBlodgett
  • 12,704
  • 4
  • 31
  • 45
A.Dev
  • 569
  • 1
  • 5
  • 19
  • 2
    if you are using a `Stream`, the only way to break would be to throw an Exception; otherwise a for loop as it used to work for decades – Eugene Feb 12 '19 at 21:14
  • Yeah, the for loop doesn't leverage any Java 8 features right? and where do I throw the exception? – A.Dev Feb 12 '19 at 21:19
  • What feature are you trying to leverage? Your example is not leveraging anything, that can't be easily done in a `for` loop. – Andreas Feb 12 '19 at 21:20
  • `Files.lines().filter()` - once the Exception is thrown, catch it - and this will act as the `break`... – Eugene Feb 12 '19 at 21:21
  • 1
    @Eugene Of course, that is using exception for flow control, which is an [anti-pattern](https://softwareengineering.stackexchange.com/q/189222/202153). – Andreas Feb 12 '19 at 21:23
  • @Eugene doesn't the Files.lines() read all the lines into memory if the filter is applied? – A.Dev Feb 12 '19 at 21:25
  • @Andreas agreed, but how would you break from a Stream otherwise? at my workplace we choose a specific exception with no stack-traces – Eugene Feb 12 '19 at 21:26
  • @A.Dev no it does not. don't confuse it with `readAllLine` and `lines` – Eugene Feb 12 '19 at 21:27
  • @Eugene Why would I break from a stream? A stream is for processing all values independent of each other (filtered or not). Rather than trying to use an anti-pattern to force the use of a screwdriver as a hammer, perhaps re-thinking why the code has to use a stream is in order. I'm not saying that exception is not the right choice, *sometimes*, but streams is also not always the right choice. The language has multiple tools (features) that can do the job. A good programmer will use the *right* tool at the right time. – Andreas Feb 12 '19 at 21:35
  • 1
    @Andreas well I am not the good programmers, in such a case. We use it for `reduce`, mainly. – Eugene Feb 12 '19 at 21:36

1 Answers1

1

This is easy by using java-9 takeWhile, So when the condition (s < 1) fails it will drop the remaining part of stream here

when an element is encountered that does not match the predicate, the rest of the stream is discarded.

s.takeWhile(s -> s < 1).forEach(System.out::println);

By using java-8 tried an example but this not every efficient as takeWhile, by combining peek and findFirst() but need to have double check condition which makes no sense rather than this i will prefer standard while loop

IntStream.range(0, 5).peek(i->{
         if(i<3) {
             System.out.println(i);
         }

     }).filter(j->j>=3).findFirst();

do the action in peek and stream will break when ever predicate validated in findFirst

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98