码迷,mamicode.com
首页 > 其他好文 > 详细

11、Map、可变参数、Collections

时间:2019-02-04 23:17:41      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:turn   shm   java基础   查看   传递参数   hang   单列   binary   reac   

Map接口

Map集合概述

*A:Map集合概述:
    我们通过查看Map接口描述,发现Map接口下的集合与Collection接口下的集合,它们存储数据的形式不同
    a:Collection中的集合,元素是孤立存在的(理解为单身),向集合中存储元素采用一个个元素的方式存储。
    b:Map中的集合,元素是成对存在的(理解为夫妻)。每个元素由键与值两部分组成,通过键可以找对所对应的值。
    Collection中的集合称为单列集合,Map中的集合称为双列集合。
    需要注意的是,Map中的集合不能包含重复的键,值可以重复;每个键只能对应一个值。
    Map
     |--HashMap
     |--LinkedHashMap

Map接口中的常用方法 ?

*A:Map接口中的常用方法
    /*
     *  Map接口中的常用方法
     *    使用Map接口的实现类 HashMap
     */
    public class MapDemo {
        public static void main(String[] args) {
            function_2();
        }

        /*
         *  移除集合中的键值对,返回被移除之前的值
         *  V remove(K)
         */
        public static void function_2() {
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(1, "a");
            map.put(2, "b");
            map.put(3, "c");
            System.out.println(map);
            String value = map.remove(33);
            System.out.println(value);
            System.out.println(map);
        }

        /*
         * 通过键对象,获取值对象
         * V get(K)
         * 如果集合中没有这个键,返回null
         */
        public static void function_1() {
            //创建集合对象,作为键的对象整数,值的对象存储字符串
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(1, "a");
            map.put(2, "b");
            map.put(3, "c");
            System.out.println(map);

            String value = map.get(4);
            System.out.println(value);
        }

        /*
         *  将键值对存储到集合中
         *  V put(K,V) K 作为键的对象, V作为值的对象
         *  存储的是重复的键,将原有的值,覆盖
         *  返回值一般情况下返回null,
         *  存储重复键的时候,返回被覆盖之前的值
         */
        public static void function() {
            //创建集合对象,HashMap,存储对象,键是字符串,值是整数
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("a", 1);

            map.put("b", 2);

            map.put("c", 3);

            System.out.println(map);
        }
    }

Map集合遍历方式keySet方法

*A:Map集合遍历方式keySet方法
    1.获取Map集合中所有的键,由于键是唯一的,所以返回一个Set集合存储所有的键
    2.遍历键的Set集合,得到每一个键
    3.根据键利用get(key)去Map找所对应的值

    /*
     *  Map集合的遍历
     *    利用键获取值
     *    Map接口中定义方法keySet
     *    所有的键,存储到Set集合
     */
    public class MapDemo1 {
        public static void main(String[] args) {
                 /*
                 *  1. 调用map集合的方法keySet,所有的键存储到Set集合中
                 *  2. 遍历Set集合,获取出Set集合中的所有元素 (Map中的键)
                 *  3. 调用map集合方法get,通过键获取到值
                 */
            Map<String, Integer> map = new HashMap<String, Integer>();
            map.put("a", 11);
            map.put("b", 12);
            map.put("c", 13);
            map.put("d", 14);
            //1. 调用map集合的方法keySet,所有的键存储到Set集合中
            Set<String> set = map.keySet();
            //2. 遍历Set集合,获取出Set集合中的所有元素 (Map中的键)
            Iterator<String> it = set.iterator();
            while (it.hasNext()) {
                //it.next返回是Set集合元素,也就是Map中的键
                //3. 调用map集合方法get,通过键获取到值
                String key = it.next();
                Integer value = map.get(key);
                System.out.println(key + "...." + value);
            }

            System.out.println("=======================");
            for (String key : map.keySet()) {
                Integer value = map.get(key);
                System.out.println(key + "...." + value);
            }
        }
    }

Map集合Entry对象

