We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
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.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all 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.