Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the principle of JavaHashMap and example parsing?

Shulou Source: shulou.com Published: 2022-06-02 07:22:58 10月03日 Update

Today, I would like to talk to you about the principle of JavaHashMap and example analysis, many people may not know much about it. In order to make you understand better, the editor summarized the following content for you. I hope you can get something according to this article.

Example 1: key-value pair of HashMap

The way HashMap stores data is key-value pairs.

Package collection; import java.util.HashMap

Public class TestCollection {

Public static void main (String [] args) {

HashMap dictionary = new HashMap ()

Dictionary.put ("adc", "physical Hero")

Dictionary.put ("apc", "Magic Hero")

Dictionary.put (t, Tank)

System.out.println (dictionary.get ("t"))

}

}

Example 2: keys cannot be repeated and values can be repeated

For HashMap, key is unique and cannot be repeated.

Therefore, inserting different value into the Map with the same key causes the old element to be overwritten, leaving only the last inserted element.

However, the same object can be inserted into the map as a value, as long as the corresponding key is different

Package collection; import java.util.HashMap; import charactor.Hero

Public class TestCollection {

Public static void main (String [] args) {

HashMap heroMap = new HashMap ()

HeroMap.put ("gareen", new Hero ("gareen1"))

System.out.println (heroMap)

/ / key already has value for gareen. Putting data in gareen as key will cause the original hero to be overwritten.

/ / No new elements will be added to Map heroMap.put ("gareen", new Hero ("gareen2"))

System.out.println (heroMap)

/ / clear map heroMap.clear ()

Hero gareen = new Hero ("gareen")

/ / the same object can be inserted into the map as a value, as long as the corresponding key is different.

HeroMap.put ("hero1", gareen)

HeroMap.put ("hero2", gareen)

System.out.println (heroMap)

}

}

After reading the above, do you have any further understanding of the principle of JavaHashMap and example parsing? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Tags: Element content hero principle instance object data example different same tank way more physics knowledge article industry information information channel channel Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Information vpn Shulou Tech Info macOS NVidia