Common traversal methods of Map sets in Java and Applications of hashmap

Source: Internet
Author: User

There are roughly three types of map traversal:

1. Traverse map. entryset (): each of its elements is a map. Entry object. In this object,

There is a key-value pair in the map;

2. Traverse map. keyset (): It is a set of key values in map. We can traverse this set

Read the elements in the map;

3. Traverse map. Values (): It is a set of values in map. We can traverse through this set directly.

The value in the map, but cannot read the key.

 

Package COM. sort; import Java. util. hashmap; import Java. util. iterator; import Java. util. map; import Java. util. set; import Java. util. map. entry; /*** the map set is the same as the set. * several common methods of map set traversal * @ author owner **/public class maptest5 {public static void main (string [] ARGs) {Map <string, string> map = new hashmap <string, string> (); map. put ("A", "zhangsan"); map. put ("B", "Lisi"); map. put ("C", "wangwu"); // system. out. println (MAP);/*** method 1 traverse map */set <string> keyset = map. keyset (); For (iterator <string> iterator = keyset. iterator (); iterator. hasnext ();) {string key = iterator. next (); string value = map. get (key); system. out. println (Key + "=" + value);} system. out. println ("**********************"); /*** method 2 traverse map */For (string key: map. keyset () {system. out. println (Key + "=" + map. get (key);} system. out. println ("**********************");/***** method 3 traverses the map, we recommend that you use this method to traverse Map sets, especially when the capacity is large */For (map. entry <string, string> entry: map. entryset () {system. out. println (entry. getkey () + "=" + entry. getvalue ();} system. out. println ("**********************"); /*** Method 4 traverse map */set <entry <string, string> entryset = map. entryset (); For (iterator <map. entry <string, string> iterator = entryset. iterator (); iterator. hasnext ();) {map. entry <string, string> entry = iterator. next (); system. out. println (entry. getkey () + "=" + entry. getvalue ();} system. out. println ("**********************");/***** Method 5, this method traverses all values */For (string value: map. values () {system. out. println (value );}}}

 

Elements in map are stored unordered.

Hashmap considerations:

1. An array is maintained at the bottom layer of hashmap. The objects we put into hashmap are actually stored in this array;

2. When a key value is put to hashmap, it calculates a location based on the hashcode value of the key, which is the location where the object is to be stored in the array.


Example of a hashmap application: enter an English sentence on the console to calculate the number of times each word appears.

 

Package COM. sort; import Java. util. hashmap; import Java. util. iterator; import Java. util. map; import Java. util. imports; import Java. util. set;/*** count the number of times each word appears in a simple English sentence ** @ author owner **/public class maptest3 {public static void main (string [] ARGs) {pipeline SC = new pipeline (system. in); system. out. println ("enter an English sentence separated by spaces:"); string sentence = SC. nextline (); string [] arr = sentence. split (""); // The key represents the word, Value Indicates the number of times Map <string, integer> map = new hashmap <string, integer> (); For (INT I = 0; I <arr. length; I ++) {If (! Map. containskey (ARR [I]) {map. put (ARR [I], 1);} else {// indicates that the element int num = map exists in the map. get (ARR [I]); map. put (ARR [I], ++ num) ;}} system. out. println ("count the number of words, the result is as follows:"); set <string> set = map. keyset (); For (iterator <string> iterator = set. iterator (); iterator. hasnext ();) {string key = iterator. next (); integer value = map. get (key); system. out. println (Key + "=" + value );}}}

 

Output result:

Enter an English sentence separated by spaces:
I love u China I am pround to be a Chinese
Count the number of words that appear. The result is as follows:
To = 1
U = 1
Love = 1
AM = 1
A = 1
Pround = 1
Chinese = 1
I = 2
China = 1
Be = 1

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.