java中的map方法

  Map 是一种键值映射的数据结构,相对 List 结构查找效率更高。
public class hello{
    public static void main(String args[]){

        Student s = new Student("xiaoming", 99);
        Map<String, Student> m = new HashMap<>();
        m.put("xiaoming", s);
        Student target = m.get("xiaoming");
        System.out.println(target == s);  //true
        System.out.println(target.score);  //99
        System.out.println(m.containsKey("xiaoming"));  //true
        System.out.println(m.get("xiaohong"));  //null

    }
}

class Student {
    public String name;
    public int score;
    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }
}
上面代码用到了 Map 的 put()、get() 、containsKey()方法。
        Student s = new Student("xiaoming", 99);
        Student s1 = new Student("xiaohong", 98);
        Map<String, Student> m = new HashMap<>();
        m.put("good student", s);
        System.out.println(m.get("good student").name); //xiaoming
        m.put("good student", s1);
        System.out.println(m.get("good student").name); //xiaohong
这里 m 中的 good student 先关联 xiaoming ,然后再将 good student 关联 xiaohong 。
这里可看到第二次输出 good student 关联的 Student 变成了 xiaohong。
这里是因为 Map 中的 Key 只能对应一个 Value 的缘故。但是 Map 的多个 Key 可以关联同一个 Value 。
		Map<String, Integer> map = new HashMap<>();
        map.put("xiaoming", 1001);
        map.put("xiaohong", 1002);
        map.put("xiaozhan", 1003);
        for (String key : map.keySet()){
            Integer value = map.get(key);
            System.out.println(key + "编号为" + value);
        }

xiaohong编号为1002
xiaoming编号为1001
xiaozhan编号为1003

可以看到输出并不是按照 put 的顺序。
package demo;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class hello{
    public static void main(String args[]){

        List<Student> list = new ArrayList<Student>();
        list.add(new Student("Bob", 78));
        list.add(new Student("Alice", 85));
        list.add(new Student("Brush", 66));
        list.add(new Student("Newton", 99));
        Students holder = new Students(list);
        System.out.println(holder.getScore("Bob") == 78 ? "测试成功!" : "测试失败!");
        System.out.println(holder.getScore("Alice") == 85 ? "测试成功!" : "测试失败!");
        System.out.println(holder.getScore("Tom") == -1 ? "测试成功!" : "测试失败!");
    }
}

class Students {
    List<Student> list;
    Map<String, Integer> cache;

    Students(List<Student> list) {
        this.list = list;
        cache = new HashMap<>();
    }

    /**
     * 根据name查找score,找到返回score,未找到返回-1
     */
    int getScore(String name) {
        // 先在Map中查找:
        Integer score = this.cache.get(name);
        if (score == null) {
            // TODO:
            score = findInList(name);
            if(score != null){
                cache.put(name, score);
            }
        }
        return score == null ? -1 : score.intValue();
    }

    Integer findInList(String name) {
        for (Student ss : this.list) {
            if (ss.name.equals(name)) {
                return ss.score;
            }
        }
        return null;
    }
}

class Student {
    String name;
    int score;

    Student(String name, int score) {
        this.name = name;
        this.score = score;
    }
}
上面实现了一个利用 Map 作为 List 查询缓存的小程序。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值