Map集合

Map集合

  1. Map接口概述:

    ​ 将键映射到值的对象

    ​ 一个映射不能包含重复的键

    ​ 每个键最多只能映射到一个值

  2. Map接口和Collection接口的区别

    ​ Map是双列的,Collection是单列的

    ​ Map的键唯一,Collection的子体系Set是唯一的

    ​ Map集合的数据结构针对键有效,跟值无关;Collection集合的数据结构是针对元素有效的

  3. Map集合的功能

    ​ a:添加功能

    ​ V put(K key,V value) 添加元素,还可以替换

    ​ 如果键是第一次存储,就直接存储元素,返回null

    ​ 如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值

    ​ b:删除功能

    ​ void clear() 移除所有的键值对元素

    ​ V remove(object key) 根据键删除键值对元素,并把值返回

    ​ c:判断功能

    ​ boolean containsKey(object key) 判断集合中是否包含指定的键

    ​ boolean containsValue(object value) 判断集合是否包含指定的值

    ​ boolean isEmpty () 判断集合是否为空

    ​ d:获取功能

    ​ Set<Map.Entry<K,V>> entrySet () 返回一个键值对的Set集合

    ​ V get(object key) 根据键获取值

    ​ Set keySet() 获取集合中所有键的集合

    ​ Collection values() 获取集合中所有的值

    1. Map集合的遍历
    public class MapTraverse {
        public static void main(String[] args) {
            HashMap<String, Integer> map = new HashMap<>();
            map.put("卡莉斯塔",23);
            map.put("奥利安娜",25);
            map.put("希维尔",26);
            map.put("希维尔",28);//键相同,值会被替换
            //第一种:通过map.keySet遍历key和value
            //map.keySet返回的时所有Key的值
            for (String key : map.keySet()) {
                Integer value = map.get(key);//得到每个key对应的值
                System.out.println(key+"==="+value);
            }
            System.out.println("====================");
            //第二种:通过map.entrySet使用 iterator遍历key和value
            Set<Map.Entry<String, Integer>> entries = map.entrySet();
            Iterator<Map.Entry<String, Integer>> it= entries.iterator();
            while (it.hasNext()){
                Map.Entry<String, Integer> next = it.next();
                String key = next.getKey();
                Integer value = next.getValue();
                System.out.println(key+"==="+value);
            }
            System.out.println("===================");
            //第三种:通过map.entrySet遍历key和value
            for (Map.Entry<String, Integer> entry : map.entrySet()) {
                String key = entry.getKey();
                Integer value = entry.getValue();
                System.out.println(key+"==="+value);
            }
        }
    }
    
    
    1. TreeMap集合概述

      ​ 键的数据结构是红黑树,可保证见得排序和唯一性

      ​ 排序分为自然排序和比价器排序

      ​ 线程是不安全的效率比较高

      TreeMap集合键是Student值是String的案例

      按照年龄大小进行排序
      注意键要实现Comparable 接口

    public class TreeMapDemo {
        public static void main(String[] args) {
            TreeMap<Student, String> treeMap = new TreeMap<>();
            Student s1 = new Student("卡莉斯塔", 23);
            Student s2 = new Student("奥利安娜", 26);
            Student s3 = new Student("希维尔", 25);
            Student s4 = new Student("希维尔weier", 26);
            treeMap.put(s1,"s001");
            treeMap.put(s2,"s002");
            treeMap.put(s3,"s003");
            treeMap.put(s4,"s004");
            for (Map.Entry<Student, String> entry : treeMap.entrySet()) {
                Student key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key+"   "+value);
            }
        }
    }
    
    public class Student implements Comparable<Student> {
        private String name;
        private int age;
    
        public Student() {
        }
    
        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 int compareTo(Student o) {
            int num=this.getAge()-o.getAge();
            int num2=num==0?o.getName().compareTo(this.getName()):num;
            return num2;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    
    
    1. 集合嵌套之HashMap嵌套ArrayList
    public class HashMapDemoArrayList {
        public static void main(String[] args) {
            HashMap<String, ArrayList<String>> czbk = new HashMap<>();
            ArrayList<String> jc = new ArrayList<>();
            jc.add("张三   20");
            jc.add("李四   22");
            ArrayList<String> jy = new ArrayList<>();
            jy.add("王五   21");
            jy.add("赵六   23");
            czbk.put("基础班",jc);
            czbk.put("就业班",jy);
            Set<String> keySet = czbk.keySet();
            for (String s : keySet) {
                ArrayList<String> list = czbk.get(s);
                System.out.println(s);
                for (String s1 : list) {
                    System.out.println(s1);
                }
    
                }
            }
        }
    
    1. 集合嵌套之HashMap嵌套HashMap
    public class HashMapDemoHashMap {
        public static void main(String[] args) {
            HashMap<String, HashMap<String, Integer>> czbk = new HashMap<>();
            HashMap<String, Integer> jc = new HashMap<>();
            jc.put("张三",20);
            jc.put("李四",22);
            HashMap<String, Integer> jy = new HashMap<>();
            jy.put("王五",21);
            jy.put("赵六",23);
            czbk.put("基础班",jc);
            czbk.put("就业班",jy);
            Set<String> keySet = czbk.keySet();
            for (String s : keySet) {
                HashMap<String, Integer> hashMap = czbk.get(s);
                System.out.println(s);
                Set<String> keySet1 = hashMap.keySet();
                for (String s1 : keySet1) {
                    Integer age = hashMap.get(s1);
    
                    System.out.println("   "+s1+"  "+age);
    
                }
            }
        }
    }
    
    
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值