forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL 3131 guide to java hash map (eugenp#7758)
* BAEL-3131 Guide to Java HashMap * Update java-collections-maps-2/src/test/java/com/baeldung/map/ProductUnitTest.java Co-Authored-By: KevinGilmore <[email protected]>
- Loading branch information
Showing
3 changed files
with
258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
java-collections-maps-2/src/main/java/com/baeldung/map/Product.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package com.baeldung.map; | ||
|
||
import java.util.*; | ||
|
||
public class Product { | ||
|
||
private String name; | ||
private String description; | ||
private List<String> 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<String> 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<String, Product> 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<String, Product> entry : productsByName.entrySet()) { | ||
Product product = entry.getValue(); | ||
String key = entry.getKey(); | ||
//do something with the key and value | ||
} | ||
} | ||
|
||
public static void getOrDefault() { | ||
|
||
HashMap<String, Product> 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<String, Product> 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<String, Product> 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<String, Product> 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); | ||
} | ||
} | ||
} |
124 changes: 124 additions & 0 deletions
124
java-collections-maps-2/src/test/java/com/baeldung/map/ProductUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<String, Product> 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<String, Product> 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"); | ||
|
||
assertNull(nextPurchase); | ||
} | ||
|
||
@Test | ||
public void getExistingValueAfterSameKeyInsertedTwice() { | ||
HashMap<String, Product> 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<String, Product> 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<String, Product> 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<String, Product> 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<String, Product> 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<String, Product> 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")); | ||
} | ||
|
||
} |