介绍ListStringnamesusers.stream().map(User::getName)// FunctionUser, String.collect(Collectors.toList());里User::getName是什么意思意思很明白返回但User::getName 为什么这样写?User::getName 是 Java 8 引入的方法引用语法它是 Lambda 表达式的简写形式。等价于(Useruser)-user.getName()因为 Stream.map() 需要一个 FunctionUser, String 类型的参数RStreamRmap(Function?superT,?extendsRmapper)所以编译器会自动把 User::getName 转换成对应的 FunctionListUserusers...;// 写法一方法引用最简洁ListStringnames1users.stream().map(User::getName).collect(Collectors.toList());// 写法二Lambda 表达式ListStringnames2users.stream().map(user-user.getName()).collect(Collectors.toList());// 写法三匿名内部类最啰嗦ListStringnames3users.stream().map(newFunctionUser,String(){OverridepublicStringapply(Useruser){returnuser.getName();}}).collect(Collectors.toList());三种写法效果完全一样推荐用写法一方法引用方法引用的四种形式形式语法示例等价 Lambda静态方法引用类名::静态方法Integer::parseInts - Integer.parseInt(s)实例方法引用对象::实例方法str::length() - str.length()任意对象的实例方法类名::实例方法String::lengthstr - str.length()构造方法引用类名::newUser::new() - new User()其他例子// 静态方法引用ListStringstrListArrays.asList(1,2,3);ListIntegerintListstrList.stream().map(Integer::parseInt)// 等价于 s - Integer.parseInt(s).collect(Collectors.toList());// 构造方法引用ListUserusersnames.stream().map(User::new)// 等价于 name - new User(name).collect(Collectors.toList());// 任意对象的实例方法ListStringwordsArrays.asList(hello,world);ListIntegerlengthswords.stream().map(String::length)// 等价于 s - s.length().collect(Collectors.toList());User::getName 就是 “调用每个 User 对象的 getName() 方法” 的简写本质是一个 FunctionUser, String。它是 Java 8 方法引用语法比 Lambda 更简洁、可读性更强。