BAEL 3131 guide to java hash map (#7758)

* BAEL-3131 Guide to Java HashMap

* Update java-collections-maps-2/src/test/java/com/baeldung/map/ProductUnitTest.java

Co-Authored-By: KevinGilmore <kpg102@gmail.com>
This commit is contained in:
maibin 2019-10-01 13:48:51 -07:00 committed by GitHub
commit 84109faec8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 258 additions and 0 deletions

View File

@ -1,6 +1,7 @@
## 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]()
- [Guide to WeakHashMap in Java](https://www.baeldung.com/java-weakhashmap)
- [Map to String Conversion in Java](https://www.baeldung.com/java-map-to-string-conversion)
- [Iterate over a Map in Java](https://www.baeldung.com/java-iterate-map)

View File

@ -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);
}
}
}

View File

@ -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"));
}
}