天天看點

map方法compute/computeIfAbsent/computeIfPresent/merge/putIfAbsent/forEach/getOrDefault

/**
         *  forEach(BiConsumer<? super K, ? super V> action)
         *  循環key,value
         */
        @Test
        public void testHashMapForEach(){
            Map<String,String> map = new HashMap<String, String>();
            map.put("a","aaa");
            map.put("b","bbb");
            map.put("c","ccc");
            map.put("d","ddd");

            map.forEach((k,v)->{
                System.out.println(k+"  "+v);
            });
            /**
             * a  aaa
             * b  bbb
             * c  ccc
             * d  ddd
             */

        }

        /**
         *  V getOrDefault(Object key, V defaultValue)
         *  如果根據key得到value為null并且不包含這個key,則傳回預設設定的值
         */
        @Test
        public void testHashMapGetOrDefault(){
            Map<String,String> map = new HashMap<String, String>();
            map.put("a","aaa");
            map.put("b",null);
            map.put("c","ccc");
            map.put("d","ddd");

            String orDefault1 = map.getOrDefault("a", "eeee");
            String orDefault2 = map.getOrDefault("b", "eeee");
            String orDefault3 = map.getOrDefault("m", "eeee");
            System.out.println(orDefault1+"  "+orDefault2+"   "+orDefault3);
            /**
             * aaa  null   eeee
             */

        }


        /**
         * compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) {
         *         Objects.requireNonNull(remappingFunction);
         *         V oldValue = get(key);
         *         V newValue = remappingFunction.apply(key, oldValue);
         *
         *   隻要新value不為null就會插入新key或者覆寫原來value,如果新value為null,就會把這個鍵值對直接删除
         */
        @Test
        public void testHashMapCompute(){
            List<Student> studentList = Arrays.asList(
                    new Student("apple",null,10),
                    //new Student("orange","女",30),
                    //  new Student("pink",null,60),
                    new Student("pink","男",60),
                    new Student("black","男",60),
                    new Student("orange",null,150)
            );
            Map<String,String> map = new HashMap<String, String>();
            map.put("pink",null);//原來為空,測試現在不為空
            map.put("orange","女");//原來不為空,測試現在為空
            map.put("apple",null);//原來為空,測試現在為空
            studentList.stream().forEachOrdered(student->map.compute(student.getName(),(k,v)->{
                return student.getSex();}));
            System.out.println(map);
            // {pink=男, black=男}
        }

        /**
         * computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction) {
         *         Objects.requireNonNull(mappingFunction);
         *         傳回值就是Function裡面的V,是以computeIfAbsent方法執行後還可以繼續做一系列的操作,
         *         但是value已經放到集合中了,是以隻有引用類型才可以繼續操作,String不行
         *
         * 隻要是原來的value為null,就會插入或者更新,如果不為null,不變
         * 如果新的value為null,則不做操作,原來的資料不會删除
         *
         */
        @Test
        public void testHashMapComputeIfAbsent(){
            List<Student> studentList = Arrays.asList(
                    new Student("apple",null,10),
                    //new Student("orange","女",30),
                    //  new Student("pink",null,60),
                    new Student("pink","男",60),
                    new Student("black","男",60),
                    new Student("orange",null,150)
            );
            Map<String,String> map = new HashMap<String, String>();
            map.put("pink",null);//原來為空,測試現在不為空
            map.put("orange","女");//原來不為空,測試現在不為空
            studentList.stream().forEachOrdered(student->map.computeIfAbsent(student.getName(),k->k).toUpperCase());
            //  studentList.stream().forEachOrdered(student->map.computeIfAbsent(student.getName(),k->null));
            System.out.println(map);
            // {{orange=女, apple=apple, pink=pink, black=black}   toUpperCase沒有達到效果
        }

        @Test
        public void testHashMapComputeIfAbsent2(){
            List<Student> studentList = Arrays.asList(
                    new Student("apple",null,10),
                    new Student("pink","男",60),
                    new Student("black","男",60),
                    new Student("black","女",80)
            );
            Map<String,List<Student>> map = new HashMap<>();
            studentList.stream().forEachOrdered(
                    student->map.computeIfAbsent(student.getName(),k->new ArrayList<Student>()).add(student)
            );
            System.out.println(map);
            // {
            // apple=[Student{name='apple', sex='null', money=10.0}],
            // pink=[Student{name='pink', sex='男', money=60.0}],
            // black=[Student{name='black', sex='男', money=60.0}, Student{name='black', sex='女', money=80.0}]
            // }   add達到效果了
        }


        /**
         * computeIfPresent(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)
         *
         * 不會插入新key-value,
         * 根據key擷取value,
         * 如果oldValue不為null, newValue為null則删除,newValue不為null則更新,
         * 如果oldValue為null,當不存在這個鍵值對處理,不做任何操作
         */
        @Test
        public void testHashMapComputeIfPresent(){
            List<Student> studentList = Arrays.asList(
                    new Student("apple","女",10),
                    new Student("pink","男",60),
                    new Student("black","男",60),
                    new Student("red","女",60),
                    new Student("orange",null,150)
            );
            Map<String,String> map = new HashMap<String, String>();
            map.put("pink",null);//原來為空,測試現在不為空
            map.put("orange","女");//原來不為空,測試現在為空
            map.put("apple","男");//原來不為空,測試現在不為空
            studentList.stream().forEachOrdered(student->map.computeIfPresent(student.getName(),(k,v)->{
                return student.getSex();}));
            System.out.println(map);
            // {apple=女, pink=null}
        }

        /**
         *  V merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)
         *  根據key擷取value為null,則用新value,
         *  若不為null, oldvalue與newValue會執行這個函數remappingFunction,指派給目前key
         */
        @Test
        public void testHashMapMerge(){
            Map<String,String> map = new HashMap<String, String>();
            map.put("a","aaa");
            map.put("b",null);

            map.merge("a","hahaha",String::concat);
            map.merge("b","hahaha",String::concat);
            System.out.println(map);
            //{a=aaahahaha, b=hahaha}
        }


        /**
         * default V putIfAbsent(K key, V value)
         *
         * 如果擷取的value為null,則用賦予新的value(插入或者更新)
         */
        @Test
        public void testHashMapPutIfAbsent(){
            Map<String,String> map = new HashMap<String, String>();
            map.put("a","aaa");
            map.put("b",null);

            map.putIfAbsent("a","ddd");
            map.putIfAbsent("b","ddd");
            map.putIfAbsent("c","ddd");
            System.out.println(map);
            //{a=aaa, b=ddd, c=ddd}
        }