*A:Map集合Entry对象

    interface Map {
        interface Entry {//Entry是Map的一个内部接口
            //由Map的子类的内部类实现
        }
    }

    class HashMap {
        static class Entry<K, V> implements Map.Entry<K, V> {//Entry对象指的就是该类的对象
            final K key;
            V value;
        }
    }
    /*
        在Map类设计时,提供了一个嵌套接口:Entry。
        Entry将键值对的对应关系封装成了对象。
        即键值对对象,这样我们在遍历Map集合时,就可以从每一个键值对(Entry)对象中获取对应的键与对应的值。
        a:Entry是Map接口中提供的一个静态内部嵌套接口。
        b:相关方法
        getKey()方法:获取Entry对象中的键
        getValue()方法:获取Entry对象中的值
        entrySet()方法:用于返回Map集合中所有的键值对(Entry)对象,以Set集合形式返回。
     */

Map集合遍历方式entrySet方法

*A:Map集合遍历方式entrySet方法

    /*
    *Map集合获取方式
    *entrySet方法,键值对映射关系(结婚证)获取
    *实现步骤:
    *1.调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
    *Set<Entry<K, V> >
    *2.迭代Set集合
    *3.获取出的Set集合的元素,是映射关系对象
    *4.通过映射关系对象方法 getKet,getValue获取键值对
    *
    *创建内部类对象 外部类.内部类=new
    */
    public class MapDemo2 {
        public static void main(String[] args) {
            Map<Integer, String> map = new HashMap<Integer, String>();
            map.put(1, "abc");
            map.put(2, "bcd");
            map.put(3, "cde");
            //1. 调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
            Set<Map.Entry<Integer, String>> set = map.entrySet();
            //2. 迭代Set集合
            Iterator<Map.Entry<Integer, String>> it = set.iterator();
            while (it.hasNext()) {
                //  3. 获取出的Set集合的元素,是映射关系对象
                // it.next 获取的是什么对象,也是Map.Entry对象
                Map.Entry<Integer, String> entry = it.next();
                //4. 通过映射关系对象方法 getKet, getValue获取键值对
                Integer key = entry.getKey();
                String value = entry.getValue();
                System.out.println(key + "...." + value);
            }
        }
    }

Map集合遍历方式增强for循环

*A:Map集合遍历方式增强for循环
    *A:Map集合遍历方式entrySet方法

        /*
        *Map集合获取方式
        *entrySet方法,键值对映射关系(结婚证)获取
        *实现步骤:
        *1.调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
        *Set<Entry<K, V> >
        *2.迭代Set集合
        *3.获取出的Set集合的元素,是映射关系对象
        *4.通过映射关系对象方法 getKet,getValue获取键值对
        *
        *创建内部类对象 外部类.内部类=new
        */
        public class MapDemo2 {
            public static void main(String[] args) {
                Map<Integer, String> map = new HashMap<Integer, String>();
                map.put(1, "abc");
                map.put(2, "bcd");
                map.put(3, "cde");
                //1. 调用map集合方法entrySet()将集合中的映射关系对象,存储到Set集合
                Set<Map.Entry<Integer, String>> set = map.entrySet();
                //2. 迭代Set集合
                Iterator<Map.Entry<Integer, String>> it = set.iterator();
                while (it.hasNext()) {
                    //  3. 获取出的Set集合的元素,是映射关系对象
                    // it.next 获取的是什么对象,也是Map.Entry对象
                    Map.Entry<Integer, String> entry = it.next();
                    //4. 通过映射关系对象方法 getKet, getValue获取键值对
                    Integer key = entry.getKey();
                    String value = entry.getValue();
                    System.out.println(key + "...." + value);
                }
                System.out.println("=========================");
                for (Map.Entry<Integer, String> entry : map.entrySet()) {
                    System.out.println(entry.getKey() + "..." + entry.getValue());
                }
            }
        }

    注意:Map集合不能直接使用迭代器或者foreach进行遍历。但是转成Set之后就可以使用了。

HashMap集合存储和遍历

