加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

用Java设置实例?

发布时间:2020-12-15 02:53:09 所属栏目:Java 来源:网络整理
导读:Java定义了一个Set接口,其中 contains() 定义如下: Returns true if this set contains the specified element. More formally,returns true if and only if this set contains an element e such that (o==null ? e==null : o.equals(e)) . Collection接口
Java定义了一个Set接口,其中 contains()定义如下:

Returns true if this set contains the specified element. More
formally,returns true if and only if this set contains an element e
such that (o==null ? e==null : o.equals(e)).

Collection接口将contains()定义如下:

Returns true if this collection contains the specified element. More
formally,returns true if and only if this collection contains at
least one element e such that (o==null ? e==null : o.equals(e)).

我需要一个Java’实例集’,其中contains()基于==而不是equals().换句话说,一组硬实例,其中两个不同的对象A和B,其中A.equals(B)可以在同一组中共存,因为A!= B.

这样的’实例集’是用Java还是在一些公共库中提供的?我找不到任何东西,但可能有人知道更好.如果没有,我会实施它.谢谢.

解决方法

JRE中没有直接的“实例集”.

但是有一个IdentityHashMap,它根据你的术语实现了“实例图”.

并且有一个名为Collections.newSetFromMap()的方法可以从任意Map实现创建一个Set.

因此,您可以轻松地构建自己的实例集,如下所示:

Set<MyType> instanceSet = Collections.newSetFromMap(new IdentityHashMap<MyType,Boolean>());

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读