Elements in a Set cannot be repeated, so what is the way to distinguish between repeated and not?

Equals == or equals()? What’s the difference?

 

 

 

1. What is a Set? (what)

Set is a subinterface of the Collection container. It does not allow duplicate elements and, of course, only a null object.

 

2. How to distinguish between repetition and non-repetition? (how)

 

“Use iterator() to distinguish repetition from repetition” is the answer circulating on the Internet. Personally, I think it is a wrong answer. The API makes it very clear: “Set does not contain satisfications

The e1.equals(e2) element pairs e1 and e2 “, so it’s more appropriate to use equals() for the answer.

 

3. Why use equals() instead of ==? (why)

 

Equals refers to the same object and equals refers to the same object. And let’s see what’s in Set

Object, or reference to an object. According to the storage mechanism of Java, set in the object reference, so when two elements as long as meet the equals () is point to the same object,

That’s where repeating elements come in. So you should use equals().