1 package cn.itcast.p10.map.test; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class MapTest2 { 7 8 public static void main(String[] args) { 9 // TODO Auto-generated method stub 10 /* 11 * Map在有映射关系时,可以优先考虑。 12 * 13 * 在查表法中的应用较为多见。 14 */ 15 String week = getWeek(1); 16 System.out.println(week); 17 18 System.out.println(getWeekByMap(week)); 19 } 20 21 public static String getWeekByMap(String week) { 22 Map<String,String> map = new HashMap<String,String>(); 23 24 map.put("星期一", "Mon"); 25 map.put("星期二", "Tus"); 26 map.put("星期三", "Wes"); 27 map.put("星期日", "Sun"); 28 map.put("星期天", "Sun"); 29 30 //map.put("预热班", "张三"); 31 //map.put("预热班", "李四");键唯一会覆盖 32 //map.put("预热班", "王五"); 33 //以上可以map.put("预热班",Set<Student>);可以把value放在集合里 34 return map.get(week); 35 36 37 } 38 39 public static String getWeek(int week) { 40 41 if (week<1 || week>7) { 42 throw new RuntimeException("没有对应的星期,请您重新输出"); 43 } 44 String[] weeks = {"","星期一","星期二"}; 45 46 return weeks[week]; 47 } 48 49 }MapTest2