To do: November 29
1) Given the map below, what are two different ways to check if a map has the key "a" ?
Map<String, String> map = new HashMap<String, String>();
// code adding entries to map not shown
To do: November 29
Modify and return the given map as follows: if exactly one of the keys "a" or "b" has a value in the map (but not both), set the other to have that same value in the map.
mapAB3({"a": "aaa", "c": "cake"}) → {"a": "aaa", "b": "aaa", "c": "cake"}
mapAB3({"b": "bbb", "c": "cake"}) → {"a": "bbb", "b": "bbb", "c": "cake"}
mapAB3({"a": "aaa", "b": "bbb", "c": "cake"}) → {"a": "aaa", "b": "bbb", "c": "cake"}
2) implement the method:
public Map<String, String> mapAB3(Map<String, String> map) {
Class work
Iterators, Sets and Maps Test on Friday (FRQ and MC)
© A+ Computer Science - www.apluscompsci.com
Map frequently used methods | |
Name | Use |
V put(K key, V value) | adds the <K, V> key-value pair to the map. If K was already present, then V that was replaced is returned. Else, null is returned. |
V get(Object k) | returns the value to which the key k is mapped or null if no mapping for the key |
void clear() | removes all items from the map |
int size() | returns the # of key-value mappings in the map |
Set<K> keySet() | returns a set of all keys in the map |
boolean containsKey(Object key) | checks if map contains a mapping for the specified key |
V remove(Object key) | Removes the mapping for a key from this map if present. If not, returns null. |
A Collection represents a group of objects, known as its elements. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
Collection
List
Set
Collection Interface
© A+ Computer Science - www.apluscompsci.com
The Map interface does not extend any
other interface.
Map
HashMap
SortedMap
TreeMap
Java Map Interface
CodingBat > Maps-2
Compare and contrast the following groups of methods:
Group 1: word0, wordLen, pairs
Group 2: wordCount, firstChar, wordAppend, wordMultiple, allSwap, firstSwap
How would you distinguish between these two groups of methods?
More Maps practice
CodingBat > Maps-2
Write implementations of wordCount, firstChar, wordMultiple in your IN and submit to Hub
Homework:
Iterators, Sets and Maps Test on Friday (FRQ and MC)