Top Banner
Maps a.k.a, associative array, map, or dictionary
15

a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Sep 16, 2018

Download

Documents

truongthuy
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Maps

a.k.a, associative array, map, or dictionary

Page 2: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

Definition

A.A. 2017/20182

! In computer science, an associative array, map, or dictionary is an abstract data type composed of (key, value) pairs, such that each key appears at most once

! Modern programming languages natively supports them E.g. Perl, Python, Ruby, Go

! Implemented through hash tables or tree data structure

V1[42] = “h2g2” V2[“h2g2”] = 42

Page 3: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

Java Collection Framework

A.A. 2017/20183

Page 4: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

Map interface

A.A. 2017/20184

! Map<K,V>! K: the type of keys maintained by this map! V: the type of mapped values

! Add/remove elements! value put(key, value)! value remove(key)

! Search! boolean containsKey(key)! boolean containsValue(value)

Page 5: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

Map interface (cont.)

A.A. 2017/20185

! Nested Class! Map.Entry<K,V>! A map entry (key-value pair).

! Set<Map.Entry<K,V>> entrySet()! Returns a Set view of the mappings contained in this map

! Set<K> keySet()! Returns a Set view of the keys contained in this map

! Collection<V> values()! Returns a Collection view of the values contained in this

map

Page 6: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

Map Family Tree

A.A. 2017/20186

Page 7: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

HashMap and Chaining

A.A. 2017/20187

0

m–1

U (universe of keys)

K (actual keys)

k1

k2

k3

k5

k4

k6

k7k8

K1,V1 K4,V4

K3,V3 K2,V2

K6,V6 K8,V8

K7,V7

Page 8: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

HashMap and Chaining

A.A. 2017/20188

! Non duplicated keys (values could be duplicated)! Chaining is not used to store multiple keys with the same value.

Each key should be unique! Chaining is used to solve the collision problem.

Page 9: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

HashMap

A.A. 2017/20189

! Non duplicated keys (values could be duplicated)! Not ordered (neither sorted)

! Implementation is based on a hash table! Operations put(k, v), get(k), remove(k), containsKey(k) have

complexity mostly O(1)

! Requires to override hashCode() equals()! Key object must be immutable

Page 10: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

HashMap vs HashSet

A.A. 2017/201810

! HashMap allows to insert key-value pairs. Each key is associated to a value

! HashSet allows to insert an object in a collection of object. The object itself (or part of it) is the key

! Similarties:! Do not accept duplicated key! Not ordered (neither sorted)! Implementation is based on a hash table! Requires to override hashCode() equals() for the Key object! Key object must me immutable (at least for the field used in

hashCode() and equals())

Page 11: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

HashMap complexity

A.A. 2017/201811

HashMapput(key, object) O(1)get(key) O(1)remove(key) O(1)containsKey(key) O(1)containsValue(object) O(N)

Page 12: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

HashMap complexity

A.A. 2017/201812

HashMapput(key, object) O(1)get(key) O(1)remove(key) O(1)containsKey(key) O(1)containsValue(object) O(N)

containsValue() will probably require time linear in the map size for most implementations of the Map interface – i.e. it is O(N)

Page 13: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

Collection Family Tree

A.A. 2017/201813

Page 14: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

LinkedHashMap

A.A. 2017/201814

! Implementation is based on a hash table and a double-linked list running through all of its entries:! Operations put(k, v), get(k), remove(k), containsKey(k) have

complexity mostly O(1)! Non duplicated keys

! Values could be duplicated! Ordered (usually insertion-order)

! Insertion order is not affected if a key is re-inserted

! Not sorted

Page 15: a.k.a, associative array, map, or dictionary · Tecniche di programmazione Definition 2 A.A. 2017/2018! In computer science, an associative array, map, or dictionary is an abstract

Tecniche di programmazione

Licenza d’uso

A.A. 2017/201815

! Queste diapositive sono distribuite con licenza Creative Commons “Attribuzione - Non commerciale - Condividi allo stesso modo (CC BY-NC-SA)”

! Sei libero:! di riprodurre, distribuire, comunicare al pubblico, esporre in pubblico,

rappresentare, eseguire e recitare quest'opera! di modificare quest'opera

! Alle seguenti condizioni:! Attribuzione — Devi attribuire la paternità dell'opera agli autori

originali e in modo tale da non suggerire che essi avallino te o il modo in cui tu usi l'opera.

! Non commerciale — Non puoi usare quest'opera per fini commerciali. ! Condividi allo stesso modo — Se alteri o trasformi quest'opera, o se

la usi per crearne un'altra, puoi distribuire l'opera risultante solo con una licenza identica o equivalente a questa.

! http://creativecommons.org/licenses/by-nc-sa/3.0/