我有两个不同的列表
,它们与彼此不同。public class App1{
private String name;
private String city;
// getter setter
// constructors
}
public class App2{
private String differentName;
private String differentCity;
private String someProperty1;
private String someProperty2;
// getter setter
// constructors
}
List<App1> app1List = new ArrayList<>();
app1List.add(new App1("test1","city1"));
app1List.add(new App1("test2","city2"));
app1List.add(new App1("test3","city3"));
app1List.add(new App1("test4","city4"));
List<App2> app2List = new ArrayList<>();
app2List.add(new App2("test2","city2"));
app2List.add(new App2("test3","city3"));
正如您所看到的,App1和App2类是两个不同的POJO,具有不同的属性名称,但是分别由name、city和differentName、differentCity属性持有的内容/值相同,即test1、test2、test3和city1、city2等。
现在我需要过滤app1List,比较其他列表中的名称和城市,即不存在于app2List中。
最终输出将是:
app1List.add(new App1("test1","city1"));
app1List.add(new App1("test4","city4"));
最简单的方法是循环其他列表多次,但我想避免这种情况。有没有在Java 8流中的方法,而不必多次循环?
app2List.add(new App2("test1","city4""));
,又会怎么样呢? - undefined