按内部对象的字段分组

And*_*kiy 6 java java-8

我有以下两个课程:

class Man {
    private int id;
    private String firstName;
    private String lastName;
    private int age;
    private int countOfChildren;
    private Address address; 
}

class Address {
    private Country country;
    private City city;
    private String street;
    private long quantityOfPeople; 
}
Run Code Online (Sandbox Code Playgroud)

我不知道如何组List<Man>通过streetcity领域Address类.我该怎么做?

Szy*_*iak 3

Collectors 类提供了Collectors.groupingBy(keyProvider, downstream)可用于按一对值进行分组的功能。要配对两个值,您可以使用AbstractMap.SimpleEntry或实现自己的Pair<T,K>类来表示一对两个值(值得一提的是,如果您想将其用作哈希中的键,则必须在 Pair 类中实现hashCode()和方法equals(object)地图)。此外,您想要在键中配对的两个值必须实现hashCode()equals(object)方法 - 在这种情况下值得使用不可变类。

整个分组部分可以通过以下方式完成:

final Map<Map.Entry<City, String>, List<Man>> groupedByCityAndStreet = people.stream()
        .collect(Collectors.groupingBy(
                man -> new AbstractMap.SimpleEntry<>(man.getAddress().getCity(), man.getAddress().getStreet()),
                Collectors.toList()
        ));
Run Code Online (Sandbox Code Playgroud)

在这个例子中,我用 来AbstractMap.SimpleEntry表示一对Country和 街道。它创建一个地图,其中每个键根据国家和街道对对象列表进行分组Man。您可以在下面找到完整的示例:

