-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHashRunner.java
More file actions
65 lines (48 loc) · 1.75 KB
/
HashRunner.java
File metadata and controls
65 lines (48 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package qa.com.hashMap;
import java.util.HashMap;
import java.util.Map.Entry;
// Hashmaps
// Allow you to store information in a collection (Lists, ArrayLists, Maps)
// You store information with a index (Key) and data (Value) pair
// A list of key value pairs (can be made up of any data types)
public class HashRunner {
public static void main(String[] args) {
// We are creating a HashMap which takes in a String and a String
// Its called currencies
// Its created by returning HashMap<>()
HashMap<String, String> currencies = new HashMap<>();
// Add data to my HashMap by using put()
currencies.put("United Kingdom", "Pound Sterling");
currencies.put("France", "Euro");
currencies.put("Internet", "Dogecoin");
// Prints out the map with all the values
System.out.println(currencies);
// {United Kingdom = currency@com.qa.hash67272b29}
// Happens if the object passing in doesnt have a toString
// Returns the length of the map
System.out.println(currencies.size());
// Get
System.out.println(currencies.get("Internet"));
// Remove
// currencies.remove("France");
System.out.println(currencies);
// Looping
// keySet() = The key (left of the map)
// values() = The values (right of the map)
// entrySet() = The entire thing
for(String i : currencies.keySet()) {
System.out.println(i);
// currencies.remove(i);
}
for(String i : currencies.values()) {
System.out.println(i);
}
for(Entry<String, String> i : currencies.entrySet()) {
System.out.println(i);
}
// Hashmaps just like arraylists / lists can take in custom objects
HashMap<Country, String> currencies2 = new HashMap<>();
currencies2.put(new Country("USA"), "Dollars");
System.out.println(currencies2);
}
}