java - collections framework

33
COLLECTIONS FRAMEWORK PROGRAMMAZIONE CONCORRENTE E DISTR. Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2015 – 2016 [email protected]

Upload: riccardo-cardin

Post on 15-Jan-2017

1.019 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Java - Collections framework

COLLECTIONS FRAMEWORKPROGRAMMAZIONE CONCORRENTE E DISTR.Università degli Studi di Padova

Dipartimento di Matematica

Corso di Laurea in Informatica, A.A. 2015 – [email protected]

Page 2: Java - Collections framework

2Programmazione concorrente e distribuita

SUMMARY Introduction Collections and iterators Linked list Array list Hash set Tree set Maps Collections framework

Riccardo Cardin

Page 3: Java - Collections framework

3Programmazione concorrente e distribuita

INTRODUCTION Data structures can make a big difference in

programmingDo you need to search quickly? Do you need to rapidly

insert and remove element? ... The first version of Java was supplied with a very

small set of classes Vector, Stack, Hashtable, BitSet and Enumeration Java needed a serious data structure library

Without the complexity of C++ STL With the benefit of «generic algorithms» of C++ STL

The collections framework satisfies those needs

Riccardo Cardin

Page 4: Java - Collections framework

4Programmazione concorrente e distribuita

INTRODUCTION Java collection framework separates interfaces

and implementations It defines a set of interfaces that represents abstract

data structuresEach interface is then implemented in different ways,

each with main focus on some aspects For example, fast insertion, fast search, fixed memory

consumption, ...Using interfaces, you can change the implementation

simply modifying a single statement

Riccardo Cardin

// ArrayList optimizes random access, LinkedList modification stmtsList<Integer> list = new ArrayList<>();list = new LinkedList<>()

Page 5: Java - Collections framework

5Programmazione concorrente e distribuita

INTRODUCTION

Riccardo Cardin

Page 6: Java - Collections framework

6Programmazione concorrente e distribuita

COLLECTIONS AND ITERATORS The fundamental interface for collection is the Collection interface

The add method adds an element ot the collection Returns true if the collection was changed by the addition

Iterators are used visit elements in the collection Implements the Iterator design pattern

Riccardo Cardin

