Presentation is loading. Please wait.

Presentation is loading. Please wait.

Use of Java’s HashMap.

Similar presentations


Presentation on theme: "Use of Java’s HashMap."— Presentation transcript:

1 Use of Java’s HashMap

2 Purpose, Declaration A HashMap object in Java allows the user to create a lookup table or their own function, where the type of the input is arbitrary. It’s exactly like a dictionary in Python. Example Declaration HashMap<String,Integer> map = new HashMap<String,Integer>(); How to add a pair to a HashMap map.put(“Harold”, 7);

3 HashMap Methods – get, containsKey
map.get(“Harold”) will return the associated value with “Harold” Produces a run-time error if “Harold” isn’t a valid key. containsKey map.containsKey(“Harold”) will allow the user to see if “Harold” is a key in the map.

4 HashMap Methods – keySet, remove
Usually used like this: for (String s: map.keySet()) Iterates through all keys in the map in no particular order. remove Map.remove(“Harold”) Run time error if key isn’t in the map.

5 HashMap Methods – size, values
map.size() returns the number of (key, value) pairs in the map. values map.values() returns a Collection of all the values in the map. (Might contain repeats.)


Download ppt "Use of Java’s HashMap."

Similar presentations


Ads by Google