import java.util.AbstractMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class GroupByTest {

    public static void main(String[] args) {
        final List<Man> people = Arrays.asList(
                new Man(1, "John", "Doe", 20, 0, new Address(new Country("England"), new City("London"), "Test Street 2", 10000)),
                new Man(2, "Mary", "Smith", 54, 4, new Address(new Country("Germany"), new City("Berlin"), "Maine Strasse 32", 10000)),
                new Man(3, "James", "Rose", 13, 0, new Address(new Country("England"), new City("London"), "Test Street 2", 10000)),
                new Man(4, "Vincent", "Dog", 43, 2, new Address(new Country("Germany"), new City("Berlin"), "Volkswagen Platz 31", 10000)),
                new Man(5, "Arnold", "Smoke", 72, 3, new Address(new Country("Italy"), new City("Rome"), "Pepperoni 31", 10000)),
                new Man(6, "Katy", "Puppet", 33, 3, new Address(new Country("England"), new City("London"), "Test Street 3", 10000))
        );

        final Map<Map.Entry<City, String>, List<Man>> groupedByCityAndStreet = people.stream()
                .collect(Collectors.groupingBy(
                        man -> new AbstractMap.SimpleEntry<>(man.getAddress().getCity(), man.getAddress().getStreet()),
                        Collectors.toList()
                ));

        // Print people associated with given city and street to console
        groupedByCityAndStreet.forEach((k, v) -> {
            System.out.println("People associated with " + k.getKey().name + ", " + k.getValue() + ":");
            v.forEach(man -> {
                System.out.println(man);
            });
        });
    }

    static final class Man {
        private final int id;
        private final String firstName;
        private final String lastName;
        private final int age;
        private final int countOfChildren;
        private final Address address;

        public Man(int id, String firstName, String lastName, int age, int countOfChildren, Address address) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
            this.age = age;
            this.countOfChildren = countOfChildren;
            this.address = address;
        }

        public int getId() {
            return id;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public int getAge() {
            return age;
        }

        public int getCountOfChildren() {
            return countOfChildren;
        }

        public Address getAddress() {
            return address;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Man man = (Man) o;
            return id == man.id &&
                    age == man.age &&
                    countOfChildren == man.countOfChildren &&
                    Objects.equals(firstName, man.firstName) &&
                    Objects.equals(lastName, man.lastName) &&
                    Objects.equals(address, man.address);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, firstName, lastName, age, countOfChildren, address);
        }

        @Override
        public String toString() {
            return "Man{" +
                    "id=" + id +
                    ", firstName='" + firstName + '\'' +
                    ", lastName='" + lastName + '\'' +
                    ", age=" + age +
                    ", countOfChildren=" + countOfChildren +
                    ", address=" + address +
                    '}';
        }
    }

    static class Address {
        private final Country country;
        private final City city;
        private final String street;
        private final long quantityOfPeople;

        public Address(Country country, City city, String street, long quantityOfPeople) {
            this.country = country;
            this.city = city;
            this.street = street;
            this.quantityOfPeople = quantityOfPeople;
        }

        public Country getCountry() {
            return country;
        }

        public City getCity() {
            return city;
        }

        public String getStreet() {
            return street;
        }

        public long getQuantityOfPeople() {
            return quantityOfPeople;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Address address = (Address) o;
            return quantityOfPeople == address.quantityOfPeople &&
                    Objects.equals(country, address.country) &&
                    Objects.equals(city, address.city) &&
                    Objects.equals(street, address.street);
        }

        @Override
        public int hashCode() {

            return Objects.hash(country, city, street, quantityOfPeople);
        }

        @Override
        public String toString() {
            return "Address{" +
                    "country=" + country +
                    ", city=" + city +
                    ", street='" + street + '\'' +
                    ", quantityOfPeople=" + quantityOfPeople +
                    '}';
        }
    }

    static class City {
        private final String name;

        public City(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            City city = (City) o;
            return Objects.equals(name, city.name);
        }

        @Override
        public int hashCode() {

            return Objects.hash(name);
        }

        @Override
        public String toString() {
            return "City{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }

    static class Country {
        private final String name;

        public Country(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Country country = (Country) o;
            return Objects.equals(name, country.name);
        }

        @Override
        public int hashCode() {

            return Objects.hash(name);
        }

        @Override
        public String toString() {
            return "Country{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当您运行此示例时,您将在控制台中看到如下内容:

People associated with Rome, Pepperoni 31:
Man{id=5, firstName='Arnold', lastName='Smoke', age=72, countOfChildren=3, address=Address{country=Country{name='Italy'}, city=City{name='Rome'}, street='Pepperoni 31', quantityOfPeople=10000}}
People associated with London, Test Street 3:
Man{id=6, firstName='Katy', lastName='Puppet', age=33, countOfChildren=3, address=Address{country=Country{name='England'}, city=City{name='London'}, street='Test Street 3', quantityOfPeople=10000}}
People associated with Berlin, Volkswagen Platz 31:
Man{id=4, firstName='Vincent', lastName='Dog', age=43, countOfChildren=2, address=Address{country=Country{name='Germany'}, city=City{name='Berlin'}, street='Volkswagen Platz 31', quantityOfPeople=10000}}
People associated with Berlin, Maine Strasse 32:
Man{id=2, firstName='Mary', lastName='Smith', age=54, countOfChildren=4, address=Address{country=Country{name='Germany'}, city=City{name='Berlin'}, street='Maine Strasse 32', quantityOfPeople=10000}}
People associated with London, Test Street 2:
Man{id=1, firstName='John', lastName='Doe', age=20, countOfChildren=0, address=Address{country=Country{name='England'}, city=City{name='London'}, street='Test Street 2', quantityOfPeople=10000}}
Man{id=3, firstName='James', lastName='Rose', age=13, countOfChildren=0, address=Address{country=Country{name='England'}, city=City{name='London'}, street='Test Street 2', quantityOfPeople=10000}}
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你。

  • @SzymonStepniak,您可以使用“Arrays.asList()”(或从 9 开始的“List.of()”)而不是“AbstractMap.SimpleEntry”。或者从 9 开始,`Map#entry` (3认同)