zoukankan      html  css  js  c++  java
  • Function.identity()

    Java 8允许在接口中加入具体方法。接口中的具体方法有两种,default方法和static方法,identity()就是Function接口的一个静态方法。
    Function.identity()返回一个输出跟输入一样的Lambda表达式对象,等价于形如t -> t形式的Lambda表达式

        private static void identity() {
            Stream<String> stream = Stream.of("I", "love", "you", "too");
            Map<String, Integer> map = stream.collect(Collectors.toMap(Function.identity(), String::length));
            System.out.println(map);
        }

    输出结果为:

                   {love=4, too=3, I=1, you=3}

    @Test public void test() {
            List<Person> personList = new ArrayList<>();
            personList.add(new Person("hepengju", 28, 20000.0));
            personList.add(new Person("lisi"    , 44, 40000.0));
            personList.add(new Person("wangwu"  , 55, 50000.0));
            personList.add(new Person("zhaoliu" , 66, 60000.0));
            personList.add(new Person("zhangsan", 33, 33333.0));
            personList.add(new Person("wgr", 23, 10000.0));
            Map<String, Person> collect = personList.stream().collect(Collectors.toMap(Person::getName, Function.identity()));
            collect.forEach((name,p) ->{
                System.out.println(name + ":"+p);
            });
     
    
        }

  • 相关阅读:
    vi/vim 如何添加和删除多行注释
    linux报错:命令未找到
    删除远程分支的方法
    k-vim常见快捷键
    [转]常见linux命令用法介绍
    python库termcolor用法
    gitignore样例解析
    [转]"git rm" 和 "rm" 的区别
    python中的slice用法
    牛客网linux试题-错误整理-20170914
  • 原文地址:https://www.cnblogs.com/ajing2018/p/14262722.html
Copyright © 2011-2022 走看看