OCP - 1
Classes, Desing Patterns, Principles, Generics and Collections
Classes, Desing Patterns, Principles, Generics and Collections
Fichier Détails
Cartes-fiches | 124 |
---|---|
Langue | English |
Catégorie | Informatique |
Niveau | Autres |
Crée / Actualisé | 31.01.2019 / 08.01.2020 |
Lien de web |
https://card2brain.ch/box/20190131_ocp_1
|
Intégrer |
<iframe src="https://card2brain.ch/box/20190131_ocp_1/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
What's the Signature of the get Method of the Map-Interface?
V get(Object key)
What's the Signature of the put Method of the Map-Interface?
V put(K key, V value)
What's the Signature of the remove Method of the Map-Interface?
V remove(Object key)
What's the Signature of the keySet Method of the Map-Interface?
Set<K> keySet()
What's the Signature of the values Method of the Map-Interface?
Collection<V> values()
Hw can ou check if a map contains a value?
A contains method does not exist, but containsKey and contaionsValue
Which data structures do not allow null and why?
- Everything that is sortable as it does not make sense to compar sth. with null
- TreeSet
- TreeMap
- Everthing that works with special return values (eg. poll)
- ArrayDeque
- Legacy
- Hashtable
Which collection types can contain duplicate elements/values?
Which collection types keep the elements ordered?
Which of these Collection Classes implement the List Interface
Which of these Collection Classes implement the List Interface
Which of these Collection Classes implement the Queue Interface
Which of these Collection Classes implement the Queue Interface
Which of these Collection Classes are sorted (logically sorted - not ordered!)?
Which of these Collection Classes are sorted (logically sorted - not ordered!)?
Which of these Collection Classes accept null values?
Which of these Collection Classes accept null values?
In Strings, ___ sort before ___ and __ sort before ___.
In Strings, numbers sort before letters and uppercase letters sort before lowercase letters.
What is the signature of the single abstract Method of Comparable?
int compareTo(T o)
(NOTE THE NAME!)
What is the signature of the single abstract Method of Comparator?
int compare(T o1, T o2)
(NOTE THE NAME!)
What is the difference between Comparable and Comparator?
Comparator: Compares two Objects "from the outside", is often expressed as Lambda
Comparable: Compares a given Object with itself
What is the return contract of the Comparable method?
- 0 if the two objects are equal
- >0 if the current object is greater than the argument
- <0 if the current object is less than the argument
What is the return contract of the Comparator method?
- 0 if the two objects are equal
- >0 if the the first argument object is greater than the second argument
- <0 if the the first argument object is less than the second argument
How can you sort a List of Objects?
Using the Collections.sort(coll) utility method. Note that the enclosed type of the collection must implement the Comparable interface. Otherwise an exception is thrown at runtime.
Alternatively, a second Parameter can be passed: Collections.sort(coll, comparator), where comparator is an instance of the Comparator interface
In a Comparator or Comparable, a.id - b.id sorts in ___ order and b.id - a.id sorts in ___ order.
In a Comparator or Comparable, a.id - b.id sorts in ascending order and b.id - a.id sorts in descening order.
Do compareTo and equals have to be consistent?
The should be consistend if, and only if, x.equals(y) == true. In this case, x.compareTo(y) should return 0
In which package is Comparable?
java.lang
in which package is Comparator?
java.util
How can you search in a Collection of Objects?
Use Collections.binarySearch. Note that, as with Arrays.binarySearch, the given list must be sorted!
For which classes can TreeSet be used?
You can add only objects that implement the Comparable interface!
How can you build a Comparator over multiple fields?
Uing the Comparator static helper fuctions, eg:
Comparator<Squirrel> c = Comparator.comparing(s -> s.getSpices()).thenComparingInts(s -> s.getWeight));
What kind of Method-References exist?
- Static Methods
- Instance Methods on a particular instance
- Instance methods on an instance to be determined at runtime
- Constructors
What does a static method reference to Collections.sort look like?
Consumer<List<Integer>> methodRef1 = Colelctions::sort;
Consumer<List<Integer>> asLambda = l -> Colelctions.sort(l);
What does a method reference to the instance method str.startsWith look like?
Predicate<String> methodRef = str::startsWith;
Predicate<String> asLambda = s -> str.startsWith(s);
What does a method reference to the instance method isEmpty of a yet unknown instance look like?
Predicate<String> methodRef = String::isEmpty;
Predicate<String> asLambda = s -> s.isEmpty();
What does a method reference to the the constructor of ArrayList look like?
Supplier<ArrayList> constRef = ArrayList::new
Supplier<ArrayList> constRef = () -> new ArrayList();
what is the signature of removeIf? How does it behave?
boolean removeIf(Predicate<? super E> filter)
Iterates through the list and applies the Predicate to each element. If the Predicate returns true, it will be removed.
returns true if any elements were removed
what is the signature of replaceAll? How does it behave?
void replaceAll(UnaryOperator<E> o)
Calls the UnaryOperator for every Element and replaces the value with the returned value
what is the signature of forEach?
void forEach(Consumer<? super E> action)
What are new methods on Map in Java8?
- putIfAbsent
- merge
- computeIfPresent
- computeIfAbsent