1/19
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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
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
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
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
containsValue()
Returns true if at least one key is associated with the specified value, otherwise returns false.
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)
clear()
Removes all map entries.
// Assume Map is: Tom->14, John->86, Mary->13
exMap.clear();
// Map is now empty
doesn’t
HashMap ______ directly implement Iterable interface
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 ___________
iterable, generic
making a custom collection
make it _______ and ______
java.util
ArrayList, HashSet, and TreeSet are in package____________
add(), remove(), contains
ArrayList, HashSet, TreeSet have these 3 methods: _____, ______, _______
true
true or false: ArrayList, HashSet, TreeSet use enhanced for loops
compareTo()
TreeSet sorts the set according _________ order of the values
keys
TreeMap sorts the map according to the ______
interface
set is an ________
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"
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
HashMap
map that provides faster access but doesn’t guarantee ordering
TreeMap
map that provides slightly slower access but maintains ordering of keys