Map練習題

經典查單詞

統計一段文章中每個單詞出現的次數

 public static void main(String[] args) {
        String speak = "this is a book this is an elephont";
        String[] words = speak.split(" ");
        Map<String, Integer> map = new HashMap<String, Integer>();

        for (int i = 0; i < words.length; i++) {
            if (map.containsKey(words[i])) {
                int count = map.get(words[i]);
                count++;
                map.put(words[i], count);
            } else {
                map.put(words[i], 1);
            }
        }
        for (String string : map.keySet()) {
            System.out.println(string + "---------" + map.get(string));
        }
    }

1.第一題 (Map)利用Map,完成下面的功能:

從命令行讀入一個字符串,表示一個年份,輸出該年的世界杯冠軍是哪支球隊。如果該 年沒有舉辦世界杯,則輸出:沒有舉辦世界杯。

歷屆世界杯冠軍
屆數 舉辦年份 舉辦地點 冠軍
第一屆 1930年 烏拉圭 烏拉圭
第二屆 1934年 意大利 意大利
第三屆 1938年 法國 意大利
第四屆 1950年 巴西 烏拉圭
第五屆 1954年 瑞士 西德
第六屆 1958年 瑞典 巴西
第七屆 1962年 智利 巴西
第八屆 1966年 英格蘭 英格蘭
第九屆 1970年 墨西哥 巴西
第十屆 1974年 前西德 西德
第十一屆 1978年 阿根廷 阿根廷
第十二屆 1982年 西班牙 意大利
第十三屆 1986年 墨西哥 阿根廷
第十四屆 1990年 意大利 西德
第十五屆 1994年 美國 巴西
第十六屆 1998年 法國 法國
第十七屆 2002年 韓日 巴西
第十八屆 2006年 德國 意大利
第十九屆 2010年 南非 西班牙
第二十屆 2014年 巴西 德國

(Map)在原有世界杯Map 的基礎上,增加如下功能: 讀入一支球隊的名字,輸出該球隊奪冠的年份列表。 例如,讀入“巴西”,應當輸出 1958 1962 1970 1994 2002 讀入“荷蘭”,應當輸出 沒有獲得過世界杯

  public static void main(String[] args) {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1930, "烏拉圭");
        map.put(1934, "意大利");
        map.put(1938, "意大利");
        map.put(1950, "烏拉圭");
        map.put(1954, "西德");
        map.put(1958, "巴西");
        map.put(1962, "巴西");
        map.put(1966, "英格蘭");
        map.put(1970, "巴西");
        map.put(1974, "西德");
        map.put(1978, "阿根廷");
        map.put(1982, "意大利");
        map.put(1986, "阿根廷");
        map.put(1990, "西德");
        map.put(1994, "巴西");
        map.put(1998, "法國");
        map.put(2002, "巴西");
        map.put(2006, "意大利");
        map.put(2010, "西班牙");
        map.put(2014, "德國");
        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("輸入年份");
            int year = scanner.nextInt();
            if (map.containsKey(year)) {
                System.out.println("該年的冠軍是" + map.get(year));
            } else {
                System.out.println("該年沒有舉辦世界杯");
                break;
            }
        }
        System.out.println("輸入國家");
        String country = scanner.next();
        boolean i = false;
        for (Integer year : map.keySet()) {
            if (country.equals(map.get(year))) {
//                System.out.println("該隊獲得冠軍的年份是");
                System.out.println(year);
                i = true;
            }

        }
        if (i == false) {
            System.out.println("該國家太弱了");
        }

    }

2.第二題 已知有十六支男子足球隊參加2008 北京奧運會。寫一個程序,把這16 支球隊隨機分為4 個組。采用List集合和隨機數

2008 北京奧運會男足參賽國家:

科特迪瓦,阿根廷,澳大利亞,塞爾維亞,荷蘭,尼日利亞,日本,美國,中國,新西蘭,巴西,比利時,韓國,喀麥隆,洪都拉斯,意大利

