Map Interface from Java Question

I was interested to know about the map from java.util.

Why does the values ​​() method return a collection when keySet and entrySet return Set?

What are the advantages / disadvantages of the kit and collection?

+3
source share
5 answers

Many guarantee that a given record can exist only once. No collection. Since the Card has no guarantees of uniqueness in terms of meanings, a set of them is not really a set, but should be an assembly.

+6
source

Actually, this is not a problem of advantages and disadvantages - it is the keys, values ​​and records of map that are important .

Keys on the map are unique

Map - Map, A Collection, , , Set.

, Set keySet.

, values Map .

, "fruit" map "apple", "computer" "apple":

map {
  key:"fruit"    -> value:"apple"
  key:"computer" -> value:"apple"
}

.

Set, , . a Map Collection, , .

Map - , Map.Entry, - , Set entrySet.

+6

,

Set , . , . , ( ), undefined. , Iterator.remove, Set.remove, removeAll, keepAll clear. add addAll.

.

+4

A Set Collection, . Set, , , .

As others have already pointed out, values()it cannot return Set, because a collection of values ​​may contain duplicates.

+2
source

values ​​() can be duplicated , so this Collection.

keySet () and entrySet () cannot be duplicated, therefore they are Set.

ps: Setis not duplicated Collection.

+2
source

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


All Articles