Ch14 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;
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);
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 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
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.
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 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.
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.
Given the following list, what List method can we use to multiply all numbers by 2?
List numbers = Arrays.asList(1, 2, 3);
numbers.replaceAll(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.
What is the disadvantage of a Set?
You lose the order in which you inserted the elements.
What is the main benefit of a TreeSet?
It is always in sorted order.
What is the disadvantage of a TreeSet?
Adding and checking wether an element exists takes longer than with a HashSet, especially as the tree grows larger.