ListA와 ListB의 두 목록은 ListB에 없는 ListA의 데이터를 가져옵니다.

제목에서와 같이 ListB에 없는 ListA의 데이터를 빼내야 합니다.
여러 가지 방법과 시간을 들여 테스트한 후 대표적인 두 가지를 선택하여 작성하였습니다.
먼저 Actor 클래스를 생성합니다.

package com.itheima.a02test;

import java.util.Objects;

public class Actor {
    
    
    private String name;
    private int age;

    public Actor() {
    
    
    }



    public Actor(String name, int age) {
    
    
        this.name = name;
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
    
    
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
    
    
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
    
    
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
    
    
        this.age = age;
    }

    public String toString() {
    
    
        return "Actor{name = " + name + ", age = " + age + "}";
    }

    @Override
    public boolean equals(Object o) {
    
    
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Actor actor = (Actor) o;
        return age == actor.age && Objects.equals(name, actor.name);
    }

    @Override
    public int hashCode() {
    
    
        return Objects.hash(name, age);
    }
}

다음은 방법 1입니다.

package com.itheima.a02test;

import java.util.*;

public class Test4 {
    
    
    public static void main(String[] args) {
    
    
//        定义一个HashSet,把ListB放入到HashSet中,然后再把ListA放入到HashSet中,然后循环这个ListA,如果 !HashSet.constain(ListA的元素) 成立,则A不在B中
        List<Actor> listA = new ArrayList<>();
        List<Actor> listB = new ArrayList<>();
        for (int i = 1; i <= 2000; i++) {
    
    
            Actor actor = new Actor("张三", i);
            listA.add(actor);
        }
        for (int i = 1001; i <= 3000; i++) {
    
    
            Actor actor = new Actor("张三", i);
            listB.add(actor);
        }

        long startTime = System.currentTimeMillis();
        List<Actor> finalList = new ArrayList<>();
        for (Actor actor : listA) {
    
    
            if (!listB.contains(actor)) {
    
    
                finalList.add(actor);
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("finalList的数量:" + finalList.size());
        System.out.println("总耗时:"+ (endTime - startTime) + "ms");
    }
}

시간 소모적인 9ms
여기에 이미지 설명 삽입
방법 2:

package com.itheima.a02test;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;

public class Test5 {
    
    
    public static void main(String[] args) {
    
    
//        定义一个HashSet,把ListB放入到HashSet中,然后再把ListA放入到HashSet中,然后循环这个ListA,如果 !HashSet.constain(ListA的元素) 成立,则A不在B中
        List<Actor> listA = new ArrayList<>();
        List<Actor> listB = new ArrayList<>();
        for (int i = 1; i <= 2000; i++) {
    
    
            Actor actor = new Actor("张三", i);
            listA.add(actor);
        }
        for (int i = 1001; i <= 3000; i++) {
    
    
            Actor actor = new Actor("张三", i);
            listB.add(actor);
        }

        long startTime = System.currentTimeMillis();
        HashSet<Actor> hashSet1 = new HashSet<>(listA);
        HashSet<Actor> hashSet2 = new HashSet<>(listB);
        hashSet1.removeAll(hashSet2);
        List<Actor> finalList = new ArrayList<>(hashSet1);

        long endTime = System.currentTimeMillis();
        System.out.println("finalList的数量:" + finalList.size());
        System.out.println("总耗时:"+ (endTime - startTime) + "ms");
    }
}

여기에 이미지 설명 삽입
이것은 거의 3ms입니다

추천

출처blog.csdn.net/weixin_44374025/article/details/128922748