*A:HashMap集合存储和遍历

    /*
     *  使用HashMap集合,存储自定义的对象
     *  自定义对象,作为键,出现,作为值出现
     */
    public class HashMapDemo {
        public static void main(String[] args) {
            function_1();
        }

        /*
         * HashMap 存储自定义对象Person,作为键出现
         * 键的对象,是Person类型,值是字符串
         * 保证键的唯一性,存储到键的对象,重写hashCode equals
         */
        public static void function_1() {
            HashMap<Person, String> map = new HashMap<Person, String>();
            map.put(new Person("a", 20), "里约热内卢");
            map.put(new Person("b", 18), "索马里");
            map.put(new Person("b", 18), "索马里");
            map.put(new Person("c", 19), "百慕大");
            for (Person key : map.keySet()) {
                String value = map.get(key);
                System.out.println(key + "..." + value);
            }
            System.out.println("===================");
            for (Map.Entry<Person, String> entry : map.entrySet()) {
                System.out.println(entry.getKey() + "..." + entry.getValue());
            }
        }

        /*
         * HashMap 存储自定义的对象Person,作为值出现
         * 键的对象,是字符串,可以保证唯一性
         */
        public static void function() {
            HashMap<String, Person> map = new HashMap<String, Person>();
            map.put("beijing", new Person("a", 20));
            map.put("tianjin", new Person("b", 18));
            map.put("shanghai", new Person("c", 19));
            for (String key : map.keySet()) {
                Person value = map.get(key);
                System.out.println(key + "..." + value);
            }
            System.out.println("=================");
            for (Map.Entry<String, Person> entry : map.entrySet()) {
                String key = entry.getKey();
                Person value = entry.getValue();
                System.out.println(key + "..." + value);
            }
        }
    }

LinkedHashMap的特点

*A:LinkedHashMap的特点

    /*
    *  LinkedHashMap继承HashMap
    *  保证迭代的顺序
    */
    public class LinkedHashMapDemo {
        public static void main(String[] args) {
            LinkedHashMap<String, String> link = new LinkedHashMap<String, String>();
            link.put("1", "a");
            link.put("13", "a");
            link.put("15", "a");
            link.put("17", "a");
            System.out.println(link);
        }
    }

Hashtable的特点

*A:Hashtable的特点

    /*
     *  Map接口实现类 Hashtable
     *  底层数据结果哈希表,特点和HashMap是一样的
     *  Hashtable 线程安全集合,运行速度慢
     *  HashMap 线程不安全的集合,运行速度快
     *
     *  Hashtable命运和Vector是一样的,从JDK1.2开始,被更先进的HashMap取代
     *
     *  HashMap 允许存储null值,null键
     *  Hashtable 不允许存储null值,null键
     *
     *  Hashtable他的孩子,子类 Properties 依然活跃在开发舞台
     */
    public class HashtableDemo {
        public static void main(String[] args) {
            Map<String, String> map = new Hashtable<String, String>();
            map.put(null, null);
            System.out.println(map);
        }
    }

静态导入

*A:静态导入:
    如果本类中有和静态导入的同名方法会优先使用本类的
    如果还想使用静态导入的,依然需要类名来调用
    /*
    * JDK1.5新特性,静态导入
    * 减少开发的代码量
    * 标准的写法,导入包的时候才能使用
    *
    * import static java.lang.System.out;最末尾,必须是一个静态成员
    */
    
    public class StaticImportDemo {
        public static void main(String[] args) {
            out.println("hello");
    
            int[] arr = {1, 4, 2};
            sort(arr);
        }
    }

方法的可变参数

*A:方法的可变参数

    /*
     *  JDK1.5新的特性,方法的可变参数
     *  前提: 方法参数数据类型确定,参数的个数任意
     *  可变参数语法: 数据类型...变量名
     *  可变参数,本质就是一个数组
     */
    public class VarArgumentsDemo {
        public static void main(String[] args) {
            //调用一个带有可变参数的方法,传递参数,可以任意
            //  getSum();
            int sum = getSum(5, 34, 3, 56, 7, 8, 0);
            System.out.println(sum);
        }
    
        /*
         * 定义方法,计算10个整数和
         * 方法的可变参数实现
         */
        public static int getSum(int... a) {
            int sum = 0;
            for (int i : a) {
                sum = sum + i;
            }
            return sum;
        }
    
        /*
         * 定义方法,计算3个整数和
         */
        /*public static int getSum(int a,int b ,int c){
            return a+b+c;
        }*/
    
        /*
         * 定义方法,计算2个整数和
         */
        /*public static int getSum(int a,int b){
            return a+b;
        }*/
    }

