Traversal of Java hashmap

tags: Java foundation knowledge  hashmap  java

I believe that everyone must be unfamiliar with Hashmap, and it is also more proficient. He here, the blogger helps everyone review the source code.

First give you a picture

This is a HashMap very clear that HashMap is composed of key values.

Ask a question

problem

How is HashMap deposits and values?

answer Put KEY and VALUE in an object and place the object in an array.
Let's take a look at the bottom source code

Object is entry (node ​​implements map.entry)

So see the first picture.Each line can see as an Entry object as a whole as an arrayThen, the first traversal method came out.

Traversal

 for (Map.Entry entry:hashMap.entrySet()) {// hashmap is an instantiated object
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }

Traverse

For the first picture, we look at the first line (Key) is a set collection (the bottom of the SET is MAP implementation)
We can traverse all KEY values.

Set<String> keys= hashMap.keySet();
        for (String key :keys) {
            System.out.println(hashMap.get(key));
        }

By the way, the second line of the first picture (Value) is a collection of Collection, we can pass
.VALUES Gets Value Collection

Intelligent Recommendation

JAVA HashMap traversal explanation

1. Code Listing 2. Two ways of memory diagram      ...

Efficient traversal of HashMap in java

Writing one Writing two...

Java HashMap way traversal of Five

Which uses a fifth of Java8 lambda expression to traverse....

To investigate the performance of Java HashMap traversal

    keySetentrySetHashMapMap.foreachlambda AI developers involved Baidu contest to win prizes 750,000 bonus +25 Wan, (providing tutorial) plus group: 418 589 053 >>>   Abo...

More Recommendation

Three HashMap java collection traversal

Preface: Gravity collection HashMap is understandable, by its ownArray + list / black treeConfiguration (JDK 1.8), so that the advantages demonstrated HashMap: Fast array of query efficiency; List ins...

4 traversal methods of Java HashMap

HashMap traversal HashMap is a container for storing data in the form of (key, value). There are 4 traversal methods; Ordinary traversal This is the ordinary traversal method of HashMap; by strengthen...

HashMap traversal and sorting code in JAVA

HashMap traversal HashMap sort...

The traversal method of HashMap and HashSet in java

I went for an interview today. The interviewer asked this question, and found that he had used HashMap and HashSet for so long, but he could barely think of a method. To sum up HashMap traversal metho...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top