zoukankan      html  css  js  c++  java
  • Java第二十天,Map集合(接口)

    Map接口

    一、定义

    Map集合是双列集合,即一个元素包含两个值(一个key,一个value),Collection集合是单列集合。

    定义格式:

    public interface Map<K,V>

    二、注意事项

    • K值(key值)是不允许重复的,而且每个键只能对应一个值。
    • key值和value数据类型可以相同也可以不同。
    • V值(value值)允许重复。
    • key和value是一一对应关系。

    三、常用子类

    • HashMap

    • LinkedHashMap

    HashMap

    1.注意事项

    1. 底层使用哈希表。HashSet集合本质new的就是HashMap的对象,只不过它只利用了它的单列K(key)值,这也是HashSet集合不允许重复值的根源所在。
    2. 无序集合。
    3. 不同步(多线程)。
    4. 实现了Map<K,V>接口。
    5. jdk1.8以前,哈希表结构 = 数组+链表;jdk1.8以后,哈希表结构 = 数组 + 链表(或红黑树,发生哈希冲突的元素达到8个以上)。
    6. 在存储自定义对象时,作为Key的类必须重写hashCode()方法和equals()方法,用以保证key值得唯一性。

    LinkedHashMap

    1.注意事项

    1. 继承自HashMap集合。
    2. LinkedHashMap结构 = 哈希表 + 链表,这保证了LinkedHashMap集合的有序性(即存放和取出元素的顺序是一致的)。

    HashTable

    1.注意事项

    1. 实现了 Map<K,V> 接口。
    2. 底层是哈希表。
    3. 线程同步(单线程),所以速度较慢。
    4. HashTable集合不允许存储null值,null键(其他集合均允许)。
    5. HashTable集合已经被取代了。
    6. HashTable集合的子类 Properties 集合依然被广泛使用,因为它是唯一一个和I/O流配合的集合。

    四、使用方法

    1.Map接口

    (1)常用方法

    pubilc V put(K key,V value)

    注意:

    如果key值已经存在,则返回被替换的value值,如果key值不存在,则返回空。

    当添加的键值已经存在时,value值会被新值替换,而并非添加失败。

    public V remove(K key)

    根据键值删除对应的对值,并且返回被删除掉的键值对应的value值。

    pubilc V get(K key)

    得到指定键值对应的value值。

    public boolean containsKey(K key)

    判断集合中是否存在键值 key

    public boolean containValue(V value)

    判断集合中是否存在value值 value

    (2)测试代码:

    Tea类:

    package com.lanyue.day19;
    
    public class Tea {
    
        public int grade;
        public String name;
        public String school;
    
        public Tea(int grade, String name, String school) {
            this.grade = grade;
            this.name = name;
            this.school = school;
        }
    
        @Override
        public String toString() {
            return name;
        }
    
        public void info(){
    
            System.out.println(this.name + "是" + this.school + this.grade +"年级的班主任");
        }
    }
    

    Stu类:

    package com.lanyue.day19;
    
    public class Stu {
    
        public int grade;
        public String name;
    
        @Override
        public String toString() {
            return name;
        }
    
        public String school;
    
        public Stu(int grade, String name, String school) {
            this.grade = grade;
            this.name = name;
            this.school = school;
        }
    
        public void info(){
    
            System.out.println(this.name + "同学目前就读于" + this.school  + "的" + this.grade + "年级");
        }
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Stu stu = (Stu) o;
            return Objects.equals(name, stu.name);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    }
    

    运行类:

    package com.lanyue.day19;
    
    import java.util.HashMap;
    import java.util.Map;
    import java.util.concurrent.TimeoutException;
    
    public class MapLearn {
    
        public static void main(String[] args) {
    
            Map<Stu,Tea> map = new HashMap<>();
    
            Tea teaOne = new Tea(1,"张晓燕","黄冈中学");
            Tea teaTwo = new Tea(2,"李艳玲","黄冈中学");
            Tea teaThree = new Tea(3,"王建福","黄冈中学");
    
            Stu stuOne = new Stu(1,"小明","黄冈中学");
            Stu stuTwo = new Stu(1,"小力","黄冈中学");
            Stu stuThree = new Stu(1,"小张","黄冈中学");
            Stu stuFour = new Stu(2,"晓明","黄冈中学");
            Stu stuFive = new Stu(2,"晓力","黄冈中学");
            Stu stuSix = new Stu(2,"晓张","黄冈中学");
            Stu stuSeven = new Stu(3,"萧明","黄冈中学");
            Stu stuEight = new Stu(3,"萧力","黄冈中学");
            Stu stuNine = new Stu(3,"萧张","黄冈中学");
            Stu stuTen = new Stu(3,"萧何","黄冈中学");
    
            map.put(stuOne,teaOne);
            map.put(stuTwo,teaOne);
            map.put(stuThree,teaOne);
    
            map.put(stuFour,teaTwo);
            map.put(stuFive,teaTwo);
            map.put(stuSix,teaTwo);
    
            map.put(stuSeven,teaThree);
            map.put(stuEight,teaThree);
            map.put(stuNine,teaThree);
            map.put(stuTen,teaThree);
    
            Tea tea = map.put(stuEight,teaOne);
            System.out.println(tea);
    
            tea = map.get(stuEight);
            System.out.println(tea);
    
            tea = map.remove(stuEight);
            System.out.println(tea);
    
            boolean b1 = map.containsKey(stuEight);
            boolean b2 = map.containsValue(teaThree);
            System.out.println(b1 + " " + b2);
    
            System.out.println(map);
        }
    }
    

    2.遍历Map集合

    方法一:

    Map中有一个方法:

    public K keySet(Map<K,V> map)

    这个方法返回一个Set视图(即将双列Map集合中的K列装换为单列Set接口,并将其实现类对象返回),可以利用Set接口遍历集合中的所有K元素(键值),然后再利用Map中的get(K key)方法得到每个key值对应的value值,如此得以遍历整个集合。

    方法二:

    在Map接口中有一个内部接口——Entry接口。

    一旦Map实现类对象创建, 就会生成一个Entry实现类对象 ==> 。用于生成键值对对象(它是一个整体对象,对象中又包含两个子对象,分别是key对象和value对象)

    步骤:

    Set<Map.Entry<K,Y>> Set对象 = Map对象.entrySet();

    Iterator<Map.Entry<K,Y>> 迭代器对象 = Set对象.iterator();

    然后利用迭代器对象得到所有的Entry对象。

    对每个Entry对象进行getKey()和getValue()方法最终得到所有的key,value对象。

    注意:完成①后,就可以使用增强for循环来遍历所有对象了。

    测试代码:

    Stu类代码:

    package com.lanyue.day19;
    
    import java.util.Objects;
    
    public class Stu {
    
        public int grade;
        public String name;
    
        @Override
        public String toString() {
            return name;
        }
    
        public String school;
    
        public Stu(int grade, String name, String school) {
            this.grade = grade;
            this.name = name;
            this.school = school;
        }
    
        public void info(){
    
            System.out.println(this.name + "同学目前就读于" + this.school  + "的" + this.grade + "年级");
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Stu stu = (Stu) o;
            return Objects.equals(name, stu.name);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(name);
        }
    }
    

    Tea代码类:

    package com.lanyue.day19;
    
    public class Tea {
    
        public int grade;
        public String name;
        public String school;
    
        public Tea(int grade, String name, String school) {
            this.grade = grade;
            this.name = name;
            this.school = school;
        }
    
        @Override
        public String toString() {
            return name;
        }
    
        public void info(){
    
            System.out.println(this.name + "是" + this.school + this.grade +"年级的班主任");
        }
    }
    

    运行代码类:

    package com.lanyue.day19;
    
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Set;
    
    public class MapLearn {
    
        public static void main(String[] args) {
    
            Map<Stu,Tea> map = new HashMap<>();
    
            Tea teaOne = new Tea(1,"张晓燕","黄冈中学");
            Tea teaTwo = new Tea(2,"李艳玲","黄冈中学");
            Tea teaThree = new Tea(3,"王建福","黄冈中学");
    
            Stu stuOne = new Stu(1,"小明","黄冈中学");
            Stu stuTwo = new Stu(1,"小力","黄冈中学");
            Stu stuThree = new Stu(1,"小张","黄冈中学");
            Stu stuFour = new Stu(2,"晓明","黄冈中学");
            Stu stuFive = new Stu(2,"晓力","黄冈中学");
            Stu stuSix = new Stu(2,"晓张","黄冈中学");
            Stu stuSeven = new Stu(3,"萧明","黄冈中学");
            Stu stuEight = new Stu(3,"萧力","黄冈中学");
            Stu stuNine = new Stu(3,"萧张","黄冈中学");
            Stu stuTen = new Stu(3,"萧何","黄冈中学");
    
            map.put(stuOne,teaOne);
            map.put(stuTwo,teaOne);
            map.put(stuThree,teaOne);
    
            map.put(stuFour,teaTwo);
            map.put(stuFive,teaTwo);
            map.put(stuSix,teaTwo);
    
            map.put(stuSeven,teaThree);
            map.put(stuEight,teaThree);
            map.put(stuNine,teaThree);
            map.put(stuTen,teaThree);
    
            view(map);
            viewTwo(map);
        }
    
        public static void view(Map<Stu,Tea> map){
    
            Set<Stu> stuSet = map.keySet();
            Iterator<Stu> it = stuSet.iterator();
            while(it.hasNext()){
    
                Stu stu = it.next();
                Tea tea = map.get(stu);
    
                System.out.println(stu.school + "" + stu.grade + "班" + stu.name + "===>" + tea.school + "的" + tea.grade + "班" + tea
                .name);
    
            }
        }
    
        public static void viewTwo(Map<Stu,Tea> map){
    
            Set<Map.Entry<Stu, Tea>> list = map.entrySet();
            Iterator<Map.Entry<Stu, Tea>> it = list.iterator();
            while(it.hasNext()){
                Map.Entry<Stu, Tea> mapTemp = it.next();
    
                Stu stu = mapTemp.getKey();
                Tea tea = mapTemp.getValue();
    
                System.out.println(stu.school + "" + stu.grade + "班" + stu.name + "------>>" + tea.school + "的" + tea.grade + "班" + tea
                        .name);
    
            }
        }
    }
    

    3.LinkedHashMap

    package com.lanyue.day19;
    
    import java.util.Collections;
    import java.util.LinkedHashMap;
    
    public class LinkedHashMapLearn {
    
        public static void main(String[] args) {
    
            LinkedHashMap<Integer,String> list = new LinkedHashMap<>();
    
            list.put(3,"三");
            list.put(4,"四");
            list.put(5,"五");
            list.put(1,"一");
            list.put(2,"二");
    
            System.out.println(list);
            
        }
    }
    

  • 相关阅读:
    面向对象基础小结
    异常应用场景
    集合应用场景1:迭代器
    集合应用场景2——数据结构
    华为ce交换机 Bridge-Domain NVE
    linux 内核内置模块
    linux bridge 转发 ip
    iptables nat&conntrack
    loopback
    配置集中式网关部署方式的VXLAN示例(静态方式)
  • 原文地址:https://www.cnblogs.com/viplanyue/p/12700529.html
Copyright © 2011-2022 走看看