Does collection <E> and set <E> match?

I have a question about these two interfaces in Java. Set extends Collection, but adds nothing. They are exactly the same. Did I miss something?

+4
source share
6 answers
Good question. I assume that the main goal of explicitly having an interface for the concept of a Set compared to the concept of a Collection is to actually formally distinguish between the concepts. Let's say you write a method
 void x(Collection<?> c); 

You will not have the same idea about what arguments you want to receive, as if you wrote

 void x(Set<?> s); 

The second method expects Collections , which contains each item no more than once (i.e. Sets ). This is a big semantic difference with the first method, which does not care if it gets Sets , Lists or any other type of Collection

If you look closely, the Set method Set also different, clearly demonstrating the various concepts that come into play when it comes to Collection or Set

+5
source

Set does not allow duplication.

This is semantic difference, not syntactic.

+8
source

From the Collection documentation:

A collection is a group of objects known as its elements. Some collections allow you to duplicate items, while others do not. Some of them are ordered, while others are disordered.

From the documentation of Set :

A collection that does not contain duplicate elements. More formally, sets do not have a pair of elements e1 and e2 , such as e1.equals(e2) and no more than one null element. As its name implies, this interface models a mathematical abstract abstraction.

This should clarify the difference between a Set and a (more general interface) Collection .

+6
source

A collection is a more general interface, which consists of lists, queues, sets, and many others.

See the All Known Subinterfaces section here .

+2
source

Everything in the documentation:

Set is a collection that does not contain duplicate elements. More formally, the sets do not contain pairs of elements e1 and e2 such that e1.equations (e2) and at are the very same zero element. As can be seen from his name, this interface models a mathematical setting of abstraction.

and

Collection. The root interface in the collection hierarchy. A collection is a group of objects known as its elements. Some collections allow you to duplicate items and others do not. Some are ordered and others are unordered. The SDK does not provide any direct implementations of this interface: it provides the implementation of more specific subinterfaces, such as Set and List. This interface is commonly used to transfer collections around and manipulate them where maximum generality is required.

A distinction should be made between future implementation and use.

It came from set theory and vocabulary

A collection is something that is being assembled; a group of objects or the amount of material accumulated in one place, especially for any purpose or as a result of some process

Set is a set of different objects.

0
source

In addition, the Set documentation defines a contract for .equals , which states that "only other sets can be equal to this set." If we could not recognize other sets by their type (using instanceof ), it would be impossible to implement.

If this were only for equals() , one could have the allowsDuplicates() method for Collection . But there are often times when the APIs want to say “please don’t give me duplicates” or “I guarantee that it does not contain duplicates”, and in Java there is no way to say in the method declaration “please give only collections, allowsDuplicates() returns false ". Thus, an additional type.

0
source

Source: https://habr.com/ru/post/1340600/


All Articles