简体   繁体   English

&lt;[]&gt; vs和之间有什么区别<null>

[英]Whats the difference between <[]> vs <null>

<[]> vs <null>

The first is an empty set containing no objects. 第一个是不包含任何对象的空集。 It can be created from the code: 可以从以下代码创建:

Set<Node> expected =new HashSet<>();

The second could be a set containing 1 null element. 第二个可以是包含1个null元素的集合。 If not, can null really have a type Set ? 如果不是,null是否真的可以具有Set类型?

Not sure how to code the second item. 不确定如何编码第二个项目。

<[]> means an object include no elements. <[]>表示对象不包含任何元素。 null is not an element. null不是元素。 after u call Structure method like new HashSet<>(); 在您调用诸如new HashSet<>();类的Structure方法之后new HashSet<>(); , set point to a real memory. ,将点设置为实际内存。 But null point to nowhere. 但是零点指向无处。

A set containing null contains the element null . 包含null的集合包含元素null An empty set does not contain the element null . 空集不包含元素null

Suppose you make a set containing null like this: 假设您创建一个包含null的集合,如下所示:

Set<Object> setWithNull = new HashSet<Object>();
setWithNull.add(null);

And an empty set like this: 像这样的空集:

Set<Object> emptySet = new HashSet<Object>();

Then you can see the following differences: 然后,您可以看到以下差异:

setWithNull.size()==1                   emptySet.size()==0
setWithNull.isEmpty()==false            emptySet.isEmpty()==true
setWithNull.contains(null)==true        emptySet.contains(null)==false

setWithNull.equals(emptySet)==false

So a set containing null is different from an empty set in many significant ways. 因此,包含null的集合在很多方面都与空集不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM