2017-05-27 165 views
2

我有一個公司實體對象列表。Group by by Java 8 Lambda表達式

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

我想將實體列表轉換爲公司對象。公司對象是嵌套結構。

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

位置對象:

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



} 

辦公室:

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



} 

大廈:

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

列表公司實體:

[ 
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]] 

完全有8個對象。我想按地點,辦公室,建築羣分組

我想要一個如下結構。

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]]]]] 
]] 

我想用java 8 group by子句。

我迄今爲止嘗試: 集團所有位置

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

集團所有的辦事處:

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

如何做一個多級分組,並獲得了嵌套的對象嗎?

回答

4

你可以做這樣的事情:

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

地圖輸出:

{Mannar&Co={CHENNAI={urban=[sez10, sez11], sub-urban=[sez1, sez2]}, BANGALORE={urban=[sez10, sez11], sub-urban=[sez1, sez1]}}} 

然後從地圖填補公司物體

+1

'Collectors.toCollection(ArrayList的::新)'能替換爲'Collectors.toList()' – Andrew

+2

@AndrewTobilko *在當前的*實現下,一般是** no **。 – Eugene

+0

@Eugene,爲什麼不能使用它? – Andrew