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()
names.stream() .filter(Filter.nameStartingWithPrefix(scanner.nextLine())) .map(Mapper.getDistinctCharactersCount()) .forEachOrdered(System.out::println);
Below is code sample:
public class FilterMapper { private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) { List<String> names = Arrays.asList( "aaryanna", "aayanna", "airianna", "alassandra", "allanna", "allannah", "allessandra", "allianna", "allyanna", "anastaisa", "anastashia", "anastasia", "annabella", "annabelle", "annebelle" ); names.stream() .filter(Filter.nameStartingWithPrefix(scanner.nextLine())) .map(Mapper.getDistinctCharactersCount()) .forEachOrdered(System.out::println); } } class Filter{ public static String nameStartingWithPrefix(String prefix) { } }
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.
- 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.
import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Scanner; import java.util.function.Function; import java.util.function.Predicate; class Filter { public static Predicate<String> nameStartingWithPrefix(String nextLine) { return new Predicate<String>() { @Override public boolean test(String t) { // System.out.println(t); return t.startsWith(nextLine); } }; } } public class Solution1 { private static final Scanner scanner = new Scanner(System.in); @SuppressWarnings("unchecked") public static void main(String[] args) { List<String> names = Arrays.asList( "aaryanna", "aayanna", "airianna", "alassandra", "allanna", "allannah", "allessandra", "allianna", "allyanna", "anastaisa", "anastashia", "anastasia", "annabella", "annabelle", "annebelle" ); names.stream().filter(Filter.nameStartingWithPrefix(scanner.nextLine())) .map(Mapper.getDistinctCharactersCount()).forEachOrdered(System.out::println); } }
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.