java 2
Von Oracle Certified Professional Java SE 8 Programmer 2 Study guide - Jeanne Boyarsky and Scott Selikoff
Von Oracle Certified Professional Java SE 8 Programmer 2 Study guide - Jeanne Boyarsky and Scott Selikoff
Set of flashcards Details
Flashcards | 500 |
---|---|
Language | Deutsch |
Category | Computer Science |
Level | University |
Created / Updated | 03.03.2020 / 22.06.2022 |
Weblink |
https://card2brain.ch/box/20200303_java_2
|
Embed |
<iframe src="https://card2brain.ch/box/20200303_java_2/embed" width="780" height="150" scrolling="no" frameborder="0"></iframe>
|
Was sind die Outputs hier?
BinaryOperator<Integer> op = (a, b) -> a * b;
Stream<Integer> empty = Stream.empty();
Stream<Integer> oneElement = Stream.of(3);
Stream<Integer> threeElements = Stream.of(3, 5, 6);
empty.reduce(op).ifPresent(System.out::print);
oneElement.reduce(op).ifPresent(System.out::print);
threeElements.reduce(op).ifPresent(System.out::print);
- Kein Output
- 3
- 90
Was gibt es für Signaturen für reduce? Welche wird für paralell streams verwendet?
- T reduce(T identity, BinaryOperator<T> accumulator)
- Optional<T> reduce(BinaryOperator<T> accumulator)
- <U> U reduce(U identity, BiFunction<U, ? super T, U> accumulator, BinaryOperator<U> combiner)
Die letzte für parallel streams
Wie wird collect() bei streams auch noch genannt?
mutable reduction
Was gibt es für Signaturen bei collect?
<R> R collect(Supplier<R> supplier, BiConsumer<R, ? super T> accumulator,
BiConsumer<R, R> combiner)
<R,A> R collect(Collector<? super T, A,R> collector)
Was passiert hier?
Stream<String> stream = Stream.of("w", "o", "l", "f");
StringBuilder word = stream.collect(StringBuilder::new,
StringBuilder::append, StringBuilder:append)
- erster parameter: objekt das die ergebnisse speichert
- zweiter: nimmt zwei parameter, returned nichts, verantwortlich dafür ein weiteres Element der Colletion hinzuzufügen
- dritter: nimmt zwei collections und merged sie
!!!! Würde für StringBuilder funktionieren wenn uns die Reihenfolge der Buchstaben egal ist
Was passiert hier? Wie könnte das noch geschrieben werden?
Stream<String> stream = Stream.of("w", "o", "l", "f");
TreeSet<String> set = stream.collect(TreeSet::new, TreeSet::add,
TreeSet::addAll);
System.out.println(set);
- supplier erzeugt leeres TreeSet, Accumulator fügt einen String vom Stream zum TreeSet hinzu, Combiner fügt alle Elemente eines TreeSets einem anderen hinzu falls die Operations parallel ausgeführt werden.
- TreeSet<String> set = stream.collect(Collectors.toCollection(TreeSet::new));
System.out.println(set); - Set<String> set = stream.collect(Collectors.toSet());
System.out.println(set);
Was sind intermediate Operations?
Händeln infinite streams und returnen einen infinite stream, z.B. filter(), distinct(),limit(), skip(), map(), flatMap(), sorted(), peek()
Wie ist die Signatur von stream.filter()?
Stream<T> filter(Predicate<? super T> predicate)
Was macht Stream.distincit() ? Wie ist die Signatur?
Returned stream ohne duplicated Values, verwendet equals
Stream.distinct()
Was machen Stream.limit() und skip()? Wie ist die signatur?
Können finite stream kleiner machen oder einen finite stream aus einem infinite stream machen
Stream<T> limit(int maxSize)
Stream<T> skip(int n)
Was macht map bei streams? Wie ist die Signatur?
One-to-one-mapping von elementen im stream zu elementen des nächsten Schrittes im Stream
<R> Stream<R> map(Function<? super T, ? extends R> mapper)
Was macht Stream.flatMap()? Wie ist die Signatur?
Nimmt jedes Element im Stream und macht es zu einem Element in einem Stream (z.B. um mehrere Streams zu einem zusammenzufügen), removed leere elemente
<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper)
Was passiert hier?
List<String> zero = Arrays.asList();
List<String> one = Arrays.asList("Bonobo");
List<String> two = Arrays.asList("Mama Gorilla", "Baby Gorilla");
Stream<List<String>> animals = Stream.of(zero, one, two);
animals.flatMap(l -> l.stream()).forEach(System.out::println);
Aus den Streams wird einer zusammengebaut und der ausgegeben, der leere weggeschmissen.
Ausgabe also Bonobo, Mama Gorilla, Baby Gorilla
Was macht sorted bei Streams? Wie ist die Methodsignatur?
Sortiert nach natural ordering solange wir keinen comparator angeben:
Stream<T> sorted()
Stream<T> sorted(Comparator<? super T> comparator)
Was ist der Unterschied zwischen den beiden sorted-aufrufen?
Stream<String> s = Stream.of("brown bear-", "grizzly-");
s.sorted(Comparator.reverseOrder())
.forEach(System.out::print);
s.sorted(Comparator::reverseOrder);
Der zweite compiled nicht!
Das Comparator interface implementiert eine Methode die zwei Strings nimmt und ein int returned. Comparator::reverseOrder nimmt aber keinen parameter und returned einen Comparator!
Was mavht Stream.peek()? Was ist die Signatur?
Ändert den Stream nicht, erlaubt uns eine operation auf dem Stream auszuführen, bspw:
long count = stream.filter(s -> s.startsWith("g")).peek(System.out::println).count();
Stream<T> peek(Consumer<? super T> action)
Was bedeutet destructive to stream? Wenn man streams printen möchte, was sind destruktive möglichkeiten, was nicht?
Bedeutet dass man den Stream nicht mehr nutzen kann nach der Operation.
Destruktive Printmöglichkeiten:
- s.forEach(System.out::println);
- System.out.println(s.collect(Collectors.
toList())); - s.limit(5).forEach(System.out::println);
Non destruktive:
- s.peek(System.out::println).count();
Wie kann man das noch anders schreiben?
Stream<Integer> stream = Stream.of(1, 2, 3);
System.out.println(stream.reduce(0, (s, n) -> s + n));
Stream<Integer> stream = Stream.of(1, 2, 3);
System.out.println(stream.mapToInt(x -> x).sum());
Wie kann man den Durschnitt von einem intstream berechnen? (bspw. 1,2,3)
IntStream intStream = IntStream.of(1,2,3);
OptionalDouble avg = intStream.average();
System.out.println(avg.getAsDouble());
Was gibt es für primitveStreams?
- IntStream : für int, short, byte, char
- LongStream: für long
- DoubleStream: für double, float
Was gibt es für Möglichkeiten einen infinite DoubleStream zu erstellen?
DoubleStream.generate(Math::random)
DoubleStream.iterate(.5, d -> d/2);
Wie kann man einen IntStream erstellen der von 1-5 zählt?
IntStream.range(1,6) (6 nicht inkluded)
Wie kann man einen IntStream erstellen von 1-5 invluded 5?
IntStream.rangeClosed(1,5)
Wie kann man zwischen Streams mappen? (bspw. Stream zu DoubleStream)
- map (Stream -> Stream, DoubleStream->DoubleStream, ..)
- mapToDouble
- mapToInt
- mapToLong
- mapToObject (zu stream)
Wie funktionieren die mapperfunctions bei Streams (z.B. doubleStream -> intStream)
Mit einer mappingfunction, bspw.:
Stream<String> objStream = Stream.of("penguin", "fish");
IntStream intStream = objStream.mapToInt(s -> s.length())
Was ist der Unterschied zwischen OptionalDouble und Optional<Double>?
OptionalDouble ist für primitives und Optional<Double> für die Wrapper klasse
Was sind die Unterschiede bei den Funktionen von OptionalDouble zu einem "normalen" Optional?
get() heißt getDouble()
.orElseGet() nimmt einen DoubleSupplier statt einem Supplier als PArameter
Was ist das Ergebnis von DoubleStream.sum(..) wenn der Stream empty ist?
Wenn stream empty dann 0, kein Optional wie bei den anderen Funktionen
Kann man mehrere terminal Operations auf einem Stream laufen lassen?
Nein, immer nur eine
Wie bekomt man eine SummaryStatistik von streams? Was ist das?
bspw. IntSummaryStatistics stats = ints.summaryStatistics();
Bündelt Berechnungen wie minimum,maximum, average, size, anzahl Elemente im stream
-> stats.getMax(),..
Was für Methoden hat ein BooleanSupplier? Beispiel dafür
boolean getAsBoolean();
BooleanSupplier b1 = () -> true;
BooleanSupplier b2 = () -> Math.random() > .5;
System.out.println(b1.getAsBoolean());
System.out.println(b2.getAsBoolean());
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat DoubleSupplier/IntSupplier/LongSupplier?
0 params, Returntype double/int/long, single abstract method getAsDouble/getAsInt/getAsLong
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat DoubleConsumer/IntConsumer/LongCounsumer?
1 Param(double/int/long), returntype void, single abstract method accept
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat DoublePredicate/IntPredicate/LongPredicate?
1 Param (double/int/long), returntype boolean/ single abstract method test
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat DoubleFunction<R>/IntFunction<R>/LongRunction<R>?
1 param(double/int/long), Returntype R, single abstract method apply
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat DoubleUnaryOperator/IntUnaryOperator/LongUnaryOperato?r
1 Param (double/int/long), returntype double/int/long, single abstract method applyAsDouble/applyAsInt/applyAsLong
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat DoubleBinaryOperator/IntBinaryOperator/LongBinaryOperator?
2 params(double,double/int,int/long,long), returntype double/int/long, single abstract method applyAsDouble/applyAsInt/applyAsLong
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat ToDoubleFunction<T>/ToIntFunction<T>/ToLongFunction<T> ?
1 Param (T), return type double/int/long, Single abstract method applyAsDouble/applyAsInt/applyAsLong
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat ToDoubleBiFunction<T,U>/ToIntBiFunction<T,U>/ToLongBiFunction<T,U>?
2 Params (T,U), return type double/int/long, single abstract method applyAsDouble/applyAsInt/applyAsLong
Wieviele Parameter, was für einen ReturnType, was für single abstract Methods hat DoubleToIntFunction/DoubleToLongFunction/IntToDoubleFunction/IntToLongFunction/LongToIntFunction?
1 param(double/int/long), return type int/long/double, Single abstract method: applyAsInt/applyAsLong/applyAsDouble