Java Learning Lesson 40th (Common Object API)-MAP collection Exercises

Source: Internet
Author: User

First, Linkedhashmap Collection

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> ();//Guaranteed 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);}}}

Practice:

Record the number of letters appearing

Import java.util.iterator;import java.util.map;import java.util.treemap;/* "As6&dg+dfg-badf453sasxa" Number of occurrences of letters in the record string A (4) B (1) d (4) ... * Ideas: * It is easy to see that there is a correspondence between the letters and the number of times, and that you need to store a set of mappings that can store a mapping. Is the array and map * Determine whether the party in the relationship is an ordinal number? No * need to use map set * to judge the uniqueness of the party with the order as a,b,c ... * So use TreeMap * */public class Main {public static void main (string[] args) {/* * 1. Converts a string into a single-letter character array * 2. Use each letter key to check the table of the map collection. * 3. Determine if the letter exists, there is no letter <key>-time<value> storage (initial time = 1) * exists, by letter <key>-++time<value> The way to store * */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]); Look up the letter as a key 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.K Eyset (). iterator (); while (It.hasnext ()) {Character key = It.next (); Integer value = (integer) map.get (key); Str.append ( Key+ "(" +value+ ")");} return str.tostring ();}}

Second, map set Check table method

Import Java.util.hashmap;import java.util.iterator;import Java.util.map;import Java.util.scanner;import Java.util.treemap;import Javax.management.runtimeerrorexception;public class Main {public static void Main (string[] args) {/* The application of Map collection in table-checking method is more */int num; Scanner in = new Scanner (system.in); num = In.nextint (); String week = Get_week (num-1); SYSTEM.OUT.PRINTLN (week); System.out.println (Get_week_english (Week)); 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", "Sunday"};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");//special note, Map.put ("",set<integer>); set Map.put ("Tuesday", "Tus") can also be placed in the collection; map. put ("Wednesday", "Wes"), Map.put ("Thursday", "Thu"), Map.put ("Friday", "Fri"), Map.put ("Saturday", "Sta"); Map.put ("Sunday", "Sun"); return Map.get (week);}}



Java Learning Lesson 40th (Common Object API)-MAP collection Exercises

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.