-2

To filter a List we can use

a1.stream().filter(x->x>6).forEach(System.out::println);

and to filter two lists I used FlatMap

Stream<List<Integer>> s2=Stream.of(a1,a2);
s2.flatMap(x->x.stream()).sorted().filter(x->x>6).forEach(System.out::println);

but I tried filtering in this way for a single List

Stream<List<Integer>> s1=Stream.of(a1);
s1.map(x->x.stream()).filter(x->x>2).forEach(System.out::print);

I got an error--- The operator > is undefined for the argument type(s) Stream<Integer>, int

but when I use the flatMap in the map no error why

Stream<List<Integer>> s1=Stream.of(a1); 
s1.flatMap(x->x.stream()).filter(x->x>2).forEach(System.out::print);
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
Krissag
  • 17
  • 4

2 Answers2

5

If you are calling map(x->x.stream()) on a Stream<List<Integer>>, you'll get a Stream<Stream<Integer>>. You cannot apply .filter(x->x>2) on the elements of that Stream, since those elements are Stream<Integer>s, and the > operator requires two numeric operands.

If you use flatMap instead of map, you are converting your Stream<List<Integer>> to a Stream<Integer> whose elements are all the elements of all the Lists of the original Stream. Therefore, you can apply the .filter(x->x>2) filter on them.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Stream> s1=Stream.of(a1); s1.filter(x->x>2).forEach(System.out::print); error--The operator > is undefined for the argument type(s) List, int – Krissag Jan 10 '21 at 11:01
  • @Krissag well, in this case the elements of your `Stream` are `List`. You cannot ask if a `List` > 2. – Eran Jan 10 '21 at 11:03
2
Stream<List<Integer>> s1 = Stream.of(a1);
        s1.map(x -> x.stream()).filter(x -> x > 2).forEach(System.out::print);

This will give error as you are not using flat map so filtering not possible whole (Streams >2). You can try like this:

    Stream<List<Integer>> s1 = Stream.of(a1);
            s1.map(x -> x.stream()).filter(x -> x.findFirst().get() > 2).forEach(System.out::print);

As we cannot apply.filter(x->x>2) on we will requires two numeric operands. s1.map(x -> x.stream()).filter(x -> x.findFirst().get() > 2).forEach(System.out::print); if we will take this then filter will find find matching element value which is greater than 2. In short we have to use flatmap to find greater than 2 in those streams or any operation to fetch numeric operands from stream then only it work.

dreamcrash
  • 47,137
  • 25
  • 94
  • 117
murari99732
  • 157
  • 1
  • 7
  • Explain your code or at least write a few words about it – Aalexander Jan 10 '21 at 22:12
  • As we cannot apply .filter(x->x>2) on we will requires two numeric operands. s1.map(x -> x.stream()).filter(x -> x.findFirst().get() > 2).forEach(System.out::print); if we will take this then filter will find find matching element value which is greater than 2. In short we have to use flatmap to find greater than 2 in those streams or any operation to fetch numeric operands from stream then only it work. – murari99732 Jan 12 '21 at 08:05
  • @murari99732 I just formatted you answer, if you do like fell free to rollback :) – dreamcrash Jan 28 '21 at 17:05