可变参数的注意事项

*A:可变参数的注意事项
    /*
    * 可变参数的注意事项
    * 1. 一个方法中,可变参数只能有一个
    * 2. 可变参数,必须写在参数列表的最后一位
    */
    public static void function(Object...o){}

Collections工具类?

*A:Collections工具类

    /*
     *  集合操作的工具类
     *    Collections
     */
    public class CollectionsDemo {
        public static void main(String[] args) {
            function_2();
        }

        /*
         * Collections.shuffle方法
         * 对List集合中的元素,进行随机排列
         */
        public static void function_2() {
            List<Integer> list = new ArrayList<Integer>();
            list.add(1);
            list.add(5);
            list.add(9);
            list.add(11);
            list.add(8);
            list.add(10);
            list.add(15);
            list.add(20);
            System.out.println(list);
            //调用工具类方法shuffle对集合随机排列
            Collections.shuffle(list);
            System.out.println(list);
        }

        /*
         * Collections.binarySearch静态方法
         * 对List集合进行二分搜索,方法参数,传递List集合,传递被查找的元素
         */
        public static void function_1() {
            List<Integer> list = new ArrayList<Integer>();
            list.add(1);
            list.add(5);
            list.add(8);
            list.add(10);
            list.add(15);
            list.add(20);
            //调用工具类静态方法binarySearch
            int index = Collections.binarySearch(list, 16);
            System.out.println(index);
        }

        /*
         *  Collections.sort静态方法
         *  对于List集合,进行升序排列
         */
        public static void function() {
            //创建List集合
            List<String> list = new ArrayList<String>();
            list.add("ewrew");
            list.add("qwesd");
            list.add("Qwesd");
            list.add("bv");
            list.add("wer");
            System.out.println(list);
            //调用集合工具类的方法sort
            Collections.sort(list);
            System.out.println(list);
        }
    }

集合的嵌套

*A:集合的嵌套

    /*
     *  Map集合的嵌套,Map中存储的还是Map集合
     *  要求:
     *    传智播客
     *      Java基础班
     *        001  张三
     *        002  李四
     *
     *      Java就业班
     *        001  王五
     *        002  赵六
     *  对以上数据进行对象的存储
     *   001 张三  键值对
     *   Java基础班: 存储学号和姓名的键值对
     *   Java就业班:
     *   传智播客: 存储的是班级
     *
     *   基础班Map   <学号,姓名>
     *   传智播客Map  <班级名字, 基础班Map>
     */
    public class MapMapDemo {
        public static void main(String[] args) {
            //定义基础班集合
            HashMap<String, String> javase = new HashMap<String, String>();
            //定义就业班集合
            HashMap<String, String> javaee = new HashMap<String, String>();
            //向班级集合中,存储学生信息
            javase.put("001", "张三");
            javase.put("002", "李四");
            javaee.put("001", "王五");
            javaee.put("002", "赵六");
            //定义传智播客集合容器,键是班级名字,值是两个班级容器
            HashMap<String, HashMap<String, String>> czbk =
                    new HashMap<String, HashMap<String, String>>();
            czbk.put("基础班", javase);
            czbk.put("就业班", javaee);

            keySet(czbk);

        }
    }

集合的嵌套keySet遍历?

