2023-1-

1.使用Map接口的实现类完成员工工资(姓名–工资)的摸拟:
    1)添加几条信息
    2)列出所有的员工姓名
    3列出所有员工姓名及其工资
    4)删除名叫“Tom”的员工信息
    5)输出Jack的工资,并将其工资加1000元(通过取值实现)
    6)将所有工资低于1000元的员工的工资上涨20%(通过取值实现)

 Map<String,Integer> map = new HashMap<>();
        map.put("胡桃",100);
        map.put("宵宫",200);
        map.put("雷电真",1000);
        map.put("心海",13899);
        map.put("tom",100);
        map.put("Jack",50);
        System.out.println("输出所有信息:"+map);
        Set<String> keys = map.keySet(); //获取姓名
        System.out.println( "姓名:" +keys);
        Set<Map.Entry<String,Integer>> entries =map.entrySet();
        System.out.println("姓名 = 工资:"+entries);
        map.remove("tom");
        System.out.println("删除tom"+map); //删除tom后
        Set<String > keySet =map.keySet();
        for (String key :keySet){
            Integer value = map.get(key);
            if(key.equals("Jack")){
                System.out.println("Jack的工资:"+(value+1000));
            }
        }
        for (String key:keySet){
            if(map.get(key)<1000){
                map.put(key,(int)(map.get(key)*1.2));
            }

        }
        System.out.println(map);
        System.out.println(map.get("胡桃"));

    }

2.分析以下需求,并用代码实现:
(1)定义一个学生类Student,包含属性:姓名(String name)、年龄(int age)
(2)定义Map集合,用Student对象作为key,用字符串(此表示表示学生的住址)作为value
(3)利用三种方式遍历Map集合中的内容

public class Student {
    public String name;
    public int age;

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

    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;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
        Map<Student, String> map = new HashMap<>();
        map.put(new Student("张三", 16), "河南");
        map.put(new Student("张四", 17), "河南");
        map.put(new Student("张五", 19), "河南");
        map.put(new Student("张六", 14), "河南");
        System.out.println("------------遍历 Map 方式1---------");
        //遍历 Map 方式1 通过遍历所有的Key 遍历map
        Set<Student> keys = map.keySet();
        Iterator<Student> it = keys.iterator();
        while(it.hasNext()){
            Student key = it.next();
            String value =map.get(key);
            System.out.println("key" + key + "value" + value);
        }
        //遍历Map 方式 2 通过遍历所有的value 遍历map
        System.out.println("------------遍历Map 方式2 ---------");
        Collection<String> values = map.values();
        for (String value :values){
            System.out.println("value" + value);
        }
        System.out.println("-----------遍历Map 方式3 ----------");
        Set<Map.Entry<Student,String>> entries = map.entrySet();
        for (Map.Entry<Student,String> entry :entries){
            Student key = entry.getKey();
            String value = entry.getValue();
            System.out.println("key" + key + "value" + value);
        }

    }
}

 3.统计一个字符串中字母字符,数字字符出现的次数。(不考虑其他字符)

public class CountString {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一串字符串(包括英文和数字):");
        String str = sc.nextLine();
        Map<String,Integer>map = new HashMap<>(); //String 为字符串 Interger为出现次数
        for (int i = 0; i < str.length(); i++) {
            char s1 = str.charAt(i);
            if(s1>=97&&s1<=122||s1>=65&&s1<=90||s1>=48&&s1<=57){
                String  s =  String.valueOf(s1);
                if(map.containsKey(s)){  //判断是否存在

                    map.put(s, map.get(s)+1);
                }
                else {
                    map.put(s,1);
                }
            }
        }

        System.out.println(map);
    }
}

 

 

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值