What is the reason for the interface The card has an internal Entry interface?

I am talking about how developed Java classes and interfaces just saw java/util/Map.java

and I still have doubts? please help me by answering.

What is the reason for the interface to Map

have an internal interface Entry

?
Have a look at the source code java / util / Map.java and answer.

+3


source to share


1 answer


Map<K,V>

can be thought of as associative storage (i.e. a container that connects keys with their values). It can also be thought of as a set of pairs, where the first element is the key and the second element is its corresponding value.

Most of the methods in it Map<K,V>

support the associative view of the map container. Map.Entry<K,V>

the interface supports a different kind of map - i.e. as a set of key-value pairs.

Each card provides access to what is called a set of records, which is a set of pairs from a card. Each pair is represented by a copy Map.Entry<K,V>

.



Map<Integer,String> myMap = ...
for (Map.Entry<Integer,String> e : myMap.entrySet()) {
    System.out.println(e.getKey()+" ---> "+e.getValue());
}

      

It was possible to define the interface MapEntry<K,V>

outside Map<K,V>

. However, since the interface is very closely related to the map, the decision to embed the interface inside the map makes sense.

+2


source







All Articles