*A:集合的嵌套keySet遍历

    /*
     *  Map集合的嵌套,Map中存储的还是Map集合
     *  要求:
     *    传智播客
     *      Java基础班
     *        001  张三
     *        002  李四
     *
     *      Java就业班
     *        001  王五
     *        002  赵六
     *  对以上数据进行对象的存储
     *   001 张三  键值对
     *   Java基础班: 存储学号和姓名的键值对
     *   Java就业班:
     *   传智播客: 存储的是班级
     *
     *   基础班Map   <学号,姓名>
     *   传智播客Map  <班级名字, 基础班Map>
     */
    public class MapMapDemo {
        public static void main(String[] args) {
            //定义基础班集合
            HashMap<String, String> javase = new HashMap<String, String>();
            //定义就业班集合
            HashMap<String, String> javaee = new HashMap<String, String>();
            //向班级集合中,存储学生信息
            javase.put("001", "张三");
            javase.put("002", "李四");
            javaee.put("001", "王五");
            javaee.put("002", "赵六");
            //定义传智播客集合容器,键是班级名字,值是两个班级容器
            HashMap<String, HashMap<String, String>> czbk =
                    new HashMap<String, HashMap<String, String>>();
            czbk.put("基础班", javase);
            czbk.put("就业班", javaee);

            keySet(czbk);

        }

        public static void keySet(HashMap<String, HashMap<String, String>> czbk) {
            //调用czbk集合方法keySet将键存储到Set集合
            Set<String> classNameSet = czbk.keySet();
            //迭代Set集合
            Iterator<String> classNameIt = classNameSet.iterator();
            while (classNameIt.hasNext()) {
                //classNameIt.next获取出来的是Set集合元素,czbk集合的键
                String classNameKey = classNameIt.next();
                //czbk集合的方法get获取值,值是一个HashMap集合
                HashMap<String, String> classMap = czbk.get(classNameKey);
                //调用classMap集合方法keySet,键存储到Set集合
                Set<String> studentNum = classMap.keySet();
                Iterator<String> studentIt = studentNum.iterator();

                while (studentIt.hasNext()) {
                    //studentIt.next获取出来的是classMap的键,学号
                    String numKey = studentIt.next();
                    //调用classMap集合中的get方法获取值
                    String nameValue = classMap.get(numKey);
                    System.out.println(classNameKey + ".." + numKey + ".." + nameValue);
                }
            }

            System.out.println("==================================");
            for (String className : czbk.keySet()) {
                HashMap<String, String> hashMap = czbk.get(className);
                for (String numKey : hashMap.keySet()) {
                    String nameValue = hashMap.get(numKey);
                    System.out.println(className + ".." + numKey + ".." + nameValue);
                }
            }
        }

    }

集合的嵌套entrySet遍历

*A:集合的嵌套entrySet遍历

    /*
     *  Map集合的嵌套,Map中存储的还是Map集合
     *  要求:
     *    传智播客
     *      Java基础班
     *        001  张三
     *        002  李四
     *
     *      Java就业班
     *        001  王五
     *        002  赵六
     *  对以上数据进行对象的存储
     *   001 张三  键值对
     *   Java基础班: 存储学号和姓名的键值对
     *   Java就业班:
     *   传智播客: 存储的是班级
     *
     *   基础班Map   <学号,姓名>
     *   传智播客Map  <班级名字, 基础班Map>
     */
    public class MapMapDemo {
        public static void main(String[] args) {
            //定义基础班集合
            HashMap<String, String> javase = new HashMap<String, String>();
            //定义就业班集合
            HashMap<String, String> javaee = new HashMap<String, String>();
            //向班级集合中,存储学生信息
            javase.put("001", "张三");
            javase.put("002", "李四");

            javaee.put("001", "王五");
            javaee.put("002", "赵六");
            //定义传智播客集合容器,键是班级名字,值是两个班级容器
            HashMap<String, HashMap<String, String>> czbk =
                    new HashMap<String, HashMap<String, String>>();
            czbk.put("基础班", javase);
            czbk.put("就业班", javaee);

            entrySet(czbk);
        }

        public static void entrySet(HashMap<String, HashMap<String, String>> czbk) {
            //调用czbk集合方法entrySet方法,将czbk集合的键值对关系对象,存储到Set集合
            Set<Map.Entry<String, HashMap<String, String>>>
                    classNameSet = czbk.entrySet();
            //迭代器迭代Set集合
            Iterator<Map.Entry<String, HashMap<String, String>>> classNameIt = classNameSet.iterator();
            while (classNameIt.hasNext()) {
                //classNameIt.next方法,取出的是czbk集合的键值对关系对象
                Map.Entry<String, HashMap<String, String>> classNameEntry = classNameIt.next();
                //classNameEntry方法 getKey,getValue
                String classNameKey = classNameEntry.getKey();
                //获取值,值是一个Map集合
                HashMap<String, String> classMap = classNameEntry.getValue();
                //调用班级集合classMap方法entrySet,键值对关系对象存储Set集合
                Set<Map.Entry<String, String>> studentSet = classMap.entrySet();
                //迭代Set集合
                Iterator<Map.Entry<String, String>> studentIt = studentSet.iterator();
                while (studentIt.hasNext()) {
                    //studentIt方法next获取出的是班级集合的键值对关系对象
                    Map.Entry<String, String> studentEntry = studentIt.next();
                    //studentEntry方法 getKey getValue
                    String numKey = studentEntry.getKey();
                    String nameValue = studentEntry.getValue();
                    System.out.println(classNameKey + ".." + numKey + ".." + nameValue);
                }
            }
            System.out.println("==================================");

            for (Map.Entry<String, HashMap<String, String>> me : czbk.entrySet()) {
                String classNameKey = me.getKey();
                HashMap<String, String> numNameMapValue = me.getValue();
                for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) {
                    String numKey = nameMapEntry.getKey();
                    String nameValue = nameMapEntry.getValue();
                    System.out.println(classNameKey + ".." + numKey + ".." + nameValue);
                }
            }
        }
    }

