且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

比较不同类型的列表

更新时间:2021-11-25 23:02:29

在Java 8中,您可以编写您所描述的内容:一种方法是遍历list1,从中创建一个新的ID列表,然后使用containsAll方法就可以了."在一行中为:

In Java 8 you can write what you described: "One way is to iterate over the list1, create a new list of Id's out of it and then use the containsAll method on it." in one line as:

list1.stream().map(a -> a.getMyObjId()).collect(Collectors.toList()).containsAll(list2);

map 通过调用a.getMyObjectId将每个MyObeject转换为ID,然后 collect 创建一个列表结果.

map converts a each MyObeject to an id by calling a.getMyObjectId and collect creates a list as a result.