Reputation: 1393
I have a List of Company Entity Object.
package com.raghu.example2;
public class CompanyEntity {
private String name;
private String locationName;
private String officeName;
private String buildingName;
public CompanyEntity(String name, String locationName, String officeName, String buildingName) {
super();
this.name = name;
this.locationName = locationName;
this.officeName = officeName;
this.buildingName = buildingName;
// System.out.println(this);
}
public String getName() {
return name;
}
public String getLocationName() {
return locationName;
}
public String getOfficeName() {
return officeName;
}
public String getBuildingName() {
return buildingName;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("CompanyEntity [name=");
builder.append(name);
builder.append(", locationName=");
builder.append(locationName);
builder.append(", officeName=");
builder.append(officeName);
builder.append(", buildingName=");
builder.append(buildingName);
builder.append("]");
return builder.toString();
}
}
I would like to convert the list of Entities to the Company Object. Company Object is a nested structure.
package com.raghu.example2;
import java.util.List;
public class Company {
private String name;
private List<Location> locationList;
public Company(String name, List<Location> locationList) {
super();
this.name = name;
this.locationList = locationList;
}
public String getName() {
return name;
}
public List<Location> getLocationList() {
return locationList;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Company [name=");
builder.append(name);
builder.append(", locationList=");
builder.append(locationList);
builder.append("]");
return builder.toString();
}
}
Location Object:
package com.raghu.example2;
import java.util.List;
public class Location {
private String locationName;
private List<Office> officeList;
public Location(String locationName, List<Office> officeList) {
super();
this.locationName = locationName;
this.officeList = officeList;
}
public String getLocationName() {
return locationName;
}
public List<Office> getOfficeList() {
return officeList;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((locationName == null) ? 0 : locationName.hashCode());
result = prime * result + ((officeList == null) ? 0 : officeList.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Location other = (Location) obj;
if (locationName == null) {
if (other.locationName != null)
return false;
} else if (!locationName.equals(other.locationName))
return false;
if (officeList == null) {
if (other.officeList != null)
return false;
} else if (!officeList.equals(other.officeList))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Location [locationName=");
builder.append(locationName);
builder.append(", officeList=");
builder.append(officeList);
builder.append("]\n");
return builder.toString();
}
}
Office:
package com.raghu.example2;
import java.util.List;
public class Office {
private String name;
private List<Building> listOfBuilding;
public Office(String name, List<Building> listOfBuilding) {
super();
this.name = name;
this.listOfBuilding = listOfBuilding;
}
public String getName() {
return name;
}
public List<Building> getListOfBuilding() {
return listOfBuilding;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("\n");
builder.append("Office [name=");
builder.append(name);
builder.append(", listOfBuilding=");
builder.append(listOfBuilding);
builder.append("]");
return builder.toString();
}
}
Building:
package com.raghu.example2;
public class Building {
private String name;
private String address;
public Building(String name, String address) {
super();
this.name = name;
this.address = address;
}
public String getName() {
return name;
}
public String getAddress() {
return address;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((address == null) ? 0 : address.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Building other = (Building) obj;
if (address == null) {
if (other.address != null)
return false;
} else if (!address.equals(other.address))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Building [name=");
builder.append(name);
builder.append(", address=");
builder.append(address);
builder.append("]");
return builder.toString();
}
}
List Of Company Entity:
[
CompanyEntity
[name=Mannar&Co, locationName=CHENNAI, officeName=sub-urban, buildingName=sez1], CompanyEntity
[name=Mannar&Co, locationName=CHENNAI, officeName=sub-urban, buildingName=sez2], CompanyEntity
[name=Mannar&Co, locationName=CHENNAI, officeName=urban, buildingName=sez10], CompanyEntity
[name=Mannar&Co, locationName=CHENNAI, officeName=urban, buildingName=sez11], CompanyEntity
[name=Mannar&Co, locationName=BANGALORE, officeName=sub-urban, buildingName=sez1],
CompanyEntity
[name=Mannar&Co, locationName=BANGALORE, officeName=sub-urban, buildingName=sez2],
CompanyEntity [name=Mannar&Co, locationName=BANGALORE, officeName=urban, buildingName=sez10],
CompanyEntity
[name=Mannar&Co, locationName=BANGALORE, officeName=urban, buildingName=sez11]]
Totally there are 8 object. I wanted to group by location, office,building
I want a structure like below.
Company [name=Mannar&Co,
locationList=
[Location [locationName=CHENNAI,
officeList=[
Office [name=sub-urban,
listOfBuilding=
[Building [name=sez1, address=sholinganallur],
Building [name=sez2, address=navallur]]],
Office [name=urban,
listOfBuilding=
[Building [name=sez10, address=t-nagar],
Building [name=sez11, address=velacherry]]]]]
, Location [locationName=BANGALORE,
officeList=[
Office [name=sub-urban,
listOfBuilding=
[Building [name=sez1, address=sarjapur], Building [name=sez2, address=marathahalli]]],
Office [name=urban, listOfBuilding=[Building [name=sez10, address=m.g.road], Building [name=sez11, address=c.v.raman nagar]]]]]
]]
I would like to use java 8 group by clause.
What I have tried so far: Group all the Locations
Map<String,List<CompanyEntity>> map1 =
listOfCompanies.stream().collect(Collectors.groupingBy(CompanyEntity::getLocationName));
map1.forEach((k,v)->System.out.println("\n"+ k + " Group BY " + v + "\n"));
Group all the Offices:
Map<String,List<CompanyEntity>> map2 =listOfCompanies.stream().collect(Collectors.groupingBy(CompanyEntity::getOfficeName));
map2.forEach((k,v)->System.out.println("\n"+ k + " Group BY " + v + "\n"));
How to do a multiple level grouping and get to the nested object?
Upvotes: 2
Views: 10041
Reputation: 119
You can do something like this:
Map<String, Map<String, Map<String, List<String>>>> map = list.stream()
.collect(Collectors.groupingBy(CompanyEntity::getName,
Collectors.groupingBy(CompanyEntity::getLocationName,
Collectors.groupingBy(CompanyEntity::getOfficeName,
Collectors.mapping(CompanyEntity::getBuildingName, Collectors.toCollection(ArrayList::new))))));
Map output:
{Mannar&Co={CHENNAI={urban=[sez10, sez11], sub-urban=[sez1, sez2]}, BANGALORE={urban=[sez10, sez11], sub-urban=[sez1, sez1]}}}
Then fill Company object from that map
Upvotes: 5