提示:分配一個,刪除一個

 public static void main(String[] args) {
        Map<Integer, List<String>> Group2Countrys = new HashMap<Integer, List<String>>();
        String s = "科特迪瓦,阿根廷,澳大利亞,塞爾維亞,荷蘭,尼日利亞,日本,美國,中國,新西蘭,巴西,比利時,韓國,喀麥隆,洪都拉斯,意大利";
        String[] country = s.split(",");
        List<String> wordcupgroup = new ArrayList<String>();
        for (int i = 0; i < country.length; i++) {
            wordcupgroup.add(country[i]);
        }

        //1組

        for (int j = 0; j < 4; j++) {
            List<String> wordcupgroup1 = new ArrayList<String>();
            for (int i = 0; i < 4; i++) {

                Random random = new Random();
                int index = random.nextInt(wordcupgroup.size());
                wordcupgroup1.add(wordcupgroup.get(index));
                wordcupgroup.remove(wordcupgroup.get(index));

            }
            Group2Countrys.put(j + 1, wordcupgroup1);
        }
        for (Integer bl : Group2Countrys.keySet()) {
            System.out.println("第" + bl + "組");
            List<String> con = Group2Countrys.get(bl);
            for (String s1 : con) {
                System.out.print(s1 + "\t");
            }
            System.out.println();
        }

    }

3.第三題 有如下Student 對象,姓名,年齡,成績,班級號,

private String name;  

    private int age;  

    private int score;  

    private String classNum; 

其中,classNum 表示學生的班號,例如“class05”。 有如下

List List list = new ArrayList();

list.add(new Student(“Tom”, 18, 100, “class05”));  

list.add(new Student(“Jerry”, 22, 70, “class04”)); 

list.add(new Student(“Owen”, 25, 90, “class05”)); 

list.add(new Student(“Jim”, 30,80 , “class05”)); 

list.add(new Student(“Steve”, 28, 66, “class06”));

list.add(new Student(“Kevin”, 24, 100, “class04”));

在這個list 的基礎上,完成下列要求:

1) 計算所有學生的平均年齡

2) 計算各個班級的平均分

public class Student {
    private String name;
    private int age;
    private double socre;
    private String ClassNum;

    public Student(String name, int age, double socre, String ClassNum) {
        this.name = name;
        this.age = age;
        this.ClassNum = ClassNum;
        this.socre = socre;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public double getSocre() {
        return socre;
    }

    public void setSocre(double socre) {
        this.socre = socre;
    }

    public String getClassNum() {
        return ClassNum;
    }

    public void setClassNum(String classNum) {
        ClassNum = classNum;
    }
}

public class ListStudent {
    public static void main(String[] args) {
        List<Student> StudentScore = new ArrayList<Student>();
        StudentScore.add(new Student("aa", 13, 89.5, "01"));
        StudentScore.add(new Student("bb", 14, 89, "02"));
        StudentScore.add(new Student("cc", 12, 90, "03"));
        StudentScore.add(new Student("dd", 14, 79, "01"));
        StudentScore.add(new Student("ee", 13, 100, "02"));
        StudentScore.add(new Student("ff", 14, 93, "03"));
        int totle = 0;
        for (Student student : StudentScore) {
            totle += student.getAge();
        }
        System.out.println("平均年齡是" + totle / StudentScore.size() + "歲");

        Map<String, List<Double>> Class2avgscore = new HashMap<String, List<Double>>();
        for (Student student : StudentScore) {
            String classname = student.getClassNum();
            double score = student.getSocre();
            if (Class2avgscore.containsKey(classname)) {
                List<Double> everyclass = Class2avgscore.get(classname);
                everyclass.add(score);
            } else {
                List<Double> everyclass = new ArrayList<>();
                everyclass.add(score);
                Class2avgscore.put(classname, everyclass);
            }
        }

        for (String string : Class2avgscore.keySet()) {
            double totlescore = 0;
            System.out.println(string + "班");
            List<Double> avgs = Class2avgscore.get(string);
            for (Double scores : avgs) {
                totlescore += scores;
            }
            System.out.println("平均成績是" + totlescore / avgs.size());
        }

    }
}

第四題 (List)寫一個函數reverseList,該函數能夠接受一個List,然后把該List 倒序排列。

public static void reverseList(List<String> stringList)
    {
        //新建一個list
        List<String> stringList1 = new ArrayList<>();

        int index2 = 1;
        for(String string : stringList)
        {
            //得到倒數第index2個元素
            String strLast = stringList.get(stringList.size() - index2);
            //加到新的list
            stringList1.add(strLast);

            index2++;
        }

        int index3 = 0;
        for(String string : stringList1)
        {
            stringList.set(index3, string);
            index3++;
        }
    }

