具有两个列表的JAVA Lambda Expression-内部联接

时间:2019-09-18 06:43:12

标签: java lambda

我的代码:

public class Country {
        private final Integer countryId; // PK, primary key
        private final String name;

        public Country(Integer countryId, String name) {
            this.countryId = countryId;
            this.name = name;
        }
        // omitting getters
    }

    public class State {
        private final Integer stateId;   // PK
        private final Integer countryId; // FK, foreign key
        private final String name;

        public State(Integer stateId, Integer countryId, String name) {
            this.stateId = stateId;
            this.countryId = countryId;
            this.name = name;
        }
        // omitting getters
    }
     public class City {
        private final Integer cityId;    // PK
        private final Integer stateId;   // FK
        private final String name;

        public Integer getCityId() {
            return cityId;
        }

        public Integer getStateId() {
            return stateId;
        }

        public String getName() {
            return name;
        }

        public City(Integer cityId, Integer stateId, String name) {
            this.cityId = cityId;
            this.stateId = stateId;
            this.name = name;
        }
        // omitting getters
    }



    public static void main(String[] args){
        Set<Country> countries = Collections.singleton(new Country(1, "India"));
        Set<State> states = Collections.singleton(new State(1, 1, "Maharastra"));
        Set<City> cities = Collections.singleton(new City(500, 30, "sangai"));
    }

使用Lambda Expression Java,需要像CountryNameStateNameCityName这样的输出,它们在countryIdstateID,{{ 1}}。

1 个答案:

答案 0 :(得分:0)

Smth。像这样:

public final class Country {

    private final int countryId;
    private final String name;

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

public final class State {

    private final int stateId;
    private final int countryId;
    private final String name;

    public State(int stateId, int countryId, String name) {
        this.stateId = stateId;
        this.countryId = countryId;
        this.name = name;
    }
}

public final class City {

    private final int cityId;
    private final int stateId;
    private final String name;

    public City(int cityId, int stateId, String name) {
        this.cityId = cityId;
        this.stateId = stateId;
        this.name = name;
    }

}

public static void main(String... args) {
    Set<Country> countries = Collections.singleton(new Country(1, "India"));
    Set<State> states = Collections.singleton(new State(1, 1, "Maharastra"));
    Set<City> cities = Collections.singleton(new City(500, 30, "sangai"));

    Map<Integer, List<State>> countryIdState = states.stream().collect(Collectors.groupingBy(State::getCountryId));
    Map<Integer, List<City>> stateIdCity = cities.stream().collect(Collectors.groupingBy(City::getStateId));

    final class Record {

        private final String country;
        private final String state;
        private final String city;

        public Record(String country, String state, String city) {
            this.country = country;
            this.state = state;
            this.city = city;
        }
    }

    List<Record> records =
            countries.stream()
                     .filter(country -> countryIdState.containsKey(country.getCountryId()))
                     .map(country -> countryIdState.get(country.getCountryId()).stream()
                                                   .filter(state -> stateIdCity.containsKey(state.getStateId()))
                                                   .map(state -> stateIdCity.get(state.getStateId()).stream()
                                                                            .map(city -> new Record(country.getName(), state.getName(), city.getName()))
                                                                            .collect(Collectors.toList()))
                                                   .flatMap(List::stream)
                                                   .collect(Collectors.toList()))
                     .flatMap(List::stream)
                     .collect(Collectors.toList());

    // use records
}