Вы находитесь на странице: 1из 79

Java Advanced

Collections Framework


1.
2.
3.
4.
5.
6.
7.
8.
9.

Georgiy Korneev

Java Advanced / Collections Framework

Collections Framework


()

java.util

Georgiy Korneev

Java Advanced / Collections Framework


Collection

A
D

Georgiy Korneev

C
A
B

Java Advanced / Collections Framework

size()
isEmpty()

contains(Object o)
containsAll(Collection c)
c

Georgiy Korneev

Java Advanced / Collections Framework

add(Object e)
addAll(Collection c)
remove(Object e)
removeAll(Collection )
retainAll(Collection )

clear()

UnsupportedOperationException

Georgiy Korneev

Java Advanced / Collections Framework

.
public int read(String file) throws IOException {
Scanner scanner = new Scanner(
new File(file), "Cp1251");
int read = 0;
while (scanner.hasNext()) {
read++;
c.add(scanner.next());
}

return read;
}
Georgiy Korneev

Java Advanced / Collections Framework


Iterator
Iterator Collection.iterator()

A
D

Georgiy Korneev

C
A
B

Java Advanced / Collections Framework

hasNext()

next()
remove()

NoSuchElementException

ConcurrentModificationException

Georgiy Korneev

Java Advanced / Collections Framework

10


for(Iterator i = c.iterator(); i.hasNext(); ) {
E element = (E) i.next();
...
}


