fdsa


Kartei Details

Karten 496
Sprache Deutsch
Kategorie Informatik
Stufe Andere
Erstellt / Aktualisiert 06.12.2020 / 24.01.2021
Weblink
https://card2brain.ch/box/20201206_java_2_3
Einbinden
<iframe src="https://card2brain.ch/box/20201206_java_2_3/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>

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

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