diff --git a/java-collections-maps-2/README.md b/java-collections-maps-2/README.md index ff84e93ce4..3740639339 100644 --- a/java-collections-maps-2/README.md +++ b/java-collections-maps-2/README.md @@ -1,3 +1,4 @@ ## Relevant Articles: - [Map of Primitives in Java](https://www.baeldung.com/java-map-primitives) - [Copying a HashMap in Java](https://www.baeldung.com/java-copy-hashmap) +- [Guide to Java HashMap]() diff --git a/java-collections-maps-2/src/main/java/com/baeldung/map/Product.java b/java-collections-maps-2/src/main/java/com/baeldung/map/Product.java new file mode 100644 index 0000000000..5559895730 --- /dev/null +++ b/java-collections-maps-2/src/main/java/com/baeldung/map/Product.java @@ -0,0 +1,133 @@ +package com.baeldung.map; + +import java.util.*; + +public class Product { + + private String name; + private String description; + private List tags; + + public Product(String name, String description) { + this.name = name; + this.description = description; + this.tags = new ArrayList<>(); + } + + public String getName() { + return name; + } + + public String getDescription() { + return description; + } + + public List getTags() { + return tags; + } + + public Product addTagsOfOtherProdcut(Product product) { + this.tags.addAll(product.getTags()); + return this; + } + + @Override + public boolean equals(Object o) { + + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + Product product = (Product) o; + return Objects.equals(name, product.name) && + Objects.equals(description, product.description); + } + + @Override + public int hashCode() { + return Objects.hash(name, description); + } + + public static void forEach() { + + HashMap productsByName = new HashMap<>(); + productsByName.forEach( (key, product) + -> System.out.println("Key: " + key + " Product:" + product.getDescription()) + //do something with the key and value + ); + + //Prior to Java 8: + for(Map.Entry entry : productsByName.entrySet()) { + Product product = entry.getValue(); + String key = entry.getKey(); + //do something with the key and value + } + } + + public static void getOrDefault() { + + HashMap productsByName = new HashMap<>(); + Product chocolate = new Product("chocolate", "something sweet"); + Product defaultProduct = productsByName.getOrDefault("horse carriage", chocolate); + Product bike = productsByName.getOrDefault("E-Bike", chocolate); + + //Prior to Java 8: + Product bike2 = productsByName.containsKey("E-Bike") + ? productsByName.get("E-Bike") + : chocolate; + Product defaultProduct2 = productsByName.containsKey("horse carriage") + ? productsByName.get("horse carriage") + : chocolate; + } + + public static void putIfAbsent() { + + HashMap productsByName = new HashMap<>(); + Product chocolate = new Product("chocolate", "something sweet"); + productsByName.putIfAbsent("E-Bike", chocolate); + + //Prior to Java 8: + if(productsByName.containsKey("E-Bike")) { + productsByName.put("E-Bike", chocolate); + } + } + + public static void merge() { + + HashMap productsByName = new HashMap<>(); + Product eBike2 = new Product("E-Bike", "A bike with a battery"); + eBike2.getTags().add("sport"); + productsByName.merge("E-Bike", eBike2, Product::addTagsOfOtherProdcut); + + //Prior to Java 8: + if(productsByName.containsKey("E-Bike")) { + productsByName.get("E-Bike").addTagsOfOtherProdcut(eBike2); + } else { + productsByName.put("E-Bike", eBike2); + } + } + + public static void compute() { + + HashMap productsByName = new HashMap<>(); + Product eBike2 = new Product("E-Bike", "A bike with a battery"); + + productsByName.compute("E-Bike", (k,v) -> { + if(v != null) { + return v.addTagsOfOtherProdcut(eBike2); + } else { + return eBike2; + } + }); + + //Prior to Java 8: + if(productsByName.containsKey("E-Bike")) { + productsByName.get("E-Bike").addTagsOfOtherProdcut(eBike2); + } else { + productsByName.put("E-Bike", eBike2); + } + } +} diff --git a/java-collections-maps-2/src/test/java/com/baeldung/map/ProductUnitTest.java b/java-collections-maps-2/src/test/java/com/baeldung/map/ProductUnitTest.java new file mode 100644 index 0000000000..28ac06e166 --- /dev/null +++ b/java-collections-maps-2/src/test/java/com/baeldung/map/ProductUnitTest.java @@ -0,0 +1,124 @@ +package com.baeldung.map; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; + +import static org.junit.jupiter.api.Assertions.*; + +class ProductUnitTest { + + + @Test + public void getExistingValue() { + HashMap productsByName = new HashMap<>(); + + Product eBike = new Product("E-Bike", "A bike with a battery"); + Product roadBike = new Product("Road bike", "A bike for competition"); + + productsByName.put(eBike.getName(), eBike); + productsByName.put(roadBike.getName(), roadBike); + + Product nextPurchase = productsByName.get("E-Bike"); + + assertEquals("A bike with a battery", nextPurchase.getDescription()); + } + + @Test + public void getNonExistingValue() { + HashMap productsByName = new HashMap<>(); + + Product eBike = new Product("E-Bike", "A bike with a battery"); + Product roadBike = new Product("Road bike", "A bike for competition"); + + productsByName.put(eBike.getName(), eBike); + productsByName.put(roadBike.getName(), roadBike); + + Product nextPurchase = productsByName.get("Car"); + + assertEquals(null, nextPurchase); + } + + @Test + public void getExistingValueAfterSameKeyInsertedTwice() { + HashMap productsByName = new HashMap<>(); + + Product eBike = new Product("E-Bike", "A bike with a battery"); + Product roadBike = new Product("Road bike", "A bike for competition"); + Product newEBike = new Product("E-Bike", "A bike with a better battery"); + + productsByName.put(eBike.getName(), eBike); + productsByName.put(roadBike.getName(), roadBike); + productsByName.put(newEBike.getName(), newEBike); + + Product nextPurchase = productsByName.get("E-Bike"); + + assertEquals("A bike with a better battery", nextPurchase.getDescription()); + } + + @Test + public void getExistingValueWithNullKey() { + HashMap productsByName = new HashMap<>(); + + Product defaultProduct = new Product("Chocolate", "At least buy chocolate"); + + productsByName.put(null, defaultProduct); + productsByName.put(defaultProduct.getName(), defaultProduct); + + Product nextPurchase = productsByName.get(null); + assertEquals("At least buy chocolate", nextPurchase.getDescription()); + + nextPurchase = productsByName.get("Chocolate"); + assertEquals("At least buy chocolate", nextPurchase.getDescription()); + } + + @Test + public void insertSameObjectWithDifferentKey() { + HashMap productsByName = new HashMap<>(); + + Product defaultProduct = new Product("Chocolate", "At least buy chocolate"); + + productsByName.put(null, defaultProduct); + productsByName.put(defaultProduct.getName(), defaultProduct); + + assertSame(productsByName.get(null), productsByName.get("Chocolate")); + } + + @Test + public void checkIfKeyExists() { + HashMap productsByName = new HashMap<>(); + + Product eBike = new Product("E-Bike", "A bike with a battery"); + + productsByName.put(eBike.getName(), eBike); + + assertTrue(productsByName.containsKey("E-Bike")); + } + + @Test + public void checkIfValueExists() { + HashMap productsByName = new HashMap<>(); + + Product eBike = new Product("E-Bike", "A bike with a battery"); + + productsByName.put(eBike.getName(), eBike); + + assertTrue(productsByName.containsValue(eBike)); + } + + @Test + public void removeExistingKey() { + HashMap productsByName = new HashMap<>(); + + Product eBike = new Product("E-Bike", "A bike with a battery"); + Product roadBike = new Product("Road bike", "A bike for competition"); + + productsByName.put(eBike.getName(), eBike); + productsByName.put(roadBike.getName(), roadBike); + + productsByName.remove("E-Bike"); + + assertNull(productsByName.get("E-Bike")); + } + +} \ No newline at end of file