for(Iterator i = c.iterator(); i.hasNext(); ) {
if (!p(i.next()) i.remove();
}

Georgiy Korneev

Java Advanced / Collections Framework

11

.
public void dump() {
for (Iterator i = c.iterator(); i.hasNext(); ) {
String word = (String) i.next();
System.out.print(word + ", ");
}
System.out.println();
}

Georgiy Korneev

Java Advanced / Collections Framework

12

Object[] toArray()
Object[] toArray(Object[] a)

Integer[] i = (Integer[])
c.toArray(new Integer[c.size()]);

Georgiy Korneev

Java Advanced / Collections Framework

13

AbstractCollection

iterator()
size()

add(Object o)
iterator.remove()

Georgiy Korneev

Java Advanced / Collections Framework

14

Set

C
D

Georgiy Korneev

A
B

Java Advanced / Collections Framework

16

Object.equals(Object object)

o1.equals(o1)

o1.equals(o2) == e2.equals(o1)

o1.equals(o2) && o2.equals(o3) => o1.equals(o3)

o1.equals(o2) , o1 o2

null
o1.equals(null) == false

Georgiy Korneev

Java Advanced / Collections Framework

17

addAll(Collection c)

retainAll(Collection c)

containsAll(Collection c)

removeAll(Collection c)

Georgiy Korneev

Java Advanced / Collections Framework

18

HashSet LinkedHashSet

HashSet
LinkedHashSet
c

Georgiy Korneev

Java Advanced / Collections Framework

19

HashSet

HashSet()
HashSet(Collection c)

HashSet(int initialCapacity)

HashSet(int initialCapacity, double


loadFactor)

Georgiy Korneev

Java Advanced / Collections Framework

20

Object.hashCode()

hashCode() ,

equals
o1.equals(o2) => o1.hashCode() == o2.hashCode()

Georgiy Korneev

Java Advanced / Collections Framework

21

.
CollectionExample c =
new CollectionExample(new HashSet());
int words = c.read(args[0]);
System.out.println("Words total: " + words);
System.out.println("Unique words: " +
c.getCollection().size());
c.dump();
Georgiy Korneev

Java Advanced / Collections Framework

22

AbstractSet

iterator()
size()

add(Object o)
iterator.remove()

Georgiy Korneev

Java Advanced / Collections Framework

23

List

Georgiy Korneev

Java Advanced / Collections Framework

25

get(int i)
set(int I, Object e)
add(int i, Object e)
remove(int i)
indexOf(Object e)
lastIndexOf(Object e)

List subList(int from, int to)

Georgiy Korneev

Java Advanced / Collections Framework

26

ListIterator extends Iterator


listIterator()
/

0
Georgiy Korneev

Java Advanced / Collections Framework

5
27

hasNext() / hasPrevious()
next() / previous()
nextIndex() / previousIndex()

remove()
set(Object e)
add(Object e)

Georgiy Korneev

Java Advanced / Collections Framework

28

ArrayList

ArrayList

Georgiy Korneev

Java Advanced / Collections Framework

29

ArrayList

ensureCapacity(int c)

trimToSize()

Georgiy Korneev

Java Advanced / Collections Framework

30

ArrayList

ArrayList()
ArrayList(Collection c)
ArrayList(int initialCapacity)

Georgiy Korneev

Java Advanced / Collections Framework

31

ArrayList

Georgiy Korneev

Java Advanced / Collections Framework

32

. ArrayList
List list = new ArrayList();

for (int i = list.size() - 1; i >= 0; i--) {


System.out.println(list.get(i));
}

Georgiy Korneev

Java Advanced / Collections Framework

33

LinkedList

LinkedList

Georgiy Korneev

Java Advanced / Collections Framework

34

LinkedList

LinkedList()
LinkedList(Collection c)

addFirst(Object o)
addLast(Object o)
removeFirst()
removeLast()

Georgiy Korneev

Java Advanced / Collections Framework

35

LinkedList

Georgiy Korneev

Java Advanced / Collections Framework

36

. LinkedList
List list = new LinkedList();

for (ListIterator li = list.listIterator(list.size());


li.hasPrevious(); )
{
System.out.println(li.previous());
}

Georgiy Korneev

Java Advanced / Collections Framework

37

AbstractList

get(index)
size()
set(index, element)

add(index, element)
remove(index)

Georgiy Korneev

Java Advanced / Collections Framework

38

AbstractSequentialList

listIterator() ( )
size()
ListIterator.set(index, element)

ListIterator.add(element)
ListIterator.remove(element)

Georgiy Korneev

Java Advanced / Collections Framework

39

Queue



null

Georgiy Korneev

Java Advanced / Collections Framework

41

add(Object o)

Object element()

NoSuchElementException

Object remove()

IllegalStateException

NoSuchElementException

offer(Object o)
Object peek()
Object poll()

Georgiy Korneev

Java Advanced / Collections Framework

42

LinkedList

Georgiy Korneev

Java Advanced / Collections Framework

43

AbstractQueue

size()
offer(Object o)
peek()
poll()
iterator()

Georgiy Korneev

Java Advanced / Collections Framework

44

-

Map

Georgiy Korneev

Java Advanced / Collections Framework

46

(1)

get(Object k)
put(Object k, Object v)
remove(Object k)
containsKey(Object k)
containsValue(Object v)

size()
isEmpty()

Georgiy Korneev

Java Advanced / Collections Framework

47

(2)

entrySet()
values()
keySet()

putAll(Map map)

Georgiy Korneev

Java Advanced / Collections Framework

48

+
Map.Entry

Object getKey()
Object getValue()
setValue(Object v)

Georgiy Korneev

Java Advanced / Collections Framework

49

HashMap LinkedHashMap

HashMap
LinkedHashMap

Georgiy Korneev

Java Advanced / Collections Framework

50

HashMap

HashMap()
HashMap(Map m)
HashMap(int initialCapacity)

HashMap (int initialCapacity, int loadFactor)


Georgiy Korneev

Java Advanced / Collections Framework

51

AbstractMap

entrySet()

Georgiy Korneev

Java Advanced / Collections Framework

52

. (1)
while (scanner.hasNext()) {
String word = scanner.next();
Integer count = (Integer) map.get(word);
int value = (count == null)
?0
: count.intValue();
map.put(word, new Integer(value + 1));
}

Georgiy Korneev

Java Advanced / Collections Framework

53

. (2)
for (
Iterator i = map.entrySet().iterator();
i.hasNext();
){
Map.Entry entry = (Map.Entry) i.next();
System.out.println(
entry.getKey() + " " + entry.getValue());
}

Georgiy Korneev

Java Advanced / Collections Framework

54

Comparable

int compareTo(Object o)

Comparator

int compare(Object o1, Object o2)

Georgiy Korneev

<

>

Java Advanced / Collections Framework

56

()

sgn(o1.compareTo(o2)) == -sgn(o2.compareTo(o1))


o1.compareTo(o2) == 0 =>
sgn(o1.compareTo(o3)) == sgn(o2.compareTo(o3))

equals()
o1.equals(o2) == (o1.compareTo(o2) == 0)

Georgiy Korneev

Java Advanced / Collections Framework

57

SortedSet

first()
last()
headSet(Object o)
o
tailSet(Object o)
o
subSet(Object o1, Object o2)
o2
o2

TreeSet

Georgiy Korneev

Java Advanced / Collections Framework

58

SortedMap

firstKey()
lastKey()
headMap(Object o)
o
tailMap(Object o)
o
subMap(Object o1, Object o2)
o2 o1

TreeMap

Georgiy Korneev

Java Advanced / Collections Framework

59

PriorityQueue

Georgiy Korneev

Java Advanced / Collections Framework

60

. TreeSet


CollectionExample c = new CollectionExample(
new TreeSet());
c.read(args[0]);
c.dump();


CollectionExample c = new CollectionExample(new
TreeSet(String.CASE_INSENSITIVE_ORDER));
int words = c.read(args[0]);
c.dump();

Georgiy Korneev

Java Advanced / Collections Framework

61

Collections

Georgiy Korneev

Java Advanced / Collections Framework

63

fill(List l, Object v)
reverse(List l)

copy(List l1, List l2)

Georgiy Korneev

Java Advanced / Collections Framework

64

shuffle(List l)
shuffle(List l, Random r)

Georgiy Korneev

Java Advanced / Collections Framework

65


Merge Sort

sort(List l) (
)
sort(List l, Comparator c)
( )

Georgiy Korneev

Java Advanced / Collections Framework

66

binarySearch(List l, Object o) o
binarySearch(List l, Object o, Comparator c)
o

Georgiy Korneev

Java Advanced / Collections Framework

67

min(Collection c)
( )
min(Collection c, Comparator cmp)
( )

max(Collection c)
( )
max(Collection c, Comparator cmp)
( )

Georgiy Korneev

Java Advanced / Collections Framework

68

.
List list = new ArrayList();
CollectionExample c = new CollectionExample(list);
c.read(args[0]);
Collections.reverse(list);
Collections.shuffle(list);
Collections.sort(list);
Collections.sort(list,
String.CASE_INSENSITIVE_ORDER);
Collections.fill(list, "temp");
System.out.println(Collections.min(list));
System.out.println(Collections.min(list,
String.CASE_INSENSITIVE_ORDER));
Georgiy Korneev

Java Advanced / Collections Framework

69

emptySet()
emptyList()
emptyMap()

singleton(Object o)
singletonList(Object o)
singletonMap(Object key, Object value)

Georgiy Korneev

Java Advanced / Collections Framework

70

unmodifiableSet(Set s)

unmodifiableSortedSet(SortedSet s)

unmodifiableList(List l)
unmodifiableMap(Map m)

unmodifiableSortedMap(SortedMap m)

Georgiy Korneev

Java Advanced / Collections Framework

71

Arrays

List asList()

Georgiy Korneev

Java Advanced / Collections Framework

72

Vector (ArrayList)

Dictionary (Map)

Stack (ArrayList)
Hashtable (HashMap)

Enumeration (Iterator)

Georgiy Korneev

Java Advanced / Collections Framework

74

Collections Framework (1)


interface
Iterator

interface
Collection

interface
ListIterator

interface
List

interface
Set

interface
SortedSet

AbstractSequentialList

AbstractList

AbstractSet

TreeSet

LinkedList

ArrayList

HashSet

LinkedHashSet

Georgiy Korneev

Java Advanced / Collections Framework

76

Collections Framework (2)


interface
Map

interface
SortedMap

AbstractMap

HashMap

Georgiy Korneev

TreeMap

interface
Queue

interface
Comparable

AbstractQueue

interface
Comparator

PriorityQueue

LinkedList

Java Advanced / Collections Framework

77

Collections Framework //
http://java.sun.com/j2se/1.5.0/docs/guide/coll
ections/index.html
Collections Tutorial //
http://java.sun.com/docs/books/tutorial/collect
ions/index.html
Introduction to the Collections Framework //
http://java.sun.com/developer/onlineTraining/
collections/

Georgiy Korneev

Java Advanced / Collections Framework

78

Georgiy Korneev

Java Advanced / Collections Framework

79