java 2 3
fdsa
fdsa
-
- 1 / 496
-
Lernkarten
For which functions is the method valid and what is the method for returning an object containing numerous stream statistics such as the average, min, max, etc?
IntSummaryStatistics/LongSummaryStatistics/DoubleSummaryStatistics summaryStatistics()
IntStream/LongStream/DoubleStream
Whats the diffierence between OptionalDouble and Optional<Double>?
OptionalDouble is for primitives and Optional<Doubloe> for the double wrapper class
What are the optionals for primitives?
OptionalDouble, OptionalInt, OptionalLong
How could this be rewritten?
private static void threeDigit(Optional<Integer> optional) {
if(optional.isPresent() {
var num = optional.get();
var string = "" + num;
if(string.length() == 3)
System.out.println(string);
}
}
private static void threeDigiht(Optional<Integer> optional) {
optional.map(n -> ""+n)
.filter(s -> s.length() == 3)
.ifPresent(System.out::println);
}
How to flat two optionals?
optional.flatMap
What is the Collector and whats the return value for calculationg the average for our three core primitive types?
Double averagingDouble(ToDoubleFunction f)
Double averagingInt(ToIntFunction f)
Double averagingLong(ToLongFunction f)
What is the Collector and whats the return value for counting the number of elements?
Long counting()
What is the Collector and whats the return value for creating a map grouping by the specified function with the optional map type spplier and optional downstream collector?
Map<K, List<T>> groupingBy(Function f)
Map<K, List<T>> groupingBy(Function f, Collector dc)
Map<K, List<T>> groupingBy(Function f, Supplier s, Collector dc)
What is the Collector and whats the return value for creating a single string using cs as a delemiter between elements if one is specified?
String joinig(CharSequence cs)
What is the Collector and whats the return value for finding the largest/smallest elements?
Optional<T> maxBy(Coparator c)
Optional<T> minBy(Comparator c)
What is the Collector and whats the return value for adding another level of collectors?
Collector mapping(Function f, Collector dc)
What is the Collector and whats the return value for creating a map grouping by the specified predicate with the optional further downstream collector?
Map<Boolean, List<T>> partitioningBy(Predicate p)
Map<Boolean, List<T>> partitioningBy(Perdicate p, Collector dc)
What is the Collector and whats the return value for calculating average, min, max, ...
DoubleSummaryStatistics summarizingDouble(ToDoubleFunction f)
IntSummaryStatistics summarizingInt(ToIntFunction f)
LongSummaryStatistics summarizingLong(ToLongFunction f)
What is the Collector and whats the return value for calculating the sum for our three core primitive types?
Double summingDouble(ToDoubleFunction f)
Integer summingInt(ToIntFunction f)
Long summingLong(ToLongFunction f)
What is the Collector and whats the return value for creating an arbitrary tpye of liust or set?
List toList()
Set toSet()
What is the Collector and whats the return value for creating a collection of the specified type?
Collection toCollection(Supplier s)
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