    public static void printList(List<String> stringList)
    {
        int index = 1;
        for(String string : stringList)
        {
            System.out.print("第" + index + "個元素:");
            System.out.println(string);
            index++;
        }
    }


    public static void main(String[] args) {

        List<String> stringList = new ArrayList<>();

        stringList.add("zhangsan");
        stringList.add("lisi");
        stringList.add("wangwu");
        stringList.add("liuliu");

        System.out.println("反轉之前");
        printList(stringList);

        reverseList(stringList);
//        Collections.reverse(stringList);
//        Collections.shuffle(stringList);//隨機打亂list集合中的數據


        System.out.println("反轉之后");
        printList(stringList);

    }

5.第五題(Map)設計Account 對象如下:

private long id;  

 private double balance;  

 private String password;

要求完善設計,使得該Account 對象能夠自動分配id。 給定一個List 如下:

List list = new ArrayList(); 

list.add(new Account(10.00, “1234”)); 

list.add(new Account(15.00, “5678”)); 

list.add(new Account(0, “1010”)); 

要求把List 中的內容放到一個Map 中,該Map 的鍵為id,值為相應的Account 對象。 最后遍歷這個Map,打印所有Account 對象的id 和余額。

用UUID的方法

public class Account {
    private String id;
    private double balabce;
    private String powerword;

    public Account(double balabce, String powerword) {

        this.balabce = balabce;
        this.powerword = powerword;
        this.id = UUID.randomUUID().toString();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public double getBalabce() {
        return balabce;
    }

    public void setBalabce(double balabce) {
        this.balabce = balabce;
    }

    public String getPowerword() {
        return powerword;
    }

    public void setPowerword(String powerword) {
        this.powerword = powerword;
    }
}
 public static void main(String[] args) {
        List<Account> accountList = new ArrayList<Account>();
        accountList.add(new Account(159, "aaa"));
        accountList.add(new Account(187, "hhh"));
        accountList.add(new Account(188, "hdd"));
        accountList.add(new Account(155, "hff"));
        accountList.add(new Account(170, "ggg"));
        accountList.add(new Account(150, "htt"));
        Map<String, Account> Id2Balance = new HashMap<String, Account>();
        for (Account account : accountList) {
            Id2Balance.put(account.getId(), account);
        }
        for (Map.Entry<String, Account> entry : Id2Balance.entrySet()) {
            System.out.println("id=" + entry.getKey() + "余額" + entry.getValue().getBalabce());
        }
    }

隨機生成的id

public class Account {
    private long id;
    private double balabce;
    private String powerword;
    private  static int object_count=0;//多加的

    public Account(double balabce, String powerword) {

        this.balabce = balabce;
        this.powerword = powerword;
        this.id= Account.object_count+1;//調用
        Account.object_count++;
    }

    public long  getId() {
        return id;
    }

    public void setId(long  id) {
        this.id = id;
    }

    public double getBalabce() {
        return balabce;
    }

    public void setBalabce(double balabce) {
        this.balabce = balabce;
    }

    public String getPowerword() {
        return powerword;
    }

    public void setPowerword(String powerword) {
        this.powerword = powerword;
    }
}

  public static void main(String[] args) {
        List<Account> accountList = new ArrayList<Account>();
        accountList.add(new Account(159, "aaa"));
        accountList.add(new Account(187, "hhh"));
        accountList.add(new Account(188, "hdd"));
        accountList.add(new Account(155, "hff"));
        accountList.add(new Account(170, "ggg"));
        accountList.add(new Account(150, "htt"));
        Map<Long, Account> Id2Balance = new HashMap<Long, Account>();//Key 變
        for (Account account : accountList) {
            Id2Balance.put(account.getId(), account);
        }
        for (Map.Entry<Long, Account> entry : Id2Balance.entrySet()) //Key 變
        {
            System.out.println("id=" + entry.getKey() + "余額" + entry.getValue().getBalabce());
        }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,106評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,441評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,211評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,736評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,475評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,834評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,829評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,009評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,559評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,306評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,516評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,038評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,728評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,132評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,443評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,249評論 3 399
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,484評論 2 379