• + 9 comments

    Ok)

    IntStream.range(0, in.nextInt()) - constructs a stream of ints [0..N). Think of it like (for i = 0; i < N; i++)

    .mapToObj(i -> in.next()) - for each i from stream we read a word

    .collect(Collectors.toList()); - gather to list

    So at this points we just read out N words to List<String> strings

    IntStream.range(0, in.nextInt()).mapToObj(i -> in.next()) -- same as previous: read count, and read M words

    .mapToLong(q -> strings.stream().filter(q::equals).count()) - for each of M word: iterate over N words, filter those who are equal to current word, and count elements.

    .forEach(System.out::println) - output that count.

    That is called 'functional style'. If you really want to learn this approach - i'd suggest learning haskell. It's the most pure functional language.