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阅读 218,451评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,172评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,782评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,709评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,733评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,578评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,320评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,241评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,686评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,878评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,992评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,715评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,336评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,912评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,040评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,173评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,947评论 2 355