changes for BAEL 5164 (#11364)

* BAEL 4293 correcting the unit test file ame.

* changes for BAEL 5164

* added maven compiler properties for fixing compilation issues.

* making BAEL 5164 java 8 compatible.
This commit is contained in:
rvsathe 2021-11-08 02:45:45 +05:30 committed by GitHub
parent 35ea11e64f
commit d8ba53a958
6 changed files with 543 additions and 29 deletions

View File

@ -1,34 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-maps-4</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-maps-4</name>
<packaging>jar</packaging>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-collections-maps-4</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-collections-maps-4</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- those tests are not made for CI purposes, but for manual testing -->
<groups>!performance</groups>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<!-- those tests are not made for CI purposes, but for manual testing -->
<groups>!performance</groups>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>

View File

@ -0,0 +1,32 @@
package com.baeldung.nestedhashmaps;
public class Address {
private Integer addressId;
private String addressLocation;
public Address() {
}
public Address(Integer addressId, String addressLocation) {
this.addressId = addressId;
this.addressLocation = addressLocation;
}
public Integer getAddressId() {
return addressId;
}
public void setAddressId(Integer addressId) {
this.addressId = addressId;
}
public String getAddressLocation() {
return addressLocation;
}
public void setAddressLocation(String addressLocation) {
this.addressLocation = addressLocation;
}
}

View File

@ -0,0 +1,43 @@
package com.baeldung.nestedhashmaps;
public class Employee {
private Integer employeeId;
private Address address;
private String employeeName;
public Employee() {
super();
}
public Employee(Integer employeeId, Address address, String employeeName) {
this.employeeId = employeeId;
this.address = address;
this.employeeName = employeeName;
}
public Integer getEmployeeId() {
return employeeId;
}
public void setEmployeeId(Integer employeeId) {
this.employeeId = employeeId;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public String getEmployeeName() {
return employeeName;
}
public void setEmployeeName(String employeeName) {
this.employeeName = employeeName;
}
}

View File

@ -0,0 +1,57 @@
package com.baeldung.nestedhashmaps;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;
public class MapsUtil {
MapsUtil() {
super();
}
public Map<Integer, String> buildInnerMap(List<String> batterList) {
Map<Integer, String> innerBatterMap = new HashMap<Integer, String>();
int index = 1;
for (String item : batterList) {
innerBatterMap.put(index, item);
index++;
}
return innerBatterMap;
}
public Map<Integer, Map<String, String>> createNestedMapfromStream(List<Employee> listEmployee) {
Map<Integer, Map<String, String>> employeeAddressMap = listEmployee.stream()
.collect(Collectors.groupingBy(e -> e.getAddress().getAddressId(),
Collectors.toMap(f -> f.getAddress().getAddressLocation(), Employee::getEmployeeName)));
return employeeAddressMap;
}
public Map<Integer, Map<Integer, Address>> createNestedObjectMap(List<Employee> listEmployee) {
Map<Integer, Map<Integer, Address>> employeeMap = new HashMap<>();
employeeMap = listEmployee.stream().collect(Collectors.groupingBy((Employee emp) -> emp.getEmployeeId(),
Collectors.toMap((Employee emp) -> emp.getAddress().getAddressId(), fEmpObj -> fEmpObj.getAddress())));
return employeeMap;
}
public Map<String, String> flattenMap(Map<?, ?> source) {
Map<String, String> converted = new HashMap<>();
for (Entry<?, ?> entry : source.entrySet()) {
if (entry.getValue() instanceof Map) {
flattenMap((Map<String, Object>) entry.getValue())
.forEach((key, value) -> converted.put(entry.getKey() + "." + key, value));
} else {
converted.put(entry.getKey().toString(), entry.getValue().toString());
}
}
return converted;
}
}

View File

@ -0,0 +1,118 @@
package com.baeldung.nestedhashmaps;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
public class NestedHashMapExamplesClass {
public static void main(String[] args) {
MapsUtil mUtil = new MapsUtil();
List<String> batterList = new ArrayList<>();
Map<String, Map<Integer, String>> outerBakedGoodsMap = new HashMap<>();
Map<String, Map<Integer, String>> outerBakedGoodsMap2 = new HashMap<>();
Map<String, Map<Integer, String>> outerBakedGoodsMap3 = new HashMap<>();
Map<String, Map<Integer, String>> outerBakedGoodsMap4 = new HashMap<>();
batterList.add("Mulberry");
batterList.add("Cranberry");
batterList.add("Blackberry");
batterList.add("Mixed fruit");
batterList.add("Orange");
outerBakedGoodsMap.put("Cake", mUtil.buildInnerMap(batterList));
batterList.clear();
batterList.add("Candy");
batterList.add("Dark Chocolate");
batterList.add("Chocolate");
batterList.add("Jam filled");
batterList.add("Pineapple");
outerBakedGoodsMap.put("Donut", mUtil.buildInnerMap(batterList));
outerBakedGoodsMap2.put("Eclair", mUtil.buildInnerMap(batterList));
outerBakedGoodsMap2.put("Donut", mUtil.buildInnerMap(batterList));
outerBakedGoodsMap3.put("Cake", mUtil.buildInnerMap(batterList));
batterList.clear();
batterList.add("Banana");
batterList.add("Red Velvet");
batterList.add("Blackberry");
batterList.add("Passion fruit");
batterList.add("Kiwi");
outerBakedGoodsMap3.put("Donut", mUtil.buildInnerMap(batterList));
outerBakedGoodsMap4.putAll(outerBakedGoodsMap);
System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap2));
System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap3));
System.out.println(outerBakedGoodsMap.equals(outerBakedGoodsMap4));
outerBakedGoodsMap.get("Cake")
.put(6, "Cranberry");
System.out.println(outerBakedGoodsMap);
outerBakedGoodsMap.get("Cake")
.remove(5);
System.out.println(outerBakedGoodsMap);
outerBakedGoodsMap.put("Eclair", new HashMap<Integer, String>() {
{
put(1, "Dark Chocolate");
}
});
System.out.println(outerBakedGoodsMap);
outerBakedGoodsMap.remove("Eclair");
System.out.println(outerBakedGoodsMap);
System.out.println("Baked Goods Map Flattened: " + mUtil.flattenMap(outerBakedGoodsMap));
// Employees Map
List<Employee> listEmployee = new ArrayList<Employee>();
listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
listEmployee.add(new Employee(3, new Address(156, "Bramles Lane"), "Bofur"));
listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
Map<Integer, Map<String, String>> employeeAddressMap = mUtil.createNestedMapfromStream(listEmployee);
Map<Integer, Map<Integer, Address>> employeeMap = mUtil.createNestedObjectMap(listEmployee);
Map<Integer, Map<Integer, Address>> employeeMap2 = mUtil.createNestedObjectMap(listEmployee);
listEmployee.clear();
listEmployee.add(new Employee(1, new Address(500, "Timbuktoo"), "Thorin Oakenshield"));
listEmployee.add(new Employee(2, new Address(600, "Misty Lanes"), "Balin"));
listEmployee.add(new Employee(3, new Address(700, "Bramles Lane"), "Bofur"));
listEmployee.add(new Employee(4, new Address(800, "Bag End"), "Bilbo Baggins"));
listEmployee.add(new Employee(5, new Address(900, "Rivendell"), "Elrond"));
Map<Integer, Map<String, String>> employeeAddressMap1 = mUtil.createNestedMapfromStream(listEmployee);
Map<Integer, Map<Integer, Address>> employeeMap1 = mUtil.createNestedObjectMap(listEmployee);
System.out.println(employeeMap.equals(employeeMap1));
System.out.println(employeeMap.equals(employeeMap2));
for (Map.Entry<String, Map<Integer, String>> outerBakedGoodsMapEntrySet : outerBakedGoodsMap.entrySet()) {
Map<Integer, String> valueMap = outerBakedGoodsMapEntrySet.getValue();
System.out.println(valueMap.entrySet());
}
for (Map.Entry<Integer, Map<String, String>> employeeEntrySet : employeeAddressMap.entrySet()) {
Map<String, String> valueMap = employeeEntrySet.getValue();
System.out.println(valueMap.entrySet());
}
System.out.println("Employee Address Map Flattened: " + mUtil.flattenMap(employeeAddressMap));
System.out.println(employeeAddressMap.equals(employeeAddressMap1));
}
}

