Chapter 14: Generics and Collections Flashcards
What is a method reference?
An operation that looks like this:
System.out::println;
Given the following statement, write an equivalent statement using method reference.
Consumer> lambda = x -> Collections.sort(x);
Consumer> methodRef = Collections::sort;
Given the following lamda expression, write an equivalent method reference.
var str = "abc"; s -> str.startsWith(s);
var str = "abc"; str::startsWith;
Which functional interface takes two parameters and returns a boolean?
BiPredicate
What is a constructor reference?
A constructor reference is a special type of method reference that uses new instead of a method, and it instantiates an object. It is common to use a Supplier.
example
Supplier> methodRef = ArrayList::new;
Which functional interface takes one parameter and returns a result? (not a boolean).
Function
How do you convert a boolean to its wrapper class Boolean?
using Boolean.valueOf();
How do you convert a byte with variable name x to its wrapper class Byte?
using Byte.valueOf((byte)x);
How do you convert a short with variable name x to its wrapper class Short?
using Short.valueOf((short)x);
How do you convert a float to its wrapper class Float?
using Float.valueOf();
What will this code produce?
var heights = new ArrayList(); heights.add(null); int h = heights.get(0); System.out.println(h);
It will throw a NullPointerException when trying to assign a null value to a primitive.
What does the following code output?
List numbers = new ArrayList<>(); numbers.add(1); numbers.add(Integer.valueOf(3)); numbers.add(Integer.valueOf(5)); numbers.remove(1); numbers.remove(Integer.valueOf(5)); System.out.println(numbers);
It prints [1] because the remove() method is overloaded.
The first remove() call removes the object at index 1.
The second remove() call removes the object that should be removed.
How can we use the diamond operator to make the following code shorter?
List numbers = new ArrayList();
List numbers = new ArrayList<>();
How can we use the diamond operator to make the following code shorter?
List< Object> numbers = new ArrayList< Object>();
List< Object> numbers = new ArrayList<>();
What is the generic type of the following statement?
var list = new ArrayList<>();
Object
What are the four main interfaces in the Java Collections Framework?
- List
- Set
- Queue
- Map
What is a List?
A list is an ordered collection of elements that allows duplicate entries. Elements in a list can be accessed by an int index.
What is a Set?
A set is a collection that does not allow duplicate entries.
What is a Queue?
A queue is a collection that orders its elements in a specific order for processing. A typical queue processes its elements in a first-in, first-out orderm but other orderings are possible.
What is a Map?
A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs.
What interfaces/data structures extend the Collection interface? And which one does not?
Interfaces that extend Collection:
- List
- Queue
- Set
Interface that does not extend Collection:
- Map
How do we insert a new element into a Collection and what does it return?
Using the add() method.
It returns true, if the operation was succesful.
How do we remove a new element into a Collection and what does it return?
Using the remove() method.
It returns true, if the operation was succesful.
Is the following code valid?
Collection birds = new ArrayList<>();
birds. add(“hawk”);
birds. add(“hawk”);
birds. add(“hawk”);
for (String bird : birds) {
birds.remove(bird);
}
No. Java does not allow removing elements from a list while using the enhanced for loop.
It will throw a ConcurrentModificationException
(More on this in chapter 18).