简体   繁体   中英

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. If not, can null really have a type Set ?

Not sure how to code the second item.

<[]> means an object include no elements. null is not an element. after u call Structure method like new HashSet<>(); , set point to a real memory. But null point to nowhere.

A set containing null contains the element null . An empty set does not contain the element null .

Suppose you make a set containing null like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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