Java8学习笔记(八)--方法引入的补充
从形参到实例方法的实参
示例
public class Example { static List<Integer> intList = Arrays.asList(1,2,3,4); public String increment(int i) { return String.valueOf(++i); } @Test public void testLambda() { //.map(this::increment)<=>.map(i->example.increment(i))... intList.stream().map(this::increment).forEach(System.out::println); } }
从形参到目标
示例
Stream.of("a","b").map(String::toUpperCase).forEach(System.out::println);
从形参到构造函数实参
示例
static List<String> strList = Arrays.asList("a","b","c","d"); //.map(String::new) <=> str->new String(str) strList.stream().map(String::new).forEach(System.out::println);
传递两个形参作为实参
示例
List<Integer> intList = Arrays.asList(1,2,3,4); //Integer::sum <=> (a,b)->Integer.sum(a,b) int i =intList.stream().reduce(Integer::sum).get(); System.out.println(i);
第一个形参作为调用的目标而传递
示例
//String::concat <=> (a,b)->a.concat(b) Stream.of("a","b").reduce("",String::concat);
岁月本长而忙者自促;天地本宽而卑者自隘;风花雪月本闲,而劳忧者自冗;天行健,君子以自强不息;地势坤,君子以厚德载物;宠辱不惊,闲看庭前花开花落;去留无意,漫随天外云卷云舒.不妄取,不妄予,不妄想,不妄求,与人方便,随遇而安