Skip to content
Advertisement

Call custom static functions from filter and map in Java 8 – stream

I want to call method name nameStartingWithPrefix() which is inside filter and its definition will be in Filter class.

All ArrayList describes in main(), but the list is not passed as in an argument, how can I call in List of names inside Filter.nameStartingWithPrefix().

Syntax is given like:

names is name of ArrayList which is inside main()

JavaScript

Below is code sample:

JavaScript
  1. Expected like, I have to write a code on Filter method which filters the names on the basis of prefix given by the user in runtime. But I am unable to access list on the basis of given code. can you please help me how to access List of names inside the Filter class.

    1. I think List should be passed in arguments of (Filter.nameStartingWithPrefix(scanner.nextLine())) but its not there.

Advertisement

Answer

Since you are using a Custom Filter Class to invoke the filter over the stream. filter function requires a Predicate, therefore you will need to create a custom predicate and return it from the nameStartingWithPrefix function.

In the same way, you will require to create a Function for mapper and will return it from the function.

JavaScript

For further clarification for filter and map please check out below links:

Documentation for Predicate Functions : https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html#test-T-

Video Explanation for Map : https://www.youtube.com/watch?v=bTTNVP_ORr8

Video Explanation for Filter : https://www.youtube.com/watch?v=vHwToYEYvsU

Note: Please follow Stackoverflow guidelines to ask questions.

Advertisement