View File

@ -0,0 +1,243 @@
package com.baeldung.nestedhashmaps;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertThat;
import org.hamcrest.collection.IsMapContaining;
public class NestedHashMapExamplesClassUnitTest {
private MapsUtil mUtil = new MapsUtil();
private List<String> batterList = new ArrayList<>();
private List<Employee> listEmployee = new ArrayList<Employee>();
private Map<String, Map<Integer, String>> actualBakedGoodsMap = new HashMap<>();
private Map<Integer, Map<String, String>> actualEmployeeAddressMap = new HashMap<>();
private Map<Integer, Map<Integer, Address>> actualEmployeeMap = new HashMap<>();
@Test
public void whenCreateNestedHashMap_thenNestedMap() {
assertThat(mUtil.buildInnerMap(batterList), is(notNullValue()));
Assert.assertEquals(actualBakedGoodsMap.keySet().size(), 2);
Assert.assertThat(actualBakedGoodsMap, IsMapContaining.hasValue(equalTo(mUtil.buildInnerMap(batterList))));
}
private Map<Integer, Map<String, String>> setup() {
Map<Integer, Map<String, String>> expectedMap = new HashMap<>();
expectedMap.put(Integer.valueOf(100), new HashMap<String, String>() {
{
put("Misty Lanes", "Balin");
}
});
expectedMap.put(Integer.valueOf(200), new HashMap<String, String>() {
{
put("Bag End", "Bilbo Baggins");
}
});
expectedMap.put(Integer.valueOf(156), new HashMap<String, String>() {
{
put("Brambles Lane", "Bofur");
}
});
expectedMap.put(Integer.valueOf(124), new HashMap<String, String>() {
{
put("Timbuktoo", "Thorin Oakenshield");
}
});
expectedMap.put(Integer.valueOf(23), new HashMap<String, String>() {
{
put("Rivendell", "Elrond");
}
});
return expectedMap;
}
@Test
public void whenCreateNestedHashMapwithStreams_thenNestedMap() {
Map<Integer, Map<String, String>> expectedMap = setup();
assertThat(actualEmployeeAddressMap, equalTo(expectedMap));
}
@Test
public void whenCompareTwoHashMapswithDifferenctValues_usingEquals_thenFail() {
Map<String, Map<Integer, String>> outerBakedGoodsMap2 = new HashMap<>();
outerBakedGoodsMap2.put("Eclair", mUtil.buildInnerMap(batterList));
outerBakedGoodsMap2.put("Donut", mUtil.buildInnerMap(batterList));
assertNotEquals(outerBakedGoodsMap2, actualBakedGoodsMap);
Map<String, Map<Integer, String>> outerBakedGoodsMap3 = new HashMap<String, Map<Integer, String>>();
outerBakedGoodsMap3.put("Cake", mUtil.buildInnerMap(batterList));
batterList = new ArrayList<>();
batterList = Arrays.asList("Banana", "Red Velvet", "Blackberry", "Passion fruit", "Kiwi");
outerBakedGoodsMap3.put("Donut", mUtil.buildInnerMap(batterList));
assertNotEquals(outerBakedGoodsMap2, actualBakedGoodsMap);
listEmployee.clear();
listEmployee.add(new Employee(1, new Address(500, "Timbuktoo"), "Thorin Oakenshield"));
listEmployee.add(new Employee(2, new Address(600, "Misty Lanes"), "Balin"));
listEmployee.add(new Employee(3, new Address(700, "Bramles Lane"), "Bofur"));
listEmployee.add(new Employee(4, new Address(800, "Bag End"), "Bilbo Baggins"));
listEmployee.add(new Employee(5, new Address(900, "Rivendell"), "Elrond"));
Map<Integer, Map<String, String>> employeeAddressMap1 = mUtil.createNestedMapfromStream(listEmployee);
Map<Integer, Map<Integer, Address>> employeeMap1 = mUtil.createNestedObjectMap(listEmployee);
assertNotEquals(employeeAddressMap1, actualEmployeeAddressMap);
assertNotEquals(employeeMap1, actualEmployeeMap);
}
@Test
public void whencomparingDifferentObjectValuesUsingEquals_thenFail() {
listEmployee.clear();
listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
listEmployee.add(new Employee(3, new Address(156, "Brambles Lane"), "Bofur"));
listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
Map<Integer, Map<Integer, Object>> employeeMap1 = listEmployee.stream().collect(Collectors.groupingBy(
(Employee emp) -> emp.getEmployeeId(),
Collectors.toMap((Employee emp) -> emp.getAddress().getAddressId(), fEmpObj -> fEmpObj.getAddress())));
assertNotSame(employeeMap1, actualEmployeeMap);
assertNotEquals(employeeMap1, actualEmployeeMap);
Map<Integer, Map<Integer, Address>> expectedMap = setupAddressObjectMap();
assertNotSame(expectedMap, actualEmployeeMap);
assertNotEquals(expectedMap, actualEmployeeMap);
}
@Test
public void whenCompareTwoHashMapsUsingEquals_thenSuccess() {
Map<String, Map<Integer, String>> outerBakedGoodsMap4 = new HashMap<>();
outerBakedGoodsMap4.putAll(actualBakedGoodsMap);
assertEquals(actualBakedGoodsMap, outerBakedGoodsMap4);
Map<Integer, Map<Integer, Address>> employeeMap1 = new HashMap<>();
employeeMap1.putAll(actualEmployeeMap);
assertEquals(actualEmployeeMap, employeeMap1);
}
@Test
public void whenAddElementinHashMaps_thenSuccess() {
assertEquals(actualBakedGoodsMap.get("Cake").size(), 5);
actualBakedGoodsMap.get("Cake").put(6, "Cranberry");
assertEquals(actualBakedGoodsMap.get("Cake").size(), 6);
}
@Test
public void whenDeleteElementinHashMaps_thenSuccess() {
assertNotEquals(actualBakedGoodsMap.get("Cake").get(5), null);
actualBakedGoodsMap.get("Cake").remove(5);
assertEquals(actualBakedGoodsMap.get("Cake").get(5), null);
actualBakedGoodsMap.put("Eclair", new HashMap<Integer, String>() {
{
put(1, "Dark Chocolate");
}
});
assertNotEquals(actualBakedGoodsMap.get("Eclair").get(1), null);
actualBakedGoodsMap.get("Eclair").remove(1);
assertEquals(actualBakedGoodsMap.get("Eclair").get(1), null);
actualBakedGoodsMap.put("Eclair", new HashMap<Integer, String>() {
{
put(1, "Dark Chocolate");
}
});
assertNotEquals(actualBakedGoodsMap.get("Eclair"), null);
actualBakedGoodsMap.remove("Eclair");
assertEquals(actualBakedGoodsMap.get("Eclair"), null);
}
@Test
public void whenFlattenMap_thenRemovesNesting() {
Map<String, String> flattenedBakedGoodsMap = mUtil.flattenMap(actualBakedGoodsMap);
assertThat(flattenedBakedGoodsMap, IsMapContaining.hasKey("Donut.2"));
Map<String, String> flattenedEmployeeAddressMap = mUtil.flattenMap(actualEmployeeAddressMap);
assertThat(flattenedEmployeeAddressMap, IsMapContaining.hasKey("200.Bag End"));
}
@Before
public void buildMaps() {
batterList = Arrays.asList("Mulberry", "Cranberry", "Blackberry", "Mixed fruit", "Orange");
actualBakedGoodsMap.put("Cake", mUtil.buildInnerMap(batterList));
batterList = new ArrayList<>();
batterList = Arrays.asList("Candy", "Dark Chocolate", "Chocolate", "Jam filled", "Pineapple");
actualBakedGoodsMap.put("Donut", mUtil.buildInnerMap(batterList));
listEmployee.add(new Employee(1, new Address(124, "Timbuktoo"), "Thorin Oakenshield"));
listEmployee.add(new Employee(2, new Address(100, "Misty Lanes"), "Balin"));
listEmployee.add(new Employee(3, new Address(156, "Brambles Lane"), "Bofur"));
listEmployee.add(new Employee(4, new Address(200, "Bag End"), "Bilbo Baggins"));
listEmployee.add(new Employee(5, new Address(23, "Rivendell"), "Elrond"));
actualEmployeeAddressMap = mUtil.createNestedMapfromStream(listEmployee);
actualEmployeeMap = mUtil.createNestedObjectMap(listEmployee);
}
private Map<Integer, Map<Integer, Address>> setupAddressObjectMap() {
Map<Integer, Map<Integer, Address>> expectedMap = new HashMap<>();
expectedMap.put(1, new HashMap<Integer, Address>() {
{
put(124, new Address(124, "Timbuktoo"));
}
});
expectedMap.put(2, new HashMap<Integer, Address>() {
{
put(100, new Address(100, "Misty Lanes"));
}
});
expectedMap.put(3, new HashMap<Integer, Address>() {
{
put(156, new Address(156, "Brambles Lane"));
}
});
expectedMap.put(4, new HashMap<Integer, Address>() {
{
put(200, new Address(200, "Bag End"));
}
});
expectedMap.put(5, new HashMap<Integer, Address>() {
{
put(23, new Address(23, "Rivendell"));
}
});
return expectedMap;
}
}