Collections Flashcards

1
Q

Syntaxe vytvoreni String pole
prazdne
hodnotami (2)

A

String[] array = new String[2];
String[] array = new String[] {“a”, “b”};
String[] array = {“a”, “b”};

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Prazdne pole ma po vytvoreni v komurkach co?

A

defaultni hodnoty

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Syntaxe vytvoreni dvourozmerne matice Byte
prazdne
hodnotami

A

Byte[][] matrix = new Byte[2][3]; // radek, sloupec
Byte[][] matrix = {{1,2,3},{2,3}} // 2 radky, 3 sloupce, posledni hodnota je 0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Metody kopirovani z pole do pole (3)

A

System.arraycopy(source, sposition, destination, dposition, length)
var x = Arrays.copyOf(source, position)
var x = Arrays.copyOfRange(source, position, length)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Jak seradit prvky v poli
Jak seradit prvky v kolekci

A

Arrays.sort(array, comparator)
Collections.sort(collection, comparator)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Jak se kompilator chova k generics

A

Smaze je - nahradi za Object. Ale kde se pouzije hodnota, pretypuje se na spravny typ.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Wildcards:
neznamy typ
nadtyp (nebo stejny) - lowerbound wc.
podtyp (nebo stejny) - upperbound wc.
- syntax + ktery z nich definuje read only kolekci?

A

List<?>
List<? super X>
List<? extends X>

List<?>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

List<Product> products = new ArrayList<Food>(); projde?</Food></Product>

A

ne, collections jsou invariantni - typ musi sedet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Product[] products = new Food[]; projde?

A

ano, arrays jsou covariantni - typ muze byt urcitejsi

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Ifc kolekce pro oboustrannou frontu + impl (3)
Jaka je impl. velikost?

A

Deque<T> d = new ArrayDeque<>();
new ArrayDeque<>(list);
new ArrayDeque<>(20);
16</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Oboustranna fronta - metody:
insert dopredu/dozadu
get odpredu/odzadu
get&remove odpredu/odzadu

A

offerFirst, offerLast
peekFirst, peekLast
pollFirst, pollLast

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Pristup oboustranne fronty k null

A

Null hodnoty nelze vkladat
Gettery vraci null, pokud je kolekce prazdna

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Kolekce key,value ifc+impl (3)
Jaka je implicitni velikost?

A

Map<T> m = new HashMap<>();
new HashMap<>(100);
new HashMap<>(otherMap);</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Iterator na kolekci:
podminky, vytvoreni
jak zjistim, vratim, odstranim prvek?

A

implements Iterable
coll.iterator()
hasNext(), next(), remove()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Jaka je podminka kolekce, aby mohla lezt do foreach

A

implements Iterable

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Seznam - ifc+impl (3)
Jaka je impl. velikost

A

List<T> l = new ArrayList<>();
new ArrayList<>(100);
new ArrayList<>(set);</T>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Metoda pro vytvoreni immutable seznamu, pokud mas prvky

A

List.of(a, b, c);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Metoda pro vytvoreni mutable seznamu, pokud mas prvky (mohou byt v poli)

A

Arrays.asList(a, b)
Arrays.asList(a[])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Seznam metody:
pridej a posun (2p), pridej a prepis prvek
odstran (2p)
obsahuje prvek
jakou ma prvek pozici

A

add(T), add(index, T), set(index, T)
remove(index), remove(T) - prvni nalezeny
contains(T)
indexOf(T) - pokud neni, vraci -1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

HashSet
co je dobre pro vykon
co naopak ne
je thread safe?

A

+ rovnomerne rozdeleni hashu
- nadstrelena kapacita
nema, to ma SynchronizedSet

21
Q

Load faktor na kolekci
Defaultni pro HashSet

A

O kolik se kapacita zvetsi pri pridani prvku nad
0.75

22
Q

Set - ifc+impl (4)
Implicitni kapacita

A

Set<T> s = new HashSet<>();
new HashSet<>(100);
new HashSet<>(100, 0.75);
new HashSet<>(coll);
16</T>

23
Q

Metoda vytvoreni immutable mnoziny z prvku

A

Set.of(a, b, c)

24
Q

Set - metody
pridej, odeber, zjisti

A

add, remove, contains

25
Q

Metody menici poradi prvku v kolekci (3)

A

Collections.sort(c)
Collections.revers(c)
Collections.shuffle(c)

26
Q

Metoda vyhledani prvku v kolekci

A

Collections.binarySearch(c, element)

27
Q

Metoda naplneni pole stejnymi hodnotami

A

Collections.fill(c, value)

28
Q

Metoda pro odstraneni prvku nesplnujicich podminku z kolekce

A

collection.removeIf(predicate)

29
Q

Metoda ulozeni kolekce do pole

A

collection.toArray(array)

30
Q

Implementace mnoziny (4)

A

HashSet, TreeSet, SortedSet, SynchronizedSet

31
Q

Kolekce maji/nemaji thread safe operace?

A

Maji

32
Q

Metody pro praci s polem:
vyplneni hodnotou
nalezeni prvku
jsou pole stejna
serad(2)

A

Arrays.fill(array, start, end, value)
Arrays.binarySearch(array, value)
Arrays.equals(a, b)
Arrays.sort(array), Arrays.sort(array, comparator)

33
Q

Priklad implementace komparatoru

A

public class MyComp<T> implements Comparator {
public int compare(T t1, T t2) {
return t1.compareTo(t2);
}
}</T>

34
Q

Vyhodnocovani v array, co se vyhodnoti driv
a[(a=b)[2]

A

Nejdriv vnejsi a, potom vnitrek

35
Q

Jak se da provest akce nad mapou
- pomoci lambdy

A

map.forEach((k, v) -> action(k,v));

36
Q

Jak se da provest akce nad listem
- pomoci existujici metody

A

list.forEach(SomeClass::method);

37
Q

Co se stane, pokud vytvorim: Set.of(1,2,2,3)

A

Runtime error
Set je immutable a nemuze obsahovat stejne prvky

38
Q

Generics pro potomky tridy

A

<T>
</T>

39
Q

Je Comparator functional interface?

A

Jo

40
Q

retezeni komparatoru sekvencne - syntax

A

comparator1.thenComparing(comparator2)

41
Q

uprava komparatoru:
zachazeni s nully (2)
obracene poradi

A

C.nullsFirst(c);
C.nullsLast(c);
c.reversed();

42
Q

Klonovani
- podminka (+trest)
- syntax
- jaka trida ma implementaci clone
- co dela

A

Trida implementuje Cloneable (vyjimka pri klonovani)
Product p2 = (Product) p1.clone();
Object
Kopii objektu na heapu

43
Q

Definuj predicate nad Stringem ze neni empty

A

Predicate<String> nonEmpty = (x) -> !x.isEmtpy();</String>

44
Q

jak funguje list.add(index, element)

A

Prida prvek na index a posune zbytek doprava.
Element
- zkopiruje do sebe, pokud je to primitiv
- zkopiruje! do sebe referenci, pokud je to objekt
(pozor u GC, tady se drzi druha reference)

45
Q

Metoda, ktera pro dany klic K nahradi hodnotu, pokud je rovna V, za V1

A

map.replace(K, V, V1)

46
Q

Vytvor komparator podle length (comparing

A

Comparator.comparing((x) -> x.length())

47
Q

Vytvor komparator podle length (comparing

A

Comparator.comparing((x) -> x.length())

48
Q

Jak vytvorim map pro paralelni zpracovani, load factor 0.8, kapacita 5 a concurrency level 15 (max pocet vlaken)

A

new ConcurrentMap(5, 0.8f, 15)

49
Q

Jak zapisu seznam do arraye

A

var myArray = list.toArray(someArray);
pokud se vejde, vratim puvodni array, jinak novy