Maps & Sets

0.0(0)
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/19

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

20 Terms

1
New cards

put()


Associates key with specified value. If key already exists, replaces previous value with specified value.

// Map originally empty
exMap.put("Tom", 14); 
// Map now: Tom->14,  
exMap.put("John", 86); 
// Map now: Tom->14, John->86

2
New cards

putIfAbsent

Associates key with specified value if the key does not already exist or is mapped to null.

// Assume Map is: Tom->14, John->86
exMap.putIfAbsent("Tom", 20); 
// Key "Tom" already exists. Map is unchanged.
exMap.putIfAbsent("Mary", 13); 
// Map is now: Tom->14, John->86, Mary->13

3
New cards

get()

Returns the value associated with key. If key does not exist, return null.

// Assume Map is: Tom->14, John->86, Mary->13
exMap.get("Tom")  // returns 14 
exMap.get("Bob")  // returns null// Assume Map is: Tom->14, John->86, Mary->13

4
New cards

containsKey()

Returns true if key exists, otherwise returns false.

// Assume Map is: Tom->14, John->86, Mary->13
exMap.containsKey("Tom")  // returns true 
exMap.containsKey("Bob")  // returns false

5
New cards

containsValue()

Returns true if at least one key is associated with the specified value, otherwise returns false.

6
New cards

remove()

Removes the map entry for the specified key if the key exists.

// Assume Map is: Tom->14, John->86, Mary->13
exMap.containsValue(86)  // returns true (key "John" associated with value 86)
exMap.containsValue(17)  // returns false (no key associated with value 17)

7
New cards

clear()

Removes all map entries.

// Assume Map is: Tom->14, John->86, Mary->13
exMap.clear();  
// Map is now empty

8
New cards

doesn’t

HashMap ______ directly implement Iterable interface

9
New cards

entrySet()

for (Map.Entry<Integer,String> entry: hashMap.entrySet()){
	System.out.println("Key: entry.getKey() + Value + entry.getValue();
}

To iterate over a HashMap, you typically
use its ___________

10
New cards

iterable, generic

making a custom collection

make it _______ and ______

11
New cards

java.util

ArrayList, HashSet, and TreeSet are in package____________

12
New cards

add(), remove(), contains

ArrayList, HashSet, TreeSet have these 3 methods: _____, ______, _______

13
New cards

true

true or false: ArrayList, HashSet, TreeSet use enhanced for loops

14
New cards

compareTo()

TreeSet sorts the set according _________ order of the values

15
New cards

keys

TreeMap sorts the map according to the ______

16
New cards

interface

set is an ________

17
New cards

keySet()

Returns a Set containing all keys within the map.

// Assume Map is: Tom->14, John->86, Mary->13
keys = exMap.keySet();
// keys contains: "Tom", "John", "Mary"

18
New cards

values

Returns a Collection containing all values within the map.

// Assume Map is: Tom->14, John->86, Mary->13
values = exMap.values();
// values contains: 14, 86, 13

19
New cards

HashMap

map that provides faster access but doesn’t guarantee ordering

20
New cards

TreeMap

map that provides slightly slower access but maintains ordering of keys