按内部对象的字段分组

时间:2018-02-18 18:46:56

标签: 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; 
}

我不知道如何按List<Man>类的streetcity字段对Address进行分组。我该怎么办?

1 个答案:

答案 0 :(得分:3)

Collectors类提供了Collectors.groupingBy(keyProvider, downstream),您可以使用它来按一对值进行分组。要配对两个值,您可以使用AbstractMap.SimpleEntry,也可以实现自己的hashCode()类,该类代表一对两个值(值得一提的是,您必须实现equals(object)和{{如果要将其用作哈希映射中的键,则可以使用Pair类中的方法。另外,您要在密钥中配对的两个值必须实现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()
        ));

在此示例中,我使用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 + '\'' +
                    '}';
        }
    }
}

运行此示例时,您将在控制台中执行以下操作:

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

希望它有所帮助。