publicclassReferenceToStaticMethod{/**
* @param args the command line arguments
*/publicstaticvoidmain(String[]args){// TODO code application logic hereListnumbers=Arrays.asList(1,2,3,4,5,6,7,8,9,10,11,12,13,15,16);ListprimeNumbers=ReferenceToStaticMethod.findPrimeNumbers(numbers,ReferenceToStaticMethod::isPrime);System.out.println("Prime Numbers are "+primeNumbers);}publicstaticbooleanisPrime(intnumber){if(number==1){returnfalse;}for(inti=2;i<number;i++){if(number%i==0){returnfalse;}}returntrue;}publicstaticListfindPrimeNumbers(Listlist,Predicatepredicate){ListsortedNumbers=newArrayList();list.stream().filter((i)->(predicate.test(i))).forEach((i)->{sortedNumbers.add(i);});returnsortedNumbers;}}
类
ReferenceToStaticMethod
静态方法名
isPrime
Lambda 方式
List primeNumbers = ReferenceToStaticMethod.testPredicate(numbers, a -> ReferenceToStaticMethod.isPrime(a));
Method Reference
List primeNumbers = ReferenceToStaticMethod.testPredicate(numbers, ReferenceToStaticMethod::isPrime);
2 . 构造方法
publicclassReferenceToConstructor{/**
* @param args the command line arguments
*/publicstaticvoidmain(String[]args){// TODO code application logic hereListnumbers=Arrays.asList(4,9,16,25,36);ListsquaredNumbers=ReferenceToConstructor.findSquareRoot(numbers,Double::new);System.out.println("Square root of numbers = "+squaredNumbers);}privatestaticListfindSquareRoot(Listlist,Function<double,double>f){Listresult=newArrayList();list.forEach(x->result.add(f.apply(Math.sqrt(x))));returnresult;}}
类名
Double
new
new
Lambda 方式
List squaredNumbers = ReferenceToConstructor.findSquareRoot(numbers, x -> new Double(x));
Method Reference
List squaredNumbers= ReferenceToConstructor.findSquareRoot(numbers,Double::new);
3 . 特定类型的任意对象的实例方法
publicclassReferenceToInstanceMethodAOPT{/**
* @param args the command line arguments
*/privatestaticclassPerson{privatefinalStringname;privatefinalintage;publicPerson(Stringname,intage){this.name=name;this.age=age;}publicStringgetName(){returnname;}publicintgetAge(){returnage;}}publicstaticvoidmain(String[]args){// TODO code application logic hereListpersons=newArrayList();persons.add(newPerson("Albert",80));persons.add(newPerson("Ben",15));persons.add(newPerson("Charlote",20));persons.add(newPerson("Dean",6));persons.add(newPerson("Elaine",17));ListallAges=ReferenceToInstanceMethodAOPT.listAllAges(persons,Person::getAge);System.out.println("Printing out all ages \n"+allAges);}privatestaticListlistAllAges(Listperson,Function<person,integer="">f){Listresult=newArrayList();person.forEach(x->result.add(f.apply(x)));returnresult;}}
类型
Person
方法名
getAge
Lambda 方式
List allAges = ReferenceToInstanceMethodAOPT.listAllAges(persons, x -> x.getAge());
Method Reference
List allAges = ReferenceToInstanceMethodAOPT.listAllAges(persons, Person::getAge);
4 . 特定对象的实例方法
publicclassReferenceToInstanceMethodOAPO{/**
* @param args the command line arguments
*/publicstaticvoidmain(String[]args){// TODO code application logic hereListnames=newArrayList();names.add("David");names.add("Richard");names.add("Samuel");names.add("Rose");names.add("John");ReferenceToInstanceMethodOAPO.printNames(names,System.out::println);}privatestaticvoidprintNames(Listlist,Consumerc){list.forEach(x->c.accept(x));}}
对象
System.out
实例方法名
println
Lambda 方式
ReferenceToInstanceMethodOAPO.printNames(names, x -> System.out.println(x));