黑马程序员——Java基础——集合(三)

来源:互联网 发布:公安大数据 编辑:程序博客网 时间:2024/05/24 07:16

——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——-
一、Map集合
Map是一个接口,是双列集合,存储的的是键值对形式。
Map集合和Collection集合的区别:
A:Map 存储的是键值对形式的元素,键唯一,值可以重复。
B:Collection 存储的是单独出现的元素,子接口Set元素唯一,子接口List元素可重复。
Map集合的子类:

Map        |--Hashtable: 底层是哈希表结构,不可以存入null键和null值,线程同步,效率低。        |--HashMap: 底层是哈希表结构,允许使用null键和null值,线程不同步,效率高。        |--TreeMap: 底层是二叉树结构,线程不同步,可以用于给Map集合的键进行排序。

二、Map集合常用的方法

1、添加:

V put(K key,V value);//添加元素。如果键是第一次存储,就直接存储元素,返回null;如果键不是第一次存在,就用值把以前的值替换掉,返回以前的值。

2、删除:

void clear();//移除所有的键值对元素。V remove(Object key);//根据键删除键值对元素,并把值返回。

3、判断:

boolean containsKey(Object key);//判断集合是否包含指定的键boolean containsValue(Object value);//判断集合是否包含指定的值boolean isEmpty();//判断集合是否为空

4、长度:

int size();//返回键值对的对数

5、获取:

V get(Object key);//根据键获取值Set<K> keySet();//获取集合中所有键的集合Collection<V> values();//获取集合中所有值的集合Set<Map.Entry<K,V>> entrySet();//返回键值对对象

三、Map集合的两种遍历方式
方式1:根据键找值
步骤:a:获取所有键的集合
b:遍历键的集合,获取到每一个键
c:根据键找值

class MapDemo3 {        public static void main(String[] args) {            // 创建集合对象            Map<String, String> map = new HashMap<String, String>();            // 创建元素并添加到集合            map.put("杨过", "小龙女");            map.put("郭靖", "黄蓉");            map.put("杨康", "穆念慈");            map.put("陈玄风", "梅超风");            // 遍历            // 获取所有的键            Set<String> set = map.keySet();            // 遍历键的集合,获取得到每一个键            for (String key : set) {                // 根据键去找值                String value = map.get(key);                System.out.println(key + "---" + value);            }        }    }

方式2、根据键值对对象找键和值
步骤:a:获取键值对对象的集合
b:遍历键值对对象的集合,获取到每一个键值对对象
c:根据键值对对象找键和值

class MapDemo4 {        public static void main(String[] args) {            // 创建集合对象            Map<String, String> map = new HashMap<String, String>();            // 创建元素并添加到集合            map.put("杨过", "小龙女");            map.put("郭靖", "黄蓉");            map.put("杨康", "穆念慈");            map.put("陈玄风", "梅超风");            // 获取所有键值对对象的集合            Set<Map.Entry<String, String>> set = map.entrySet();            // 遍历键值对对象的集合,得到每一个键值对对象            for (Map.Entry<String, String> me : set) {                // 根据键值对对象获取键和值                String key = me.getKey();                String value = me.getValue();                System.out.println(key + "---" + value);            }        }    }

HashMap:如果键是自定义对象,需要重写hashCode()方法和equals()方法,保证键的唯一

//自定义学生类    class Student {        private String name;        private int age;        public Student() {            super();        }        public Student(String name, int age) {            super();            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;        }        //重写hashCode()方法        @Override        public int hashCode() {            final int prime = 31;            int result = 1;            result = prime * result + age;            result = prime * result + ((name == null) ? 0 : name.hashCode());            return result;        }        //重写equals()方法        @Override        public boolean equals(Object obj) {            if (this == obj)                return true;            if (obj == null)                return false;            if (getClass() != obj.getClass())                return false;            Student other = (Student) obj;            if (age != other.age)                return false;            if (name == null) {                if (other.name != null)                    return false;            } else if (!name.equals(other.name))                return false;            return true;        }    }    class HashMapDemo4 {        public static void main(String[] args) {            // 创建集合对象            HashMap<Student, String> hm = new HashMap<Student, String>();            // 创建学生对象            Student s1 = new Student("貂蝉", 27);            Student s2 = new Student("王昭君", 30);            Student s3 = new Student("西施", 33);            Student s4 = new Student("杨玉环", 35);            Student s5 = new Student("貂蝉", 27);            // 添加元素            hm.put(s1, "8888");            hm.put(s2, "6666");            hm.put(s3, "5555");            hm.put(s4, "7777");            hm.put(s5, "9999");            // 遍历            Set<Student> set = hm.keySet();            for (Student key : set) {                String value = hm.get(key);                System.out.println(key.getName() + "---" + key.getAge() + "---"                    + value);            }        }    }

TreeMap:如果键是自定义对象需要元素实现Comparable接口或者集合实现Comparator接口。

//自定义学生类    class Student {        private String name;        private int age;        public Student() {            super();        }        public Student(String name, int age) {            super();            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;        }    }    class TreeMapDemo2 {        public static void main(String[] args) {            // 创建集合对象            TreeMap<Student, String> tm = new TreeMap<Student, String>(            //匿名内部类实现Comparator接口                new Comparator<Student>() {                    @Override                    public int compare(Student s1, Student s2) {                        // 主要条件                        int num = s1.getAge() - s2.getAge();                        // 次要条件                        int num2 = num == 0 ? s1.getName().compareTo(                                s2.getName()) : num;                        return num2;                    }                });            // 创建学生对象            Student s1 = new Student("潘安", 30);            Student s2 = new Student("柳下惠", 35);            Student s3 = new Student("唐伯虎", 33);            Student s4 = new Student("燕青", 32);            Student s5 = new Student("唐伯虎", 33);            // 存储元素            tm.put(s1, "宋朝");            tm.put(s2, "元朝");            tm.put(s3, "明朝");            tm.put(s4, "清朝");            tm.put(s5, "汉朝");            // 遍历            Set<Student> set = tm.keySet();            for (Student key : set) {                String value = tm.get(key);                System.out.println(key.getName() + "---" + key.getAge() + "---"                    + value);            }        }    }
0 0