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

How to traverse HashMap in java

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I would like to talk to you about how to traverse HashMap in java, 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.

Traversing HashMap/** * @ Author https://www.javastudy.cloud * @ CreateTime 2019-10-31 * * / public class HashMapStudy {public static void main (String [] args) {/ / generally speaking, it is best to initialize. If it is less than 12, do not initialize / / the default is 16, because the load factor is 0.75. That is to say, the capacity of Map map = new HashMap (3) will be expanded by 16-0.75-12.

Map.put ("welcome", "to"); map.put ("java", "study"); map.put ("wechat", "best396975802")

/ / traversal method 1: first traverse key, then take out value System.out.println ("traversal method 1: traversing key first, then value"); for (String key: map.keySet ()) {System.out.println ("key is" + key); System.out.println ("value is" + map.get (key)) } / / traversal method 2: directly traverse value System.out.println ("traversal method 2: traversing value"); for (String value: map.values ()) {System.out.println ("value is" + value);}

/ / traversal method 3: get Key and value by traversing entry, the recommended method! System.out.println ("traversal method 3: get Key and value by traversing entry, recommended method!!"); for (Map.Entry entry: map.entrySet ()) {System.out.println ("key is" + entry.getKey ()); System.out.println ("value is" + entry.getValue ());}

/ / traversal method 4: traverse key and value System.out.println directly through the forEach method ("traversal method 4: traversing directly through the forEach method"); map.forEach ((key,value)-> {System.out.println ("key is" + key); System.out.println ("value is" + value);}) }} after reading the above, do you have any further understanding of how to traverse HashMap in java? 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

Internet Technology

Wechat

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

12
Report