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:
::
What are the four kinds of reference methods?
- Reference to static method
- Reference to an instance method of an arbitrary object of a particular type
- Reference to an instance method of a particular object
- Reference to a constructor
What is the difference between a static reference method and a Instance method of an arbitrary Object?
Reference to a static method, the current object is passed as an argument to the method
Reference to an instance method of an arbitrary object of a particular type, method is called on the current object
What are the three ways of creating ArrayList objects?
ArrayList() -> constructs an empty list with the initial capacity of ten
ArrayList(int initialCapacity) -> constructs an empty list with the initial capacity
ArrayList(Collection extends E) c) -> constructs a list containing elements of the specified collection, in order they are returned by the collections iterator
In which order is the TreeSet implemented?
Are ordered using their natural ordering or by a Comparator provided at creation time, depending on which constructor is used
About the TreeSet class, what does these methods do?
ceiling
floor
headSet
tailSet
TreeSet treeSet = new TreeSet<>(); treeSet.add(1); treeSet.add(2); treeSet.add(5); treeSet.add(6);
ceiling: returns the least element in the set greater than or equal to the specified element.
Ex. treeSet.ceiling(4); // 5
floor: returns the greatest element in the set less than or equal to the specified element
Ex. treeSet.floor(4); //2
headSet: returns a view of the portion of the set whose elements are less than the specified element
treeSet.headSet(4) // 1,2
tailSet: returns a view of the portion of the set whose elements are greater than the specified element
treeSet.tailSet(4) // 5,6
About the ArrayDeque methods:
What does:
1) peek*
2) pool*
3) offer*
- can be first or last
do?
1) retrieves but does not remove the first/last element of the deque
2) retrieves and removes the first/last element of the deque
3) inserts the specified element at the beginning or the end of the deque
Queue{Integer} queue= new ArrayDeque{}()
queue.offer(10)
queue.offer(4)
queue = [10,4]
Queue{Integer} stack= new ArrayDeque{}() stack.push(10) stack.push(4) stack.push(5) queue = [5,4,10]
What is the method if the Comparable interface defines?
int compareTo(T object)
What is the method that the Comparator interface defines?
int compare(T object1, T object2)