Код: Выделить всё
Set seen = new HashSet();
seen.add("apple"); // Return value is ignored
seen.add("banana"); // Return value is ignored
seen.add("apple"); // Return value is ignored
< /code>
boolean add(E e)
Set seen = new HashSet();
if (!seen.add("apple")) {
System.out.println("Apple is already in the set!");
} else {
System.out.println("Apple was added to the set.");
}
< /code>
I understand that add returns true if the element was added to the set and false if it was already present. However, I'm confused about what happens to the return value when it's not explicitly used in the code.(Such as in the first example)
Could someone explain this behavior in detail? Thank you!
Подробнее здесь: https://stackoverflow.com/questions/794 ... ed-in-java