JAVA自学教程之 Map集合练习。
一、LinkedHashMap集合
- 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>();//保证有序
- 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);
- }
- }
- }
练习:
记录字母出现次数
- import java.util.Iterator;
- import java.util.Map;
- import java.util.TreeMap;
- /*”as6&dg+dfg-badf453sasxa” 记录字符串中字母出现的次数 a(4)b(1) d(4)…
- *
- * 思路:
- * 很容易发现,字母和次数之间存在对应关系,而且需要存储
- * 能存储映射关系的集合有数组和Map
- * 判断关系中的一方是否是有序编号?否
- * 需要使用Map集合
- * 判断具备唯一性的一方具备着顺序如a,b,c。。。
- * 所以使用TreeMap
- * */
- public class Main
- {
- public static void main(String[] args) {
- /*
- * 1.将字符串变成单个字母的字符数组
- * 2.用每个字母当中键去查Map集合的表
- * 3.判断字母是否存在,不存在按 字母<key> - time<value>的方式存储 (初始time = 1)
- * 存在,按 字母<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]); //将字母作为键查表
- 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();
- }
- }
二、Map集合查表法
- 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) {
- /*在查表法中Map集合的应用较多*/
- 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(“输入错误”);
- String[] week = {“星期一”,”星期二”,”星期三”,”星期四”,”星期五”,”星期六”,”星期天”};
- return week[num];
- }
- public static String Get_Week_English(String week){
- Map<String, String> map = new HashMap<String, String>();
- if(!(week.equals(“星期一”)||week.equals(“星期二”)||week.equals(“星期三”)
- ||week.equals(“星期四”)||week.equals(“星期五”)||week.equals(“星期六”)||week.equals(“星期天”)))
- {
- throw new RuntimeException(“输入错误的星期”);
- }
- map.put(“星期一”, ”Monday”);//特别注意一点,map.put(“”,Set<Integer>);集合中也可以放集合
- map.put(“星期二”, ”Tus”);
- map.put(“星期三”, ”Wes”);
- map.put(“星期四”, ”Thu”);
- map.put(“星期五”, ”Fri”);
- map.put(“星期六”, ”Sta”);
- map.put(“星期天”, ”Sun”);
- return map.get(week);
- }
- }