Home java What happens then Set & lt; Map.Entry & lt; K, V &...

What happens then Set & lt; Map.Entry & lt; K, V & gt; & gt; entrySet ()?




I do not understand the work of Set & lt; Map.Entry & lt; K, V & gt; & gt; entrySet () . How can we create a Map.Entry & lt; K, V & gt; , if this interface? We can not create interfaces. You can further explain and show whether it is used somewhere else?

Answer 1, Authority 100%

The essence of polymorphism is that for some interface, you can hide the implementation details.

For example, we want to create a stack, its simplified interface will look like this:

public interface Stack & lt; T & gt; {
  public void add (T t);
  public T pop ();

What realization can offer? In fact, many of them, you can use ArrayList , you can use the array if we, for example, know the maximum size. You may want to use the LinkedList . However, ask a question using the stack you need to know exactly what is hidden behind the implementation of this interface? Probably not, you just know that there are methods add and pop , as well as the elements will be located in the memory, you do not care. You simply use the stack.

Public Static Void Main (String [] Args) {
 Stack & lt; Integer & gt; stack = new MyStackLinkedList ();
 processing (stack);
 Stack & lt; Integer & gt; stack2 = new MyStackArrayList ();
 processing (stack);
// method does not know what kind of implementation is used, but it is enough to know about the add and pop
public static void processing (Stack & lt; Integer & gt; stack) {
  stack.add (1);
  stack.add (15);
  stack.pop ();

Also in the case Map.Entry & lt; K, V & gt; , the specific implementation is hidden, suddenly we understand how to optimize the HashMap in the future, for backwards compatibility of all the products we will be enough to change or write a new implementation of Node , but the rest of the code does not have to change.

Answer 2, Authority 33%

EntrySet – are values ​​Map (ie key-value). Each key-value pair is an Entry.
Generally such that map – an associative array in which (particularly in the implementation HashMap) is searched for hash key value. If there is a collision, and hash key codes match, the values ​​in this hash are formed in the linked list (up to 8 components, and then turns into a red-black tree, but the rules determining the hash function is not happening, loadfactor plus partially prevents such situeyshn, êàî little things a lot and we omit them yet)
But as we see, Set, and Map is a different collection, but not conceptually. The fact is that under the hood uses the Set Map – our associative array, just as it leaves the null values. Therefore, if we are talking about HashSet implementation in java and why this data structure works for constant O (1) time (when the key is removed), then it is the same associative maschiv – Entry, a key – a unique the K, and V – null, and when we ask to give us a certain item, he knows in advance on what index it is (it calls hashCode function () Object class and modulo division by the array length determines which cell it is – ie, for example, hashCode returned 31 and the initial emnost HashSet have 16 – & gt; it means we are doing 31% 16 = 15 (remainder of division) in 15 of our array cell and is our element and, when we get it, we do not seek it, and stupid to.. He is addressed, hence the speed)

But if to sum up – the Set just under the hood yuzaet Map (note, map does not implement iterable, as opposed to the set, but getValues ​​() and getKeys () methods implement mapy)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions