java collections framework reloadedrazvanm/600.120/java-collections-2004-10-01.pdf · java...

23
Java Collections Framework reloaded October 1, 2004 Java Collections - 2004-10-01 – p. 1/23

Upload: others

Post on 02-Jun-2020

42 views

Category:

Documents


0 download

TRANSCRIPT

Java Collections Frameworkreloaded

October 1, 2004

Java Collections - 2004-10-01 – p. 1/23

Outline

Interfaces

Implementations

Ordering

Java 1.5

Java Collections - 2004-10-01 – p. 2/23

Components

Interfaces: abstract data types which

allow collections to be manipulated

independently of the details of their

representation

Implementations: reusable data

structures

Algorithms: reusable functionality

Java Collections - 2004-10-01 – p. 3/23

Core collection interfaces

Java Collections - 2004-10-01 – p. 4/23

Core collection interfacesCollection

represents a group of objects, known as its elementsleast common denominator that all collectionsimplementis used to pass collections around and manipulatethem when maximum generality is desired

Set

List

Map

SortedSet

SortedMap

Java Collections - 2004-10-01 – p. 5/23

Core collection interfacesCollection

Setcollection that cannot contain duplicate elements

Listan ordered collection (sometimes called a sequence)elements can be access by their integer index(position)

Map

SortedSet

SortedMap

Java Collections - 2004-10-01 – p. 6/23

Core collection interfacesCollection

Set

List

Mapan object that maps keys to valuescannot contain duplicate keyseach key can map to at most one value

SortedSet

SortedMap

Java Collections - 2004-10-01 – p. 7/23

Core collection interfacesCollection

Set

List

Map

SortedSeta Set that maintains its elements in ascending order

SortedMapa Map that maintains its mappings in ascending keyorder

Java Collections - 2004-10-01 – p. 8/23

Implementations

Hash Resizable Balanced Linked Hash Table

Table Array Tree List + Linked List

Set HashSet TreeSet LinkedHashSet

List ArrayList LinkedList

Map HashMap TreeMap LinkedHashMap

Java Collections - 2004-10-01 – p. 9/23

Set: HashSet, TreeSets and LinkedHashSet

HashSetconstant time for add, remove, contains and size

offers no ordering guaranteesiteration is linear in the sum of the number of entriesand the number of buckets (the capacity)

TreeSetimplements SortedSetadd, remove and contains have O(log(n)) time cost

LinkedHashSetiteration ordering is the order in which elementswere inserted into the setmaintains a doubly-linked list running through all ofits entries

Java Collections - 2004-10-01 – p. 10/23

List: ArrayList and LinkedListArrayList

roughly equivalent to Vector, except that it isunsynchronizedcapacity grows automaticallysize, isEmpty, get, set, iterator, and listIterator runin constant timeadd operation runs in amortized constant time(adding n elements requires O(n) time)

LinkedListprovides methods to get, remove and insert anelement at the beginning and end of the list (linkedlists to be used as a stack, queue, or double-endedqueue

Java Collections - 2004-10-01 – p. 11/23

Map: HashMap, TreeMapHashMap

constant-time performance for the basic operations(get and put)iteration requires time proportional to the capacity ofthe HashMap instance (the number of buckets) plusits size (the number of key-value mappings)when the number of entries in the hash tableexceeds the product of the load factor and thecurrent capacity, the capacity is roughly doubled bycalling the rehash method

TreeMapis based on Red-Black treeO(log(n)) time cost for the containsKey, get, put andremove

Java Collections - 2004-10-01 – p. 12/23

Map: LinkedHashMapLinkedHashMap

iteration ordering is the order in which elementswere inserted into the setmaintains a doubly-linked list running through all ofits entries

Java Collections - 2004-10-01 – p. 13/23

Legacy: Vector, Hashtable

Vectorimplements Listis synchronized

Hashtableimplements Mapis synchronized

Java Collections - 2004-10-01 – p. 14/23

WrappersSynchronization

public staticCollection

synchronizedCollection(Collection c);Set synchronizedSet(Set s);List synchronizedList(List list);Map synchronizedMap(Map m);SortedSet

synchronizedSortedSet(SortedSet s);SortedMap

synchronizedSortedMap(SortedMap m);

Unmodifiable

Java Collections - 2004-10-01 – p. 15/23

WrappersSynchronization

Unmodifiable

public staticCollection

unmodifiableCollection(Collection c);Set unmodifiableSet(Set s);List unmodifiableList(List list);Map unmodifiableMap(Map m);SortedSet

unmodifiableSortedSet(SortedSet s);SortedMap

unmodifiableSortedMap(SortedMap m);

Java Collections - 2004-10-01 – p. 16/23

Special ImplementationsList-view of an Array

List l = Arrays.asList(new Object[size]);

Immutable Multiple-Copy List

List l = new ArrayList(Collections.nCopies(1000, null));

lovablePets.addAll(Collections.nCopies(69, "fruit bat"));

Immutable Singleton Set

c.removeAll(Collections.singleton(e));

Empty Set and Empty List Constants

static Set Collections.EMPTY_SET;statis List Collections.EMPTY_LIST;

Java Collections - 2004-10-01 – p. 17/23

Algorithms

are implemented in Collections

for List:sorting: uses a slightly optimized merge sortalgorithmshufflingreverse, fill, copysearching: binarySearch

any Collection:finding extreme values: min, max

Java Collections - 2004-10-01 – p. 18/23

More interfacesComparator

int compare(Object o1, Object o2)boolean equals(Object obj)

Comparable

int compareTo(Object o)

The natural ordering for a class C is said to beconsistent with equals if and only if(e1.compareTo((Object)e2) == 0) has thesame boolean value as e1.equals((Object)e2)for every e1 and e2 of class C.

Java Collections - 2004-10-01 – p. 19/23

Objectpublic boolean equals(Object obj)

public int hashCode()

If two objects are equal according to theequals(Object) method, then calling thehashCode method on each of the two objects mustproduce the same integer result.

Java Collections - 2004-10-01 – p. 20/23

Java 1.5new things:

Genericsfor-each

void cancelAll(Collection<TimerTask> c) {for (Iterator<TimerTask> i = c.iterator();

i.hasNext(); )i.next().cancel();

}

void cancelAll(Collection<TimerTask> c) {for (TimerTask t : c)

t.cancel();}

Java Collections - 2004-10-01 – p. 21/23

Java 1.5

// Returns the sum of the elements of aint sum(int[] a) {

int result = 0;for (int i : a)

result += i;return result;

}

Java Collections - 2004-10-01 – p. 22/23

That’s all!

Have a nice weekend!

Java Collections - 2004-10-01 – p. 23/23