为什么Java List无法删除动态代理对象

时间:2017-04-06 14:46:47

标签: java list proxy

我有一个由Proxy.newProxyInstance创建的代理对象。 但它无法从List集合中删除:

        Vector<ThirdPartyInterface>  all = new Vector<ThirdPartyInterface>();
    Set<ThirdPartyInterface>  all2 = new HashSet<ThirdPartyInterface>();
    List<ThirdPartyInterface>  all3 = new LinkedList<ThirdPartyInterface>();

    ThirdPartyClass realObject = new ThirdPartyClass();
    ThirdPartyInterface proxy = ProxyFactory.newInstance(realObject);

    all.addElement(proxy);      
    if (!all.remove(proxy)) {
        System.out.println("     ===>  proxy can't be found in Vector");
    }

    all2.add(proxy);
    if (!all2.remove(proxy)) {
        System.out.println("     ===>  proxy can't be found in HashSet");
    }

    all3.add(proxy);
    if (!all3.remove(proxy)) {
        System.out.println("     ===>  proxy can't be found in LinkedList");
    }

此代码将打印:

===&GT;在Vector

中找不到代理

===&GT;在LinkedList

中找不到代理

我试过真实物体,它按预期工作。

是什么原因?

我该如何解决?

我的代码在JDK7上,该类具有默认的equals和hashCode方法。

0 个答案:

没有答案