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).
How do we discard all elements of a Collection?
using the clear() method.
How do we remove an element using a condition?
With the removeIf() method, which takes a Predicate and returns true/false.
How do we loop through a Collection (without using a for/while loop)?
Using the method forEach() which takes a Consumer.
When do you want to use a List?
When you want an ordered list that can contain duplicate entries.
What is the main benefit of using an ArrayList?
- It resizes automatically
- You can look up any element in constant time.
What data structure is best if you are reading more often than (or the same amount as) writing?
ArrayList
What is the main benefit of using an ArrayList?
- You can access, add and remove from the beginning and end of the list in constant time.
What data structure is best if you want to use one as a queue?
Linkedlist
What factory methods exist to create a List?
- Arrays.asList(varags)
- List.of(varargs)
- List.copyOf(collection)
Which factory methods for creating a list return an immutable list?
- List.of(varargs)
- List.copyOf(collection)
Is it possible to add an element to a list created by Arrays.asList(varargs)?
No.
Is it possible to replace an element to a list created by Arrays.asList(varargs)?
Yes.
Is it possible to remove an element in a list created by Arrays.asList(varargs)?
No.
What is the result of the following code?
String[] array = new String[] {“a”, “b”};
List asList = new Arrays.asList(array);
array[0] = “c”;
System.out.println(asList);
[c, b]
What is the result of the following code?
String[] array = new String[] {“a”, “b”};
List asList = Arrays.asList(array);
asList.set(0, “x”);
System.out.println(Arrays.toString(array));
[x, b]
What is the result of the following code?
String[] array = new String[] {“a”, “b”};
List asList = Arrays.asList(array);
asList.add(0, “x”);
System.out.println(asList);
This code throws an UnsupportedOperationException because Arrays.asList returns a fixed sized list.
What is the result of the following code?
List asList = Arrays.asList();
asList.set(0, “x”);
System.out.println(asList);
It throws a IndexOutOfBoundsException.
Because there are no elements in the list, there are no elements to replace.
Given the following list, what List method can we use to multiply all numbers by 2?
List numbers = Arrays.asList(1, 2, 3);
numbers.replace(x -> x * 2);
What (often-used) implementations of Set exist?
- HashSet
- TreeSet
How does a HashSet store elements?
A HashSet stores its elements in a hash table, which means the keys are a hash and the values are an Object. This means that it uses the hashCode() method of the objects to retrieve them more efficiently.
What is the main benefit of a Set?
Adding elements and checking wether an element is in the set both have constant time.