MAP集合练习题

package demo;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/*利用 Map,完成下面的功能:
 从命令行读入一个字符串,
 表示一个年份,
 输出该年的世界杯冠军是哪支球队。
 如果该年没有举办世界 杯,则输出:没有举办世界杯。
* */
public class Test19 {
    public static void main(String[] args) {
        //创建map集合
        Map<Integer, String> map = new HashMap<>();
        map.put(1930, "乌拉圭");
        map.put(1934, "意大利");
        map.put(1938, "意大利");
        map.put(1950, "乌拉圭");
        map.put(1594, "德国");
        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, "意大利");

        //用户输入
        Scanner sc = new Scanner(System.in);
        //打印提示语句
        System.out.println("输入你想要查询的年份  (别查了没中国)");
        //确定Scanner类对象变量
        Integer sr = sc.nextInt();

        //使用map类中的确定key是否存在的方法
        if (map.containsKey(sr))
            System.out.println(map.get(sr));
        else System.out.println("世界杯4年一次 你这个假球迷");

        //在上一题目的基础上,增加如下功能: 读入一支球队的名字,输出该球队夺冠的年份列表。
        //确定Scanner类对象数据类型
        String str = sc.next();
        boolean bo = false;
        Set<Integer> set = map.keySet();
        for (Integer integer : set) {
            if (str.equals(map.get(integer))){
                bo = true;
                System.out.println("integer = " + integer);
            }
        }
        if (bo==false) System.out.println(str + "没有得过世界杯");

    }
}


### 关于 Map 数据结构和函数的编程练习 Map 是一种常见的数据结构,在许多编程语言中有不同的实现形式,比如 Python 中的字典 (dictionary),C++ 的 `std::map` 和 Java 的 `HashMap`。以下是几个涉及 Map 数据结构或函数的经典编程练习题: #### 1. 字符串字符频率统计 编写一个程序来计算给定字符串中每个字符出现的次数并存储在一个映射表中。 ```python def char_frequency(s): freq_map = {} for c in s: if c not in freq_map: freq_map[c] = 0 freq_map[c] += 1 return freq_map result = char_frequency("hello world") print(result) # 输出 {'h': 1, 'e': 1, 'l': 3, 'o': 2, ' ': 1, 'w': 1, 'r': 1, 'd': 1} ``` 此代码片段展示了如何利用 map 来记录字符及其对应的频次[^1]。 #### 2. 单词计数器 设计一段代码用于读取文本文件中的单词,并创建一个 map 表示每个唯一单词以及它在整个文档中出现的次数。 ```python from collections import defaultdict def word_count(file_path): counts = defaultdict(int) with open(file_path, 'r') as file: for line in file: words = line.split() for word in words: counts[word.lower()] += 1 return dict(counts) counts_result = word_count('example.txt') print(counts_result) ``` 上述例子说明了通过遍历文件每一行并将每行拆分为单独词语的方式构建了一个基于默认字典的对象,默认字典会自动初始化不存在键值为零的情况[^2]。 #### 3. 反转键值对 假设有一个已存在的关联数组或者哈希表,请写一个方法用来交换所有的 key-value 对的位置形成新的反转后的集合。 ```python def invert_dict(original_dict): inverted = {value: key for key, value in original_dict.items()} return inverted original = {"apple": "fruit", "carrot": "vegetable"} inverted = invert_dict(original) print(inverted) # 输出 {'fruit': 'apple', 'vegetable': 'carrot'} ``` 这里提供了一种简洁的方法去完成这个任务——借助推导式语法快速生成一个新的倒置版本的地图对象[^3]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值