?

总结

Map集合:

? map集合中的元素都是成对出现,成对存储的

? map集合中的元素都是以一对键和值的形式组成存在的,称为键值对,理解为夫妻对

? map集合中的键不能重复存储,值可以重复

? map集合中的每一个键 对应着一个值

? 方法:

? put(K key, V value) 把指定的键与指定的值添加到Map集合中

? remove(Object key) 把指定的键 所对应的键值对元素 在Map集合中删除,返回被删除元素的值

? Set<Map.Entry<K,V>> entrySet() 获取到Map集合中所有的键值对对象的集合(Set集合)

? get(Object key) 根据指定的键,在Map集合中获取对应的值

? Set keySet() 获取Map集合中所有的键,存储到Set集合中

Map集合遍历的两种方式

//方式1:根据键找值的方式
       //a, 获取到Map集合中所有的键,返回对应的Set集合
       //b, 遍历键的集合,获取到每一个键
       //c, 通过键,找到对应的值
       //获取到Map集合中所有的键,返回对应的Set集合
       Set<String> keys=map.keySet();
       //遍历键的集合,获取到每一个键
       for(String key:keys){
           //通过键,找到对应的值
           Student s=map.get(key);
           System.out.println(key+"..."+s.getName()+"..."+s.getAge());
       }

//方式2:根据键值对对象找键和值的方式
       //a, 获取Map集合中所有的键值对元素,返回对应的Set集合
       //b, 遍历键值对元素集合,获取到每一个键值对元素对象
       //c, 通过键值对元素对象,获取对应的键,和对应的值
       //获取Map集合中所有的键值对元素,返回对应的Set集合
       Set<Map.Entry<String, Student>>entrySet=map.entrySet();
       //遍历键值对元素集合,获取到每一个键值对元素对象
       for(Map.Entry<String, Student> entry:entrySet){
           //通过键值对元素对象,获取对应的键,和对应的值
           //找键
           String key=entry.getKey();
           //找值
           Student s=entry.getValue();
           //打印
           System.out.println(key+"..."+s.getName()+"..."+s.getAge());
       }

 

HashMap:

? 特点:

? 是Map集合的子集合

? 底层采用哈希表结构

? HashMap集合中的key不能重复,通过重写hashCode() 与 equals()方法来保证键的唯一。

? 不能保证元素存与取的顺序完全一致

LinkedHashMap:

? 特点:

? 是HashMap集合的子集合

? 底层采用哈希表+链表结构

? LinkedHashMap集合中的key不能重复,通过重写hashCode() 与 equals()方法来保证键的唯一。

Collections中的方法

? public static void sort(List list) 排序

? public static void shuffle(List<?> list) 集合中的元素存储位置随机打乱

11、Map、可变参数、Collections

标签:turn   shm   java基础   查看   传递参数   hang   单列   binary   reac   

原文地址:https://www.cnblogs.com/yoci/p/10352350.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!