【问题标题】:JAVA Lambda Expression with two list - inner join带有两个列表的 JAVA Lambda 表达式 - 内连接
【发布时间】:2019-09-18 06:43:12
【问题描述】:

我的代码:

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 表达式 java,需要像 CountryNameStateNameCityName 这样的输出在 countryIdstateIDcityID 的键上进行内连接。

【问题讨论】:

  • 不清楚你在找什么?
  • 感谢您的检查。我有三个 List、List、List。使用 lambda express,我如何使用加入 ListCity 和 ListState 的 stateID 并加入 ListState 和 List Country 的 countryID 来加入所有三个列表。最后获取 Country Name、State Name、City Name 的值。
  • 不要将此类信息放入 cmets。始终更新您的问题。您真的希望阅读您的问题的人了解它的内容。所以把这样的上下文放在问题的开头。
  • 那么,你的问题是什么?
  • 您自己尝试了什么,什么有效,什么无效。如果它是来自数据库的数据,你会谈论连接,你会更好地在数据库中而不是在 Java 中进行操作

标签: java lambda


【解决方案1】:

像这样:

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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-25
    • 2017-06-02
    • 1970-01-01
    相关资源
    最近更新 更多