JAVA learning lesson 40th (Common Object API)-Map set exercises, apimap

Source: Internet
Author: User

JAVA learning lesson 40th (Common Object API)-Map set exercises, apimap

1. LinkedHashMap set

Import java. util. hashMap; import java. util. iterator; import java. util. linkedHashMap; import java. util. map; public class Main {public static void main (String [] args) {HashMap <Integer, String> hm = new LinkedHashMap <Integer, String> (); // ensure ordered hm. put (1, "a"); hm. put (5, "c"); hm. put (3, "e"); hm. put (4, "s"); Iterator <Map. entry <Integer, String> It = hm. entrySet (). iterator (); while (It. hasNext () {Map. entry <Integer, String> meEntry = It. next (); Integer key = meEntry. getKey (); String value = meEntry. getValue (); System. out. println ("key-value:" + key + "-" + value );}}}

Exercise:

Number of occurrences of letters

Import java. util. iterator; import java. util. map; import java. util. treeMap;/* "as6 & dg + dfg-badf453sasxa" record the number of occurrences of letters in the string a (4) B (1) d (4 )... ** train of thought: * It is easy to find that there is a correspondence between letters and numbers, and * The set that can store ing needs to be stored with arrays and maps * to determine whether one side of the relationship is an ordered number? No * Map set * is used to determine whether the uniqueness of a Party has a sequence such as a, B, and c... * Use TreeMap **/public class Main {public static void main (String [] args) {/** 1. converts a string into a character array of a single letter * 2. use each letter as the key to check the table of the Map set * 3. determines whether a letter exists. There is no storage by letter <key>-time <value> (initial time = 1, storage By letter <key>-++ time <value> **/String str = "asdgdfgbadfsasxa"; String result = Get (str); System. out. println (result);} public static String Get (String str) {char [] ch = str. toCharArray (); Map <Character, Integer> map = New TreeMap <Character, Integer> (); for (int I = 0; I <ch. length; I ++) {if (! ('A' <= ch [I] & ch [I] <= 'Z' | 'A' <= ch [I] & ch [I] <= 'Z ')) continue; Integer value = map. get (ch [I]); // use the letter as the key to look up the table int time = 1; if (value! = Null) time = value + 1; map. put (ch [I], time);} return Map_To_String (map);} private static String Map_To_String (Map map) {StringBuilder str = new StringBuilder (); iterator <Character> it = map. keySet (). iterator (); while (it. hasNext () {Character key = it. next (); Integer value = (Integer) map. get (key); str. append (key + "(" + value + ")");} return str. toString ();}}

Ii. Map set lookup Method

Import java. util. hashMap; import java. util. iterator; import java. util. map; import java. util. imports; import java. util. treeMap; import javax. management. runtimeErrorException; public class Main {public static void main (String [] args) {/* the Map set has many applications in the Lookup Table Method */int num; specified in = new partition (System. in); num = in. nextInt (); String week = Get_Week (num-1); System. out. println (week); System. out. println (Get_Week_English (w Eek); in. close ();} public static String Get_Week (int num) {if (num> 7 | num <0) throw new RuntimeException ("input error "); string [] week = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday "}; return week [num];} public static String Get_Week_English (String week) {Map <String, String> map = new HashMap <String, String> (); if (! (Week. equals ("Monday") | week. equals ("Tuesday") | week. equals ("Wednesday") | week. equals ("Thursday") | week. equals ("Friday") | week. equals ("Saturday") | week. equals ("Sunday") {throw new RuntimeException ("input wrong week");} map. put ("Monday", "Monday"); // pay special attention to the following: map. put ("", Set <Integer>); the Set map can also be placed in the Set. put ("Tuesday", "Tus"); map. put ("Wes", "Wes"); map. put ("Thu"); map. put ("Friday", "Fri"); map. put ("Saturday", "Sta"); map. put ("Sunday", "Sun"); return map. get (week );}}






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.