Collections, Streams and Filters Flashcards
What is a predicate and where can we use it?
Predicate is an expression that evaluates to a boolean value, often used for filtering operations. This interface has a single abstract method
boolean test(T t)
removeIf is a method of which class?
Collection
how can we use the removeIf method?
Passing a Predicate argument
boolean removeIf(Predicate super {{E}} filter)
What does the filter method returns?
Returns a stream consisting of elements that match the specified predicate
Stream filter(Predicate super {{T}} predicate)
What is the Consumer interface represent?
An operation
What does the Consumer interface accepts and returns and where is it used?
Accepts a single input argument and returns no result. Used in forEach methods in Streams and List
void accept(T t)
as it it a functional interface, can be represented by a lambda expression
The Stream.forEach takes which parameter?
void forEach(Consumer super {{T}} action)
this method is a terminal operation, consuming the stream. Behaviour non-deterministic
From which class is the forEach method in List interface inherited from?
The Iterable interface, and it is a default method
What is the difference between the forEach method in List and Stream?
Stream is nondeterministic while the forEach of List is deterministic.
Their method signature is the same
void forEach(Consumer super {{T}} action)
How can a Stream be created?
Stream method can be created by a static method in Stream interface, or by a stream creation method invoked on a data source. Stream can only be operated once
The source should not be modified while the stream si being processed
What is the stream pipeline comprised of?
Comprised of a data source (method such as of() and generate()), intermediate operations(map, filter, distinct, sorted) and a single terminal operation(sum, collect, forEach, reduce)
What does intermediate operations returns?
They return a new stream, these operations are all lazy and only triggered when the terminal operation is initialized
Stream stream = Stream.of(“Whizlabs”);
stream.forEach(s -> sout(s)).forEach(s -> sout(s)
what happens when the code is compiled and executed
compilation fails because the forEach method cannot be chained
Stream stream = Stream.of(“Whizlabs”);
stream. filter(s -> s.startsWith(“ J”));
stream. forEach(s -> System.out.println(s));
What happens when this code is compiled and executed?
An exception is thrown because once you use the stream, it cannot be re-used. For it to work you would have to chain the foreEach method or re-declare the stream ex.
stream = stream.filter(s -> s.startsWith(“ J”));
public class Data { private int var; public Data(int var) { this.var = var; } public int getVar() { return var; } }
Collection collection = new ArrayList();
collection. add(new Data(1));
collection. add(new Data(2));
collection. removeIf(d -> d.getVar() % 2 == 0);
what is the output of this code?
When a collection is created with raw types, its elements are considered to be of type Object, hence “d” does not have the getVar() method and it fails to compile
to compile it would have to be:
Collection collection = new Arraylist<>()
When to use a reference method?
a reference method can be used in the place of a lambda expression if the expression body does nothing but invoke an existing method
syntax:
::