输出listNew里哪些数据的id是listOld中不存在的

GoogleVip8 3月前 ⋅ 139 阅读

QQ20210826-0.jpg

package org.example;

import java.util.ArrayList;
import java.util.List;

public class Test {
    static class Alarm{
        String id;
        String name;

        public Alarm(String id,String name){
            this.id = id;
            this.name=name;
        }

        @Override
        public String toString() {
            return "Alarm{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
    public static void main(String[] args) {

        List<Alarm> listOld = new ArrayList<>();
        List<Alarm> listNew = new ArrayList<>();
        Alarm alarm1 = new Alarm("id1", "姓名1");
        Alarm alarm2 = new Alarm("id2", "姓名2");
        Alarm alarm3 = new Alarm("id3", "姓名3");
        Alarm alarm4 = new Alarm("id4", "姓名4");
        Alarm alarm5 = new Alarm("id1", "姓名5");
        Alarm alarm6 = new Alarm("id6", "姓名1");
        Alarm alarm7 = new Alarm("id7", "姓名7");
        Alarm alarm8 = new Alarm("id8", "姓名8");

        listOld.add(alarm1);
        listOld.add(alarm2);
        listOld.add(alarm3);
        listOld.add(alarm4);

        listNew.add(alarm5);
        listNew.add(alarm6);
        listNew.add(alarm7);
        listNew.add(alarm8);


        List<Alarm> listTmp = new ArrayList<>();
        for (Alarm alarmOld : listOld) {
            for (Alarm alarmNew : listNew) {
                if (alarmOld.id.equals(alarmNew.id)){
                    listTmp.add(alarmNew);
                }
            }
        }
        for (Alarm alarmTmp : listTmp) {
            for (int i = 0; i < listOld.size(); i++) {
                if (alarmTmp.id.equals(listOld.get(i).id)){
                    listOld.remove(listOld.get(i));
                }
            }
        }
        for (Alarm alarm : listOld) {
            System.out.println("listOld中不存在"+alarm.toString());
        }
    }
}

请写出比较两个list的伪代码,实现如下的需求:

  1. 输出listNew里哪些数据的id是listOld中不存在的

  2. 输出listNew中,哪些数据的id在listOld存在,只是名字不同


全部评论: 0

    我有话说: