JAVA自学教程之 Map集合练习



JAVA自学教程之 Map集合练习。

一、LinkedHashMap集合

 

  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. public class Main
  6. {
  7.     public static void main(String[] args) {
  8.         HashMap<Integer, String> hm = new LinkedHashMap<Integer,String>();//保证有序
  9.         hm.put(1, ”a”);
  10.         hm.put(5, ”c”);
  11.         hm.put(3, ”e”);
  12.         hm.put(4, ”s”);
  13.         Iterator<Map.Entry<Integer,String>> It = hm.entrySet().iterator();
  14.         while(It.hasNext()){
  15.             Map.Entry<Integer, String> meEntry = It.next();
  16.             Integer key = meEntry.getKey();
  17.             String value = meEntry.getValue();
  18.             System.out.println(“key - value :”+key+”-”+value);
  19.         }
  20.     }
  21. }

练习:

 

记录字母出现次数


 

  1. import java.util.Iterator;
  2. import java.util.Map;
  3. import java.util.TreeMap;
  4. /*”as6&dg+dfg-badf453sasxa” 记录字符串中字母出现的次数 a(4)b(1) d(4)…
  5.  *
  6.  * 思路:
  7.  * 很容易发现,字母和次数之间存在对应关系,而且需要存储
  8.  * 能存储映射关系的集合有数组和Map
  9.  * 判断关系中的一方是否是有序编号?否
  10.  * 需要使用Map集合
  11.  * 判断具备唯一性的一方具备着顺序如a,b,c。。。
  12.  * 所以使用TreeMap
  13.  * */
  14. public class Main
  15. {
  16.     public static void main(String[] args) {
  17.         /*
  18.          * 1.将字符串变成单个字母的字符数组
  19.          * 2.用每个字母当中键去查Map集合的表
  20.          * 3.判断字母是否存在,不存在按      字母<key> - time<value>的方式存储 (初始time = 1)
  21.          * 存在,按      字母<key> - ++time<value>的方式存储
  22.          * */
  23.         String str = ”asdgdfgbadfsasxa”;
  24.         String result = Get(str);
  25.         System.out.println(result);
  26.     }
  27.     public static String Get(String str){
  28.         char[] ch = str.toCharArray();
  29.         Map<Character,Integer> map = new TreeMap<Character,Integer>();
  30.         for (int i = 0; i < ch.length; i++) {
  31.             if(!(‘a’<=ch[i]&&ch[i]<=’z' || ’A'<=ch[i]&&ch[i]<=’Z'))
  32.                 continue;
  33.             Integer value =  map.get(ch[i]); //将字母作为键查表
  34.             int time = 1;
  35.             if(value!=null)
  36.                 time = value + 1;
  37.             map.put(ch[i], time);
  38.         }
  39.         return Map_To_String(map);
  40.     }
  41.     private static String Map_To_String(Map map)
  42.     {
  43.         StringBuilder str = new StringBuilder();
  44.         Iterator<Character> it = map.keySet().iterator();
  45.         while(it.hasNext()){
  46.             Character key = it.next();
  47.             Integer value = (Integer) map.get(key);
  48.             str.append(key+”(“+value+”)”);
  49.         }
  50.         return str.toString();
  51.     }
  52. }

 

 

二、Map集合查表法

 

  1. import java.util.HashMap;
  2. import java.util.Iterator;
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6. import javax.management.RuntimeErrorException;
  7. public class Main
  8. {
  9.     public static void main(String[] args) {
  10.         /*在查表法中Map集合的应用较多*/
  11.         int num;
  12.         Scanner in = new Scanner(System.in);
  13.         num = in.nextInt();
  14.         String week = Get_Week(num-1);
  15.         System.out.println(week);
  16.         System.out.println(Get_Week_English(week));
  17.         in.close();
  18.     }
  19.     public static String Get_Week(int num){
  20.         if (num>7 || num <0)
  21.             throw new RuntimeException(“输入错误”);
  22.         String[] week = {“星期一”,”星期二”,”星期三”,”星期四”,”星期五”,”星期六”,”星期天”};
  23.         return week[num];
  24.     }
  25.     public static String Get_Week_English(String week){
  26.         Map<String, String> map = new HashMap<String, String>();
  27.         if(!(week.equals(“星期一”)||week.equals(“星期二”)||week.equals(“星期三”)
  28.                 ||week.equals(“星期四”)||week.equals(“星期五”)||week.equals(“星期六”)||week.equals(“星期天”)))
  29.                 {
  30.             throw new RuntimeException(“输入错误的星期”);
  31.                 }
  32.         map.put(“星期一”, ”Monday”);//特别注意一点,map.put(“”,Set<Integer>);集合中也可以放集合
  33.         map.put(“星期二”, ”Tus”);
  34.         map.put(“星期三”, ”Wes”);
  35.         map.put(“星期四”, ”Thu”);
  36.         map.put(“星期五”, ”Fri”);
  37.         map.put(“星期六”, ”Sta”);
  38.         map.put(“星期天”, ”Sun”);
  39.         return map.get(week);
  40.     }
  41. }