diff --git a/CodeBase/Java_Intermediate/src/qa/com/hashMap/Country.java b/CodeBase/Java_Intermediate/src/qa/com/hashMap/Country.java new file mode 100644 index 0000000..b1ea6b8 --- /dev/null +++ b/CodeBase/Java_Intermediate/src/qa/com/hashMap/Country.java @@ -0,0 +1,28 @@ +package qa.com.hashMap; + +public class Country { + + private String name; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Country(String name) { + super(); + this.name = name; + } + + @Override + public String toString() { + return "Country [name=" + name + "]"; + } + + + + +} diff --git a/CodeBase/Java_Intermediate/src/qa/com/hashMap/HashRunner.java b/CodeBase/Java_Intermediate/src/qa/com/hashMap/HashRunner.java new file mode 100644 index 0000000..12678f1 --- /dev/null +++ b/CodeBase/Java_Intermediate/src/qa/com/hashMap/HashRunner.java @@ -0,0 +1,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 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 i : currencies.entrySet()) { + System.out.println(i); + } + + // Hashmaps just like arraylists / lists can take in custom objects + + HashMap currencies2 = new HashMap<>(); + currencies2.put(new Country("USA"), "Dollars"); + System.out.println(currencies2); + + } + +} diff --git a/CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/Country.class b/CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/Country.class new file mode 100644 index 0000000..3d589bb Binary files /dev/null and b/CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/Country.class differ diff --git a/CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/HashRunner.class b/CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/HashRunner.class new file mode 100644 index 0000000..d4ef4e6 Binary files /dev/null and b/CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/HashRunner.class differ