java 2 3
fdsa
fdsa
-
- 1 / 496
-
Flashcards
What is the Collector and whats the return value for creating a map using functions to map the keys, values, an optional merge function and an optional map type supplier?
Map toMap(Function k, Function v)
Map toMap(Function k, Function v, BinaryOperator m)
Map toMap(Function k, Function v, BinaryOperator m, Supplier s)
What happens here? How To fix?
var ohMy = Stream.of("lions", "tigers", "bears";
Map<Integer, String> map = ohMy.collect(Collectors.toMap(String::length, k-> k));
java.lang.IlllegalStateException: Duplicate key 5 (two have the same length)
var ohMy = Stream.of("lions", "tigers", "bears"M
Map<Integer, Stream> map = ohMy.collect(Collectors.toMap(String::length, k->k, (s1,s2) -> s1 + "," + s2));
Could the function you call in groupingBy return null?
no, it does not alllow null keys
How to change the type of the map returned by Collectors.groupingBy?
F.e.:
Collectors.groupingBy( String:: length,
TreeMap::new, Collectors.toSet());
How to change the type of map returned by partitioningBy?
We cannot change the type returned by parititionBy?
How to add a second collector?
With Collectors.mapping, F.e.
Collectors.groupingBy( String::length,
Collectors.mapping( s-> s.charAt(0), Collectors.minBy((a,b)->a-b))));
Is it possible to use multiple parameters with intance method references?
Yes
What happens here?
var heights = new ArrayList<Integer>();
heights.add(null);
int h = heights.get(0);
NullPointerException -> we try to unbox null into a int primitive
Are duplicates allowed in lists?
yes
What happens with remove() on a list and a index passed which does not exists?
IndexOutOfBoundsException
When is ArrayList a good choice?
when you are reading more often than(or the same amount as) writing to the ArrayLList
When is LinkedList a good choice?
when yo will be using it as a queue
What are the factorymethods to create a list?
- Arrays.asList(varargs)
- List.of(varargs)
- List.copyOf(collection)
Is it possible to add / replace / delete elements on a list which is created by Arrays.asList(varargs)?
No, Yes, No
Is it possible to add / replace / delete elements on a list which is created by List.of(varargs)?
No, No, No
Is it possible to add / replace / delete elements on a list which is created by List.copyOf(collection)
No, No, No
Is Map.of with two or more key-value-pairs possible?
Yes but key and value per set has to exist.
F.e. Valud: Map.of("key1","value1","key2","value2");
invalid: Map.of("key1","value1", "key2");
Can the type List contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
Yes / Yes / No / No
Can the type Map contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
Yes / No / Yes / No
Can the type queue contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
Yes / Yes / No / Yes
Can the type Set contain duplicate elements / elements always ordered / has keys and values / must add/remove in specific order?
No / No / No / No
Is a type ArrayList sorted? / calls hashcode? / calls compareTo?
No / No / No
Is a type HashMap sorted? / calls hashcode? / calls compareTo?
No / Yes / No
Is a type HashSet sorted? / calls hashcode? / calls compareTo?
No / Yes / No
Is a type LinkedList sorted? / calls hashcode? / calls compareTo?
No / No / No
Is a type TreeMap sorted? / calls hashcode? / calls compareTo?
Yes / No / Yes
Is a type TreeSet sorted? / calls hashcode? / calls compareTo?
Yes / No / Yes
Sorting: Whats the order (numbers/letters, upercases)?
numbers before letters, uppercase before lowercase
When can classes be used for datastructures that require comparision?
if it implements comparable
When to use a comparator?
to specify that you want to use a different order than the object itslefs provides
Whats the signature of Comparable-interface?
public interface Comparable<T> {
int compareTo( T o);
}
When is a Cast needed within the compareTo-method?
when dealing with legacy code or code that does not use generics
When implementing compareTo, where do we have to pay attention to?
keeping compareTo and equals consistent
Can comparable or comparator be used without a import statement?
Comparator yes, comparable not
Whats the package name, whats the method name, whats the number of parameters, has the interface to be implemented by class compaing in class Comparable?
java.lang / compareTo() / 1 / Yes
Whats the package name, whats the method name, whats the number of parameters, has the interface to be implemented by class compaing in class Comparator?
java.util / compare() / 2 / no
Whats the static comparator-method for comparing by results of a function that returns any object?
comparing(function)
Whats the static comparator-method for comparing by the results of a function that returns a double?
comparingDouble(function)
Whats the static comparator-method for comparing by the results of a function that returns an int?
comparingInt(function)
Whats the static comparator-method for comparing by the results of a function that returns a long?
comparingLong(function)