List<E> subList(int fromIndex, int toIndex)
Collection
Podívejme se na potomky třídy Collection
, konkrétně:
rozhraní List
, implementace:
ArrayList
LinkedList
rozhraní Set
, implementace:
HashSet
List
int
List<E> subList(int fromIndex, int toIndex)
ArrayList
LinkedList
Kontejnery ukladají pouze jeden typ, v tomto případě String .
|
Operace nad ArrayList vs LinkedList
add 100000 elements | 12 ms | 8 ms remove all elements from last to first | 5 ms | 9 ms add 100000 elements at 0th position | **1025 ms** | 18 ms remove all elements from 0th position | **1014 ms** | 10 ms add 100000 elements at random position | 483 ms | **34504 ms** remove all elements from random position | 462 ms | **36867 ms**
ArrayList()
ArrayList(int initialCapacity)
ArrayList(Collection<? extends E> c)
c
Kapacita reprezentuje interní kapacitu, neznamená to počet null prvků v nové kolekci!
|
Statické "factory" metody:
List.of(elem1, elem2, …)
Set.of, Map.of
List<String> modifiableList = new ArrayList<>(List.of("Y", "N"));
List
kolekci Collection
?// change the type, it is its superclass
Collection<Long> collection = list;
Collection
seznam List
?// create new list
List<Long> l = new ArrayList<>(collection);
List
IRozhraní List
dědí od Collection
.
Kromě metod v Collection
obsahuje další metody:
E get(int index)
IndexOutOfBoundsException
je-li mimo rozsah
E set(int index, E element)
index
prvkem element
List
II
void add(int index, E element)
E remove(int index)
int indexOf(Object o)
o
int lastIndexOf(Object o)
pro index posledního výskytuList<String> list = new ArrayList<>();
list.add("A");
list.add("C");
list.add(1, "B");
// ["A", "B", "C"]
list.get(2); // "C"
list.set(1, "D"); // "B"
list.indexOf("D"); // 1
Set
equals
provádí rychle atomické operace (se složitostí O(1), O(log(n))):
add
remove
contains
Množiny jsou primárně bez pořadí, bez uspořádání, existuje však i množina s uspořádáním. |
zjistí, jestli jsou objekty logicky stejné (porovnání atributů).
vrací pro logicky stejné objekty stejné číslo, haš.
je falešné ID — pro různé objekty může hashCode
vracet stejný haš.
HashSet
Pro (ne úplně ideální) hashCode
x + y vypadá tabulka následovně:
haš | objekt 0 | [0,0] 1 | [1,0] 2 | 3 | [2,1]
HashSet
pod lupou
boolean contains(Object o)
o
o
pomocí equals
Co když mají všechny objekty stejný haš?
Co když porušíme kontrakt metody hashCode
(pro stejné objekty vrátí různá čísla)?
Další implmentací množiny je LinkedHashSet = Hash Table + Linked List
|
třída Stack
, struktura LIFO
třída Queue
, struktura FIFO
PriorityQueue
třída Deque
(čteme "deck")
Existují tyto starší typy kontejnerů (za → uvádíme náhradu):
Hashtable
→ HashMap
, HashSet
(podle účelu)Vector
→ List
Stack
→ List
nebo lépe Queue
či Deque
Integer, Char, Boolean, Double
…List<Integer> list = new ArrayList<>();
list.add(new Integer(1));
list.add(1); // autoboxing
int primitiveType = list.get(0); // unboxing
Základní typy:
forEach
for
.List<Integer> numbers = List.of(1, 1, 2, 3, 5);
for(Integer i: list) {
System.out.println(i);
}
ConcurrentModificationException
.Set<String> set = Set.of("Donald Trump", "Barrack Obama");
for(String s: set) {
if (s.equals("Donald Trump")) {
set.remove(s);
break;
}
}
Iterator<E>
while
:Set<Integer> set = Set.of(1, 2, 3);
Iterator<Integer> iterator = set.iterator();
while(iterator.hasNext()) {
Integer element = iterator.next();
...
}
E next()
NoSuchElementException
jestli iterace nemá žádné zbývající prvky
boolean hasNext()
true
jestli iterace obsahuje nějaký prvek
void remove()
next()
Pro procházení iterátoru se dá použít i for
cyklus:
Set<String> set = Set.of("Donald Trump", "Barrack Obama", "Hillary Clinton");
for (Iterator<String> iter = set.iterator(); iter.hasNext();) {
String element = iter.next();
if (!element.equals("Barrack Obama")) iter.remove();
}
Roli iterátoru plnil dříve výčet
(Enumeration ) — nepoužívat.
|
/