1 /** 2 *homework0926 3 *@author:kai li 4 */ 5 package com.kai.li.homework0926; 6 import java.util.List; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.HashMap; 10 import java.util.Map; 11 import java.util.Scanner; 12 /** 13 * 14 */ 15 public class HomeWork0926{ 16 public static void main(String[] args)throws Exception{ 17 18 /** 19 *question one 20 */ 21 22 /*create data sourse*/ 23 24 List<String> keyList=Arrays.asList("1930","1934","1938","1950","1954","1958","1962","1966","1970","1974","1978","1982","1986","1990","1994","1998","2002","2006","2010","2014"); 25 List<String> valueList=Arrays.asList("乌拉圭","意大利","意大利","乌拉圭","西德","巴西","巴西","英格兰","巴西","西德","阿根廷","意大利","阿根廷","西德","巴西","法国","巴西","意大利","西班牙","德国"); 26 Map<String,String> footballMap=new HashMap<>(); 27 for(int i=0;i<keyList.size();i++){ 28 footballMap.put(keyList.get(i),valueList.get(i)); 29 } 30 31 /*query with years*/ 32 33 System.out.print("请输入年份:"); 34 Scanner scanner=new Scanner(System.in); 35 String strKey=scanner.nextLine(); 36 if(!footballMap.containsKey(strKey)) 37 System.out.println("没有举办世界杯"); 38 else 39 System.out.println("世界杯冠军是: "+footballMap.get(strKey)); 40 41 /*query with countrys */ 42 43 System.out.print("请输入国家名称:"); 44 String strValue=scanner.nextLine(); 45 if(!footballMap.containsValue(strValue)) 46 System.out.println(strValue+"没有得过世界杯的冠军"); 47 else{ 48 System.out.println(strValue+"得冠军年份是:"); 49 footballMap.keySet().stream().filter(i->strValue.equals(footballMap.get(i))).sorted().forEach(System.out::println); //filter the data then sorted then println 50 } 51 } 52 }
88 }