How to retrieve the values of key and value in Map

Source: Internet
Author: User
Tags map class

Are you tired of getting keywords from Map every time and getting the corresponding values? Using the Map. Entry class, you can get all the information at the same time. The standard Map access method is as follows: Set keys = map. keySet (); if (keys! = Null) {Iterator iterator = keys. iterator (); while (iterator. hasNext () {Object key = iterator. next (); Object value = map. get (key );;....;}} then there is a problem with this method. After obtaining keywords from Map, we must repeat the returned values to Map to obtain the relative values, which is cumbersome and time-consuming. Fortunately, there is a simpler way. The Map class provides a method called entrySet (), which returns an object set after Map. Entry instantiation. Next, the Map. Entry class provides a getKey () method and a getValue () method. Therefore, the above Code can be organized to be more logical. Example: Set entries = map. entrySet (); if (entries! = Null) {Iterator iterator = entries. iterator (); while (iterator. hasNext () {Map. entry entry = iterator. next (); Object key = entry. getKey (); Object value = entry. getValue ();;....}} although a line of code is added, we omit many unnecessary "get" calls to Map. At the same time, it provides developers with a class that maintains both the keyword and its corresponding value. Map. Entry also provides a setValue () method, which can be used by programmers to modify values in map. The internal arrangement of Hashtable is hash, so the output information is unordered. To ensure that the output data is arranged in sequence, do not use the built-in functions of java to adjust and process the Hashtable object. When we get the KEY and VALUE in Hashtable, we usually run the Map. Entry class for conversion. Well, now we use this class for writing. I have written a specific method. Code:/*** Method Name: getSortedHashtable * parameter: Hashtable h introduced to the processed hash list * Description: hashtable to be introduced. entrySet is sorted and */public static Map is returned. entry [] getSortedHashtable (Hashtable h) {Set = h. entrySet (); Map. entry [] entries = (Map. entry []) set. toArray (new Map. entry [set. size ()]); Arrays. sort (entries, new Comparator () {public int compare (Object arg0, Object arg1) {Object key1 = (Map. entry) arg0 ). getKey (); Object key2 = (Map. entry) arg1 ). getKey (); return (Comparable) key1 ). compareTo (key2) ;}}); return entries ;}call this method: Map. entry [] set = getSortedHashtable (t); // perportyTable for (int I = 0; I <set. length; I ++) {System. out. println (set [I]. getKey (). toString (); System. out. println (set [I]. getValue (). toString ());}

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.