BiConsumer 的使用

HashMap遍历对BiConsumer的使用:

 public static void main(String[] args){

        HashMap staff = new HashMap<>();
        staff.put("144-25-5464",new Person("Amy lee",18));
        staff.put("566-25-5464",new Person("Amy Wang",13));
        staff.put("258-26-5494",new Person("Amy Zhang",56));
        staff.put("964-15-5774",new Person("Amy Sun",48));

        System.out.println(staff);
        //remove
        staff.remove("566-25-5464");
        staff.put("964-15-5774",new Person("Harry Paul",79));
        System.out.println(staff.get("566-25-5464"));

        staff.forEach(new BiConsumer() {
            @Override
            public void accept(String s, Person person) {
                System.out.println("key:"+s+" , value:"+person.toString());
            }
        });
        System.out.println("-------------------------");
        //lamada表达式可能在比较久的时间执行
        BiConsumer consumer = (a, b) -> System.out.println("a的值:"+a.toString()+"----b的值:"+ b);
        String str ="mmmmmmmmm";
        consumer.accept(5987,str);      


    }

结果:

{144-25-5464=Person{name='Amy lee', age=18}, 258-26-5494=Person{name='Amy Zhang', age=56}, 964-15-5774=Person{name='Amy Sun', age=48}, 566-25-5464=Person{name='Amy Wang', age=13}}
null
key:144-25-5464 , value:Person{name='Amy lee', age=18}
key:258-26-5494 , value:Person{name='Amy Zhang', age=56}
key:964-15-5774 , value:Person{name='Harry Paul', age=79}
-------------------------
a的值:5987----b的值:mmmmmmmmm

BiConsumer 的使用_第1张图片
image.png

BiConsumer 的使用_第2张图片
image.png

你可能感兴趣的:(BiConsumer 的使用)