Check if one list contains element from the other

前端 未结 12 2638
没有蜡笔的小新
没有蜡笔的小新 2020-11-30 20:45

I have two lists with different objects in them.

List list1;
List list2;

I want to check if element from list

12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-30 21:23

    faster way will require additional space .

    For example:

    1. put all items in one list into a HashSet ( you have to implement the hash function by yourself to use object.getAttributeSame() )

    2. Go through the other list and check if any item is in the HashSet.

    In this way each object is visited at most once. and HashSet is fast enough to check or insert any object in O(1).

提交回复
热议问题