1 of 9

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

2 of 9

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) {

3 of 9

Class work

  • Read Intro to Data Structures chapter 5 pages 1-4 and then complete goFormative (3 key points from reading)
  • CodingBat > Map-1 > mapShare, mapAB, mapAB2
    • Write 3 method implementations in your IN and submit image/pdf

Iterators, Sets and Maps Test on Friday (FRQ and MC)

4 of 9

© 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.

5 of 9

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

6 of 9

© A+ Computer Science - www.apluscompsci.com

The Map interface does not extend any

other interface.

Map

HashMap

SortedMap

TreeMap

Java Map Interface

7 of 9

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?

8 of 9

More Maps practice

CodingBat > Maps-2

  • wordLen
  • wordCount
  • firstChar
  • wordMultiple
  • allSwap *
  • firstSwap *

Write implementations of wordCount, firstChar, wordMultiple in your IN and submit to Hub

9 of 9

Homework:

  • CodingBat > Maps-2 > wordLen, wordCount, firstChar, wordMultiple, allSwap, firstSwap
  • Write implementations of CodingBat > Maps-2 > wordCount, wordMultiple, allSwap in your IN and submit to Hub

Iterators, Sets and Maps Test on Friday (FRQ and MC)