Help With Filters Java Programming
I am new to java and do not understand how to use filters very well. I need to write a code that determines if an integer is not divisible by some value. A filter also contains an upstream filter so that this filter can be part of a chain of filters. please help me get started. thanks!
Re: Help With Filters Java Programming
What do you mean when you use the word "filter"? Is it the standard English meaning?
Or are there some specific java classes you are talking about?
Re: Help With Filters Java Programming
What do you mean by filter? I would use if-then statements from what you're making the program
Re: Help With Filters Java Programming
I assume he is referring to the new Java 8 filters.
What a filter does is takes a stream of objects and checks for the condition you gave it. If the condition is true, it will be returned in the new stream.
Here is a simple example
Code java:
List<Integer> list = new ArrayList<>();
int[] nums = { 8, 9, 2, -1, 10, 3, 23, 13, 14, 28 };
for (int n : nums)
list.add(n);
Stream<Integer> evens = list.stream().filter(n -> n % 2 == 0);
The above code will find all even values in the list and add them to the new stream evens.
Alternatively, you could have it return a list at the end of everything.
Code java:
List<Integer> evens = list.stream().filter(n -> n % 2 == 0).collect(Collectors.toList());