I want to know the difference between using a list with the Set<> interface and the Map<> interface, which one is better for me to access the data more easily?
I want to know the difference between using a list with the Set<> interface and the Map<> interface, which one is better for me to access the data more easily?
The difference is the functionality defined for each of these interfaces:
Map
is an interface that defines storing objects in pairs of type Key (or Key) Value, where you can register, get and remove an element from the Key.Example:
There can only be 1 Value associated with a Key. This does not mean that there can be 1 Value associated with multiple Keys. Example:
Set
is an interface that defines storing objects as if it were a mathematical set. This allows elements to be registered and removed from this set, but it does not allow them to be obtained one by one.Set
it is mainly used to check that there are no duplicate objects in a collection.Example:
It should be noted that a
Set
can be seen simply as aMap<Object, Boolean>
, where the objects to be inserted into theSet
are the keys of theMap
. This can be checked by using the methodCollections#newSetFromMap(Map<E, Boolean>)
.