OCP - 1
Classes, Desing Patterns, Principles, Generics and Collections
Classes, Desing Patterns, Principles, Generics and Collections
Kartei Details
Karten | 124 |
---|---|
Sprache | English |
Kategorie | Informatik |
Stufe | Andere |
Erstellt / Aktualisiert | 31.01.2019 / 08.01.2020 |
Weblink |
https://card2brain.ch/box/20190131_ocp_1
|
Einbinden |
<iframe src="https://card2brain.ch/box/20190131_ocp_1/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
Lernkarteien erstellen oder kopieren
Mit einem Upgrade kannst du unlimitiert Lernkarteien erstellen oder kopieren und viele Zusatzfunktionen mehr nutzen.
Melde dich an, um alle Karten zu sehen.
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
What is the signature of Map.merge?
V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)
What does the Map.merge method do?
merge(key, value,function)
- If the the value for the given key is null or not present, insert the provided value.
- Otherwise, replace the value for the given key with the result of function
- function takes the value from the map as the first argument and the new value as the second one.
Map<String, String> leMap = new HashMap<String, String>();
String result;
result = leMap.merge("key", "v1", (k1, k2) -> k1 + k2);
result = leMap.merge("key", "v2", (k1, k2) -> k1 + k2);
result = leMap.merge("key", "v3", (k1, k2) -> k1 + k2);
System.out.println(result);
When defining an enum, where do the static method values() and valueOf() come from?
The compiler generates it on the enum class
When defining an enum, where do the methods name() and ordinal() come from?
All enums implicitly extend the enum in which these methods are defined
What is the Result of `null instanceOf X` ?
False, an instanceOf check with null always results in false
When are instanceOf checks executed?
- For Classes: At Compiletime
- For Interfaces: At Runtime
What is virtual method invocation?
It basically means that Jva will look at subclasses when finding the right method to call (override)
What's the goal of @override?
It is helpful because the compiler can tell you when you've messd up
What does the equals signature look like?
public boolean equals(Object obj)
What is the equals contract?
- "Should not change at random"
- reflexive: x.equals(x) == true
- symmetric: x.equals(y) == y.equals(x)
- transitive: x.equals(y) == y.equals(z) == z.equals(x)
- consistent: x.equals(y) == x.equals(y) (does not change when x and y are not modified)
- An object an null are not equal
- x.equals(null) must return false
What is the contract of hashCode?
- The result must not change -> derive it from members that do not change (eg. an id)
- The hasCode() can use a subset of equals
- two equal objects must return the same hashcode
- the hash code can be a constant, altough it is not efficient
What does the hashCode signature look like?
public int hashCode()
How is an enum declared?
By using the enum keyword instead of the class keyword
Are Enum values comparable with ==
yes, think of them as static final constants
What Methods does an enum provide?
- Static:
- MyEnum.values(): Returns an array containing the constants of this enum
- MyEnum.valueOf("string"): Create an enum from a string
- Instance:
- MyEnum.FIRST.name(): Returns the name of this enum constant, exactly as declared in its enum declaration
- MyEnum.FIRST.ordinal(): ordinal of this enumeration constant
What happens when an illegal string value is providee to the Enum valueOf method?
An IllegalArgumentException is Thrown
Can a class or enum extend an enum?
No
What needs special attention when implementing a switch statement with an enum
The enum type is implied in the case statements and it is not allowed to type it!
switch(myEnum){
case ONE: // NOT MyEnum.ONE
// ...
}
Can a constructor of an enum be public?
No, it wont' compile!
-
- 1 / 124
-