public interface Collection<E> { boolean add(E element); Iterator<E> iterator(); // ...}

public interface Iterator<E> { E next(); boolean hasNext(); void remove();}

Page 7: Java - Collections framework

7Programmazione concorrente e distribuita

COLLECTIONS AND ITERATORS Using iterators we have decoupled a collection

from the traversing policiesThe order in which elements are visited depends on

the the collection typeUsing the next method the collection is visited one

element at time If the end of the collection is reached an NoSuchElementException is thrown

Use the hasNext method to check if the collection has more elements to visit

Think of Java iterators as being between elements The iterator jumps over the next element

Riccardo Cardin

Page 8: Java - Collections framework

8Programmazione concorrente e distribuita

COLLECTIONS AND ITERATORS

Riccardo Cardin

Think of Java iterators as being between elements

While moving, the iterator returns the current element

Page 9: Java - Collections framework

9Programmazione concorrente e distribuita

COLLECTIONS AND ITERATORS Using next and hasNext it is possibile to

traverse the collection

As of Java 5, there is an elegant shortcut to looping a collection using iterators

To use the for each loop the data structure must implement Iterable<E> interface Riccardo Cardin

for (String element : c) { // do something with element}

Collection<String> c = /* ... */;Iterator<String> iter = c.iterator();while (iter.hasNext()) { String element = iter.next(); // do something with element}

Page 10: Java - Collections framework

10Programmazione concorrente e distribuita

COLLECTIONS AND ITERATORS Using an iterator it possibile to remove elements

from a collectionThe remove method removes the element that was

returned by the last call to next There is a strong dependency between next and remove: it

is illegal to call remove if it wasn’t preceded by a call to next

This is the only way to safely modify a collection after the creation of an iterator

Riccardo Cardin

for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) { // Point the iterator to the current element String string = iterator.next(); iterator.remove();}

Page 11: Java - Collections framework

11Programmazione concorrente e distribuita

COLLECTIONS AND ITERATORS

Riccardo Cardin

Page 12: Java - Collections framework

12Programmazione concorrente e distribuita

CONCRETE COLLECTIONS All concrete collections implement Collection

or Map interfacesWe will introduce only few of them

Riccardo Cardin

Collection type

Description

ArrayList An indexed sequence that grows and shrinks dynamically

LinkedList An ordered sequence that allows efficient insertion and removal at any location

HashSet An unordered collection that rejects duplicates

TreeSet A sorted setHashMap A data structure that stores key/value

associations

Page 13: Java - Collections framework

13Programmazione concorrente e distribuita

LINKED LIST A LinkedList<E> is an ordered data structure

that stores each object in a linkEach link store a reference to the next link of the seq.

In Java, a linked list is always doubly linked

Riccardo Cardin

Implements List<E> interface

Page 14: Java - Collections framework

14Programmazione concorrente e distribuita

LINKED LIST Very efficient for remove and add operations

These ops are made through an Iterator Other elements in the list have not to be repositioned after

removal of an element

Riccardo Cardin

Page 15: Java - Collections framework

Programmazione concorrente e distribuita

LINKED LIST Also di add operation is made efficiently through

an iterator of type ListIterator<E>

Use List.listIterator method to get oneNew element is added before the current position of

the iterator Be carefull of concurrent modification using iterators

Linked list are very inefficient in random access

15Riccardo Cardin

interface ListIterator<E> extends Iterator<E> { void add(E element); E previous() boolean hasPrevious()}

for (int i = 0; i < list.size(); i++) // do something with list.get(i);

Page 16: Java - Collections framework

16Programmazione concorrente e distribuita

LINKED LIST

Riccardo Cardin

Adding a new element changes at most two

references

Page 17: Java - Collections framework

17Programmazione concorrente e distribuita

ARRAY LIST An ArrayList<E> is an ordered data structure

that is very efficient in random access ops. It encapsulate a dynamically reallocated array of

objectsAdding and removing operation are not so efficient

Reallocation of elements is neededUse ArrayList instead of Vector

More efficient due to its not synchronized methods

Riccardo Cardin

Page 18: Java - Collections framework

18Programmazione concorrente e distribuita

ARRAY LIST

Riccardo Cardin

Page 19: Java - Collections framework

19Programmazione concorrente e distribuita

HASH SET A Set<E> is a data structure that doesn’t care

about element’s orderingA set contain the same element only onceSearch operation performs very efficientlyA set is a Collection

An HashSet<E> uses hash codes to distinguish among elementsAn hashCode is a number that can be derived from

object data You must provide an hash function to your classes The function must be compatible with the equals method

Riccardo Cardin

Page 20: Java - Collections framework

20Programmazione concorrente e distribuita

HASH SET Hash codes have to be computed quickly

Hash functions have some hash collision Using the correct hash function the number of collision

should be unlikelyHash table are implemented as an array of linked lists

Riccardo Cardin

Using an hash function, the bucket in which inserting a new element is equal to hash(element) % #bucket

If the hash function produceds values that are randomly

distributed, collision should be rare

buck

ets

Page 21: Java - Collections framework

21Programmazione concorrente e distribuita

HASH SET An HashSet is implemented using an hash table

The contains method is very efficient, because it has to lookup the element only in one bucket

An iterator to hash set visit each bucket in turn Because of scattering, they are visited in a seemingly random

orderThe add method adds an element if it is not already

present Don’t mutate an element in a set once inserted

If the hash code of an element were to change, the element would no longer be in the correct position

Riccardo Cardin

Page 22: Java - Collections framework

22Programmazione concorrente e distribuita

TREE SET A TreeSet<E> is a sorted set

While iterating over the collection, the elements are presented in sorted order

It uses a red-black tree to store data Insertion is slower than insertion in an hash table, but it is

still much faster than insertion in an array or linked list ...but a tree set automatically sorts the elements ;)

The type of the elements may implement Comparable<T>

Riccardo Cardin

public interface Comparable<T> { // It returns a value that is < 0, = 0 or > 0 int compareTo(T other);}

Page 23: Java - Collections framework

23Programmazione concorrente e distribuita

TREE SET What if elements do not implement Comparable or

if you need more than on compation alg?Provide a Comparator during set construction

The compare method acts like the compareTo method Function object (lambda anyone?!)

Using an HashSet or a TreeSet?There must be a total ordering defined on elements

You have to implement also the Comparator interfaceDo you neeed elements to be sorted?

Riccardo Cardin

public interface Comparator<T> { // Defines how two elements of type T have to be compared int compare(T a, T b);}

Page 24: Java - Collections framework

24Programmazione concorrente e distribuita

TREE SET

Riccardo Cardin

Page 25: Java - Collections framework

25Programmazione concorrente e distribuita

MAPS A Map<K,V> is a data structure that allows you to

search an element using a keyA map stores key/value pairs

Key must be unique: If you call the put method twice with the same key, the second value replaces the first one.

If no info is associated with a key, get returns null In sets to find an element you must have a copy of it

An HashMap hashes the keysRiccardo Cardin

// HashMap implements MapMap<String, Employee> staff = new HashMap<>(); Employee harry = new Employee("Harry Hacker");staff.put("987-98-9996", harry);

String s = "987-98-9996";e = staff.get(s); // gets harry

Page 26: Java - Collections framework

26Programmazione concorrente e distribuita

MAPS The collection framework does not consider a

map itself as a Collection It is possible to obtain views of the map

If you are interested in all the values of a map, loop over the enumeration of its entries

Iterators on views cannot add elements to the map An UnsupportedOperationException is thrownRiccardo Cardin

Set<K> keySet() // Set of keysCollection<K> values() // Collection of valuesSet<Map.Entry<K, V>> entrySet() // Set of pairs (key,value)

for (Map.Entry<String, Employee> entry : staff.entrySet()) { String key = entry.getKey(); Employee value = entry.getValue(); // do something with key, value}

Page 27: Java - Collections framework

27Programmazione concorrente e distribuita

THE FRAMEWORK A framework is a set of classes that form the

basis for building advanced functionalityThe Collection framework defines classes to

implement collectionsThe main interfaces are Collection and Map

Insertion interfaces are different between the two types

To get the elements from a Collection, just iterate over it To get a value from a Map, use the get method

Riccardo Cardin

// To insert an element in a Collectionboolean add(E element)// To store a key/value pair in a MapV put(K key, V value)

V get(K key)

Page 28: Java - Collections framework

28Programmazione concorrente e distribuita

THE FRAMEWORK A List is an ordered collection

There is the concept of position of an element

A list provides random access methods Lists provides a specialized iterator, ListIterator

A Set is a Collection with no duplicatesThe add method can reject a value if already presentMethods equals and hashCode are used to maintain

elements inside the setRiccardo Cardin

void add(int index, E element)E get(int index)void remove(int index)

void add(E element)

Page 29: Java - Collections framework

29Programmazione concorrente e distribuita

THE FRAMEWORK The framework provides some wrappers

Riccardo Cardin

Page 30: Java - Collections framework

30Programmazione concorrente e distribuita

THE FRAMEWORK The framework have some companion objects

The Arrays type allows to trasform arrays into List

The list returned is a view on the array: it is not possible to change the size of the list; elements are the same

The Collections type have a bunch of utilities The method nCopies builds an illusory immutable list

Object is stored only once Method singleton returns an illusory set with one element

And so on...Riccardo Cardin

static <T> List<T> asList(T... a)

List<String> settings = Collections.nCopies(100, "DEFAULT");

Collections.singleton(anObject)

A.k.a. views

Page 31: Java - Collections framework

31Programmazione concorrente e distribuita

THE FRAMEWORK

Riccardo Cardin

Page 32: Java - Collections framework

32Programmazione concorrente e distribuita

EXAMPLES

Riccardo Cardin

https://github.com/rcardin/pcd-snippets

Page 33: Java - Collections framework

33Programmazione concorrente e distribuita

REFERENCES Chap. 13 «Collections», Core Java Volume I - Fundamentals, Cay

Horstmann, Gary Cornell, 2012, Prentice Hall

Riccardo Cardin