黑马程序员——Map集合

——- android培训java培训、期待与您交流! ———-
Map集合
Map集合,独立于Collection
Map存储的是键值对,键唯一性,只能映射一个值
集合 ,每个键只能对应一个值
Map接口中的方法
put get keySet entrySet size

public static void method_5(){
    Map<String,Integer> map = new HashMap<String, Integer>();
    map.put("a", 11);
    map.put("b", 12);
    map.put("c", 13);
    map.put("d", 14);
    map.put("e", 15);
    //调用集合 get方法,根据键获取值
    Integer i = map.get("e");
    //调用集合方法values,将所有值,存储到Collection集合
    Collection<Integer> coll = map.values();
    Iterator<Integer> it = coll.iterator();
    while(it.hasNext()){
        System.out.println(it.next());
    }
    //boolean isEmpty()判断集合有没有键值对
    map.clear();
    System.out.println(map.isEmpty());
    //调用集合方法 remove 删除d这键
    Integer i = map.remove("d");
    System.out.println(map);
    System.out.println(i);
    //判断集合有没有c这个键
    boolean b = map.containsKey("c");   
    //判断集合中有没有12这个值
    b = map.containsValue(12);
}

Map集合获取方式
Map集合获取方式依赖Set集合
keySet方式,实际使用多,简单
entrySet方法,代码结构复杂,内部接口
keySet方式 实现步骤:
1) 调用Map集合的方法Set keySet()
作用:将Map中的所有的键,存储到Set集合
2) 迭代Set集合
获取到的是Set中的元素,这些元素是Map中的键
3) 调用Map集合中的get方法,通过键获取值

public static void main(String[] args) {
    Map<String,Integer> map = new HashMap<String, Integer>();
    map.put("a", 1);
    map.put("b", 2);
    map.put("c", 3);

    //调用Map集合方法keySet获取一个Set集合,Set中存储的是Map中的键
    Set<String> set = map.keySet();
    //迭代Set集合
    Iterator<String> it = set.iterator();
    while(it.hasNext()){
        //it.next获取到的是Set元素,正好是Map中的键
        String key = it.next();
        //调用Map集合方法 get 获取值
        Integer value = map.get(key);   
        System.out.println(key+"..."+value);
    }
}

entrySet方法 实现步骤
1) Map集合中的方法 entrySet()获取键值对关系对象 Map.Entry接口的实现类
将Map.Entry接口实现类对象,存储到Set集合
Set< Map.Entry

public static void main(String[] args) {
    Map<String,Integer> map = new HashMap<String, Integer>();
    map.put("a", 1);
    map.put("b", 2);
    map.put("c", 3);

    //1. 集合方法entrySet()获取Map.Entry对象,存储到Set集合
    Set<Map.Entry<String, Integer>> set =   map.entrySet();
    //2.迭代Set集合
    Iterator<Map.Entry<String, Integer>> it = set.iterator();
    while(it.hasNext()){
        //3. it.next()是什么 Map.Entry接口的实现类对象
        Map.Entry<String, Integer> entry = it.next();
        //4 .通过内部接口Entry方法 getKey  getValue
        String key = entry.getKey();
        Integer value = entry.getValue();
        System.out.println(key+"..."+value);
    }
}

Map接口派系中的类 HashMap
HashMap子类自身特性
底层数据结构就是哈希表
允许存储null值,null键
是线程不安全的集合,运行速度快
存储到HashMap集合中,用于键的对象,必须实现hashCode和equals方法保证唯一性

public static void main(String[] args) {
    //创建HashMap集合,键存储Student对象,值存储String
    HashMap<Student,String> hm = new HashMap<Student, String>();

    hm.put(new Student("a",19), "华北地区");
    hm.put(new Student("b",22), "华南地区");
    hm.put(new Student("c",21), "西南地区");
    hm.put(new Student("d",18), "东北地区");
    hm.put(new Student("a",19), "西北地区");
    hm.put(new Student("c",21), "伊拉克");
    hm.put(new Student("d",18), "阿富汗");
    entrySet(hm);

}
public static void entrySet(HashMap<Student,String> hm){
    //entrySet方法,获取Map.Entry对象,存储到Set集合
    Set<Map.Entry<Student, String>> set = hm.entrySet();
    //迭代Set集合
    Iterator<Map.Entry<Student, String>> it = set.iterator();
    while(it.hasNext()){
        //next方法获取出的是 Map.Entry对象
        Map.Entry<Student, String> entry = it.next();
        //Map.Entry接口方法 getKet  getValue
        Student key = entry.getKey();
        String value = entry.getValue();
        System.out.println(key+"..."+value);
    }
}

public static void keySet(HashMap<Student,String> hm){
    //keySet方法,获取所有键,存储到Set集合
    Set<Student> set = hm.keySet();
    //迭代Set集合
    Iterator<Student> it = set.iterator();
    while(it.hasNext()){
        //next方法获取出来的是Set中的元素,Map中的键 Student对象
        Student key = it.next();
        //Map集合方法get获取值
        String value = hm.get(key);
        System.out.println(key+".."+value);
    }
}

Map接口派系中的类 Hashtable
Hashtable 出现版本早于集合框架,JDK1.0出现, 底层数据结构哈希表
不允许存储null值,null键 ,线程安全,运行速度慢,已经被HashMap取代

Hashtable操作方式,存储取出和HashMap完全一样
没有集合框架之前存储键值对,依靠Hashtable
Hashtable 退出历史舞台 ,但是孩子Properties依然活跃在开发舞台

Map接口派系中的类 TreeMap
对存储到集合中的键进行排序,不理会值 ,底层红黑树,
要求是存储的键对象,必须拥有自然顺序,或者指定比较器
线程不安全,运行速度快

public static void method_1(){
        TreeMap<Student,String> tree = new TreeMap<Student, String>(new StudentAgeComprator());
        tree.put(new Student("pakuiao",30), "菲律宾");
        tree.put(new Student("meiweise",32), "美国");
        tree.put(new Student("jackson",58),"美国");
        tree.put(new Student("bushi",60), "美国");
        for(Student key : tree.keySet()){
            String value = tree.get(key);
            System.out.println(key+"..."+value);
        }

    }

Map集合的两种获取方式,HashMap,Hashtable,TreeMap 的课程总结。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值