1// Import the HashMap class
2import java.util.HashMap;
3
4 // First Example
5 // Create a HashMap object called capitalCities
6 HashMap<String, String> capitalCities = new HashMap<String, String>();
7
8 // Add keys and values (Country, City)
9 capitalCities.put("England", "London");
10 capitalCities.put("Germany", "Berlin");
11 capitalCities.put("Norway", "Oslo");
12 capitalCities.put("USA", "Washington DC");
13 System.out.println(capitalCities);
14
15 //Second Example
16 // Create a HashMap object called stGrade
17 Map<String, Integer> stGrade = new HashMap<String, Integer>();
18
19 // Insert elements
20 stGrade.put("aaron", new Integer(90));
21 stGrade.put("isaac", new Integer(100));
22 stGrade.put("john", new Integer(35));
23 stGrade.put("mohammad", new Integer(100));
24
25 // Get value
26 stGrade.get("mohammad"); // returns 100
27 stGrade.get("aaron"); // returns 90
28 stGrade.get("john"); // returns 35
29 stGrade.get("isaac"); // returns 100
30
1import java.util.HashMap; // import the HashMap class
2
3// instantiate a HashMap instance
4HashMap<String, String> capitalCities = new HashMap<String, String>();
5
6// SCROLL DOWN FOR LIST OF ALL METHODS --------------------------------------
7// to use any methods below add the function name to the instance name
8// General Form: HashMapInstanceName.{functionName}({parameter list});
9// Specific Example Using the Put Method:
10capitalCities.put("California", "Sacramento");
11
12// SCROLL DOWN FOR LIST OF ALL METHODS --------------------------------------
13
141. void clear()
15 - Removes all of the mappings from this map.
16
172. Object clone()
18 - Returns a shallow copy of this HashMap instance: the keys and values themselves are not cloned.
19
203. boolean containsKey(Object key)
21 - Returns true if this map contains a mapping for the specified key.
22
234. boolean containsValue(Object value)
24 - Returns true if this map maps one or more keys to the specified value.
25
265. V get(Object key)
27 - Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
28
296. V getOrDefault(Object key, V defaultValue)
30 - Returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key.
31
327. boolean isEmpty()
33 - Returns true if this map contains no key-value mappings.
34
358. Set<K> keySet()
36 - Returns a Set view of the keys contained in this map.
37
389. V put(K key, V value)
39 - Associates the specified value with the specified key in this map.
40
4110. V remove(Object key)
42 - Removes the mapping for the specified key from this map if present.
43
4411. boolean remove(Object key, Object value)
45 - Removes the entry for the specified key only if it is currently mapped to the specified value.
46
4712. V replace(K key, V value)
48 - Replaces the entry for the specified key only if it is currently mapped to some value.
49
5013. boolean replace(K key, V oldValue, V newValue)
51 - Replaces the entry for the specified key only if currently mapped to the specified value.
52
5314. int size()
54 - Returns the number of key-value mappings in this map.
55
5615. Collection<V> values()
57 - Returns a Collection view of the values contained in this map.
1//remember to first import java.util.*; first
2
3//you can swap out string or integer for other data types
4Map<String, Integer> d = new HashMap<>();
5
1//Hash map creation
2Map< String,Integer> hm =
3 new HashMap< String,Integer>();
4//inserting elements into hashmap
5 hm.put("a", new Integer(100));
6 hm.put("b", new Integer(200));
7 hm.put("c", new Integer(300));
8 hm.put("d", new Integer(400));
1HashMap<Integer,String> map=new HashMap<Integer,String>();//key is integer, value is String