Tải bản đầy đủ (.ppt) (21 trang)

Map.ppt

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (146.46 KB, 21 trang )



Map
Map


// Basic operations
// Basic operations
V put(K key, V value);
V put(K key, V value);
V get(Object key);
V get(Object key);
V remove(Object key);
V remove(Object key);
boolean containsKey(Object key);
boolean containsKey(Object key);
boolean containsValue(Object value);
boolean containsValue(Object value);
int size();
int size();
boolean isEmpty();
boolean isEmpty();


// Bulk operations
// Bulk operations
void putAll(Map<? extends K, ? extends V> m);
void putAll(Map<? extends K, ? extends V> m);
void clear();
void clear();



// Collection Views
// Collection Views
public Set<K> keySet();
public Set<K> keySet();
public Collection<V> values();
public Collection<V> values();
public Set<Map.Entry<K,V>> entrySet();
public Set<Map.Entry<K,V>> entrySet();


// Interface for entrySet elements
// Interface for entrySet elements
public interface Entry
public interface Entry
{
{
K getKey();
K getKey();
V getValue();
V getValue();
V setValue(V value); }
V setValue(V value); }


Three Map implementations
Three Map implementations

HashMap
HashMap


TreeMap
TreeMap

LinkedHashMap
LinkedHashMap


Map vs HashTable
Map vs HashTable

Different types of view.
Different types of view.

Hashtable does not provide iteration over key-
Hashtable does not provide iteration over key-
value pairs.
value pairs.

Map : safely remove entries during iteration
Map : safely remove entries during iteration


Quizzes
Quizzes

Generates a frequency table of the words found
Generates a frequency table of the words found
in its argument list. The frequency table maps
in its argument list. The frequency table maps

each word to the number of times it occurs in
each word to the number of times it occurs in
the argument list.
the argument list.


Comparing two Maps
Comparing two Maps

Two Map instances are equal if they represent
Two Map instances are equal if they represent
the same key-value mappings
the same key-value mappings


Construct a new map from an
Construct a new map from an
existing Map
existing Map

Map<K, V> copy = new HashMap<K, V>(m);
Map<K, V> copy = new HashMap<K, V>(m);


Bulk operations
Bulk operations

clear()
clear()


putAll()
putAll()


Examples
Examples

Combine two maps by using a constructor and
Combine two maps by using a constructor and
putAll()
putAll()


Collection views
Collection views

keySet : the Set of keys contained in the Map
keySet : the Set of keys contained in the Map

values — The Collection of values contained in
values — The Collection of values contained in
the Map.
the Map.

entrySet — the Set of key-value pairs contained
entrySet — the Set of key-value pairs contained
in the Map.
in the Map.



Iterating over the keys in a Map
Iterating over the keys in a Map

for (KeyType key : m.keySet())
for (KeyType key : m.keySet())
System.out.println(key);
System.out.println(key);

for (Iterator<Type> it = m.keySet().iterator();
for (Iterator<Type> it = m.keySet().iterator();
it.hasNext(); ) if (it.next().isBogus()) it.remove();
it.hasNext(); ) if (it.next().isBogus()) it.remove();

On key-value collections:
On key-value collections:
for (Map.Entry<KeyType, ValType> e :
for (Map.Entry<KeyType, ValType> e :
m.entrySet())
m.entrySet())
System.out.println(e.getKey() + ": " +
System.out.println(e.getKey() + ": " +
e.getValue()); .
e.getValue()); .


Modify Map Value
Modify Map Value

Map.Entry's setValue.
Map.Entry's setValue.



Remove in Map
Remove in Map

remove, removeAll, retainAll, and clear
remove, removeAll, retainAll, and clear
operations from the Collection view (or iterator
operations from the Collection view (or iterator
view).
view).


Quizzes
Quizzes

Identify whether the first Map contains all the
Identify whether the first Map contains all the
key-value mappings in the second.
key-value mappings in the second.


Quizzes
Quizzes

If whether two Map objects contain mappings
If whether two Map objects contain mappings
for all of the same keys.
for all of the same keys.



Quizzes
Quizzes

Suppose you have a Map that represents a
Suppose you have a Map that represents a
collection of attribute-value pairs, and two Sets
collection of attribute-value pairs, and two Sets
representing required attributes and permissible
representing required attributes and permissible
attributes. (The permissible attributes include the
attributes. (The permissible attributes include the
required attributes.) The following snippet
required attributes.) The following snippet
determines whether the attribute map conforms
determines whether the attribute map conforms
to these constraints and prints a detailed error
to these constraints and prints a detailed error
message if it doesn't.
message if it doesn't.


Quizzes
Quizzes

You want to know all the keys common to two
You want to know all the keys common to two
Map objects.
Map objects.



Multimap
Multimap

A
A
multimap
multimap
is like a Map but it can map each key
is like a Map but it can map each key
to multiple values.
to multiple values.

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×