从Java LinkedList Collection框架

时间:2017-09-14 17:34:56

标签: java collections

我想从Java LinkedList Collection框架中删除用户定义的对象。下面的代码不会删除该对象。我错过了什么?

import java.util.LinkedList;
import java.util.List;

public class MyObjectKeySearch {
    static class CMDTimeSlot{
        String comm;
        int orig;
        int prio;
        public  CMDTimeSlot(String comm,int orig,int prio){
            this.comm = comm;
            this.orig = orig;
            this.prio = prio;
        }

    }

public static void main(String a[]){
        List<CMDTimeSlot> cmdlist = new LinkedList<>();

        CMDTimeSlot one = new CMDTimeSlot("010D",2,2);
        CMDTimeSlot two = new CMDTimeSlot("0100",1,1);

        CMDTimeSlot three = new CMDTimeSlot("010C",3,3);

        cmdlist.add(one);
        cmdlist.add(two);
        cmdlist.add(three);

        for(CMDTimeSlot obj: cmdlist){
            System.out.println(obj.comm +" " + obj.orig +" "+obj.prio);
        }

        cmdlist.remove(new CMDTimeSlot("0100",1,1) );

        for(CMDTimeSlot obj: cmdlist){
            System.out.println(obj.comm +" " + obj.orig +" "+obj.prio);
        }
    }
}

输出:

  

010D 2 2 0100 1 1 010C 3 3

     

010D 2 2

     

0100 1 1 010C 3 3

1 个答案:

答案 0 :(得分:-2)

使用类equals的成员变量覆盖hashcodeCMDTimeSlot方法。