【发布时间】:2020-11-08 20:02:16
【问题描述】:
我有一个具有以下属性的城市列表。
城市 - 城市 ID、城市名称、州、邮政编码
City city1 = new City(1,"Lucknow","UP",123);
City city2 = new City(2,"Kanpur","UP",1234);
City city3 = new City(3,"Allahabad","UP",12345);
City city4 = new City(4,"Agra","UP",1232);
List<City> cityList = new ArrayList();
cityList.add(city1);
cityList.add(city2);
cityList.add(city3);
cityList.add(city4);
我有另一个学生列表,学生也有一个名为 City 的属性。
学生 -> id, name, cityName
Student s1 = new Student(111,"Sachin","Allahabad");
Student s2 = new Student(222,"Anil","Agra");
List<Student> studList = new ArrayList();
studList.add(s1);
studList.add(s2);
我必须检查 cityList(cityName) 是否包含 studentList(cityName) 中的所有城市。
在上述情况下,它应该返回 true。
如果我用 cityName("Delhi") 添加另一个学生对象,它应该返回 false,因为它不在 cityList 中。
有人可以帮助我了解如何使用 Java 8 有效地实现上述场景吗?
【问题讨论】:
标签: java collections java-8 java-stream