-
Notifications
You must be signed in to change notification settings - Fork 1
Hash Map
Sharina Stubbs edited this page Jan 23, 2020
·
5 revisions
- Hold key value pairs
- key value pairs are not in order
- Let us specify what the data type is for keys and what is data type for values
- Can't handle primitives without help, ie, use Integer versus integer.
- Can only take either one type of data type for keys, and only one type of data type for values, not multiple types of data types for different values.
Does not refer to key value pairs of data, like in JavaScript.
If static is before a block of code, such as before a hashmap, it will fill in the values, before any code is written.
Hashmap <String> mapper = new Hashmap<>();
static {
mapper.put('{');
package demo;
public class Library {
public boolean someLibraryMethod() {
HashMap<String, Integer> shoeSizes = new HashMap<>();
shoeSizes.put("Jonny", 9); ^---- note that this is verbally said as 'gets' not 'equals'
shoeSizes.put("Peter", 12);
shoeSizes.put("Jackie", 10);
System.out.println(shoeSizes.get("Nicholas"));
}
// To iterate through hash map, iterate through the keys to make life easier. (vice versa is hard)
for (String name : shoeSizes.keySet())
System.out.println(name + ": " + shoeSizes.get(name)); //Johny: 9 ,etc
return true;
}
}