当前位置: 首页 > 知识库问答 >
问题:

Java8lambda创建一个映射,其中Value是列表的包装器对象

富凯旋
2023-03-14

我有这个Address类作为一个列表,我想创建一个以街道名称为键的地图,并将所有Address对象作为值的列表,街道名称是其中的一部分。假设我得到的地址是一个列表,所以我有一个地址列表。

public class Address{
    private String streetName;
    private String city;
    private String country;
    private String zipCode;
    public String getStreetName() {
        return streetName;
    }
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
    public String getCity() {
        return city;
    }
    public void setCity(String city) {
        this.city = city;
    }
    public String getCountry() {
        return country;
    }
    public void setCountry(String country) {
        this.country = country;
    }
    public String getZipCode() {
        return zipCode;
    }
    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

}

I was able to do this using Lambda with the below Code

Map<String, List<Address>> resultMap =
addressList.stream().collect(Collectors.groupingBy(address-> address.getStreetName())); // This creates Map with street name as key and list of addresses as values

我需要在Address上面创建一个包装器,这样它看起来就像

public class AddressWrapper {
     private String streetName;
    private List<Address> addressWrapperList;
    public String getStreetName() {
        return streetName;
    }
    public void setStreetName(String streetName) {
        this.streetName = streetName;
    }
    public List<Address> getAddressWrapperList() {
        return addressWrapperList;
    }
    public void setAddressWrapperList(List<Address> addressWrapperList) {
        this.addressWrapperList= addressWrapperList;
    }
}

现在有了addressList,我如何创建一个地图,以便不是街道名称作为键,属于该街道名称的地址列表作为值,而是创建一个地图,街道名称作为键,但值作为addressWrapperList(地址列表)

共有1个答案

慕弘义
2023-03-14

一种方法是从groupingby操作的结果创建一个流,并收集到map并将值映射器函数指定为AddressWrapper

Map<String, AddressWrapper> result = addressList.stream()
                .collect(Collectors.groupingBy(Address::getStreetName))
                .entrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey, e -> {
                    AddressWrapper addressWrapper = new AddressWrapper();
                    addressWrapper.setStreetName(e.getKey());
                    addressWrapper.setAddressWrapperList(e.getValue());
                    return addressWrapper;
                }));

通过创建签名为public AddressWrapper(String streetName,list

addressWrapperList){...}的构造函数,可以简化值映射器逻辑。

则代码变为:

Map<String, AddressWrapper> result = addressList.stream()
                .collect(Collectors.groupingBy(Address::getStreetName))
                .entrySet()
                .stream()
                .collect(Collectors.toMap(Map.Entry::getKey, 
                              e -> new AddressWrapper(e.getKey(), e.getValue())));

此外,您还可以使用tomap收集器实现相同的结果,如下所示:

Map<String, AddressWrapper> resultSet = addressList.stream()
      .collect(Collectors.toMap(Address::getStreetName,
              e -> new AddressWrapper(e.getStreetName(),
                      new ArrayList<>(Collections.singletonList(e))),
            (left, right) -> {
                left.getAddressWrapperList().addAll(right.getAddressWrapperList());
                return left;
            }));

 类似资料: