Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the principle of JavaHashMap and example parsing?

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report