Reputation: 20901
When examined Java's hierarchy Set
and Map
are different interfaces and Map
doesn't implement Collection
interface. Set
is used to hold unique values regardless of ordering, while Map
is used to unique keys and mapped values without ordering(except through LinkedHashMap
, TreeMap
). AFAIS, the major common part is that both of them make use of hash tables to store. My question is about entrySet()
method. I just know about the method that it is used to iterate through to get key and mapped value with the key.
Set
<Map.Entry<K,V>>
? I mean why
Set, why not ArrayList or something?HashMap
)?I have gotten the structures as different from each other. But it seems both take advantage of each other. It is a little bit vague to comprehend. Can you illuminate?
Upvotes: 1
Views: 52
Reputation: 393791
Why does its return type use Set>? I mean why Set, why not ArrayList or something?
A Set
is the preferred Collection
type to use for a group of unique elements with no ordering. The entries of a Map
are unique, since the keys must be unique. And the base Map
interface assumes no ordering on its entries, so a List
of entries wouldn't be appropriate.
Does postfix of the method(set) express something why entrySet why not entryFoo or entryMap (it's already the method of HashMap)?
It returns a Set
of entries, so entrySet()
makes perfect sense.
Upvotes: 1