0

I am trying to use multiple filters in my stream. It works ok with one filter but when I add another it can't resolve the symbol stream. It also can't resolve contains or split but all those were working fine with one filter.

try (Stream<String> lines = Files.lines(Paths.get(filePath))){
        lines.stream
                .filter(line -> line.contains(delimiter)).forEach(line -> map.putIfAbsent(line.split(delimiter)[0], RomanNumerals.romanToInt(line.split(delimiter)[1]))
                .filter(line2 -> line.contains(delimiter2)).forEach(line2-> map.putIfAbsent(line2.split(delimiter2)[0], RomanNumerals.romanToInt(line2.split(delimiter2)[1]))
                        )

        );
    }

I updated my code to

try (Stream<String> lines = Files.lines(Paths.get(filePath))){
        lines
                .filter(line -> line.contains(delimiter))
                .filter(line -> line.contains(delimiter2))
                .forEach(line-> map.putIfAbsent(line.split(delimiter)[0], RomanNumerals.romanToInt(line.split(delimiter)[1]))

        );
justcurious
  • 101
  • 1
  • 7
  • 1
    1) Remove `stream`. 2) `forEach` is a *terminal operation*, i.e. it ends the stream chain. 3) `forEach` is void, so it doesn't return anything, ergo you can't chain any further. – Andreas Jan 08 '17 at 00:51
  • Thanks. That works but then how could I do a split based on whether it is delimiter 1 or 2? Also it doesn't seem to do what I want because I want to filter on whether delimiter 1 or delimiter 2 not if delimiter 1 and then a further filter on delimiter 2. – justcurious Jan 08 '17 at 00:57
  • That question would be a duplicate of [Can you split a stream into two streams?](http://stackoverflow.com/q/19940319/5221149) – Andreas Jan 08 '17 at 01:02
  • Thanks! I'll check it out. – justcurious Jan 08 '17 at 01:04

0 Answers0