From 712cb3caca9d7b3d703357f4ef563a5f63484a05 Mon Sep 17 00:00:00 2001 From: reece-elder Date: Fri, 21 Jan 2022 13:28:42 +0000 Subject: [PATCH] hash maps --- .../src/qa/com/hashMap/Country.java | 28 ++++++++ .../src/qa/com/hashMap/HashRunner.java | 65 ++++++++++++++++++ .../classes/qa/com/hashMap/Country.class | Bin 0 -> 784 bytes .../classes/qa/com/hashMap/HashRunner.class | Bin 0 -> 2065 bytes 4 files changed, 93 insertions(+) create mode 100644 CodeBase/Java_Intermediate/src/qa/com/hashMap/Country.java create mode 100644 CodeBase/Java_Intermediate/src/qa/com/hashMap/HashRunner.java create mode 100644 CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/Country.class create mode 100644 CodeBase/Java_Intermediate/target/classes/qa/com/hashMap/HashRunner.class 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 0000000000000000000000000000000000000000..3d589bbbd237fae4e9066d5ae601dfae43cfe94d GIT binary patch literal 784 zcmZvZ&raJw5XQerOl-C$5CaLJ|DY{2qyis%pyfa$PANT5Ayrimn`D(3C3fmKNPVkT zYC!_25739II^(!03c1YA?#}*ZzHfj3x%ma)HD21t5z1e*-;M@;Pmg-<^w4ia<1mT8 z+OP;K7y45BeI0iF_pJ-lP6$@02ZrD^|6iRYaS(P733k^cr|I=tZEs$I)rdM~5rhXW z7EmB8H-pfejt4Chf6%Rd3Xa;kf3D*|_SvD8^nwv#qd9+8X+8SadcAhP^WHh3@Foa@ z?SM)0zukpOYMgV&_-)N4Fzien-(ZG1*EmsB^<-TW*0UaEcFlc z&ReH{z{`$agu%|Jcb)OZ%iig=f5Q5lO~{AAnNDWTIs1OU&-;hBkpC zTV^un7Lsdbac$PzOy1;ux8yj|(-0EqSvR-Mq-{F+6N>_& zw7V)JIE1i)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