代码如下:
package MyInterfacePack01;@FunctionalInterface
public interface MyInterface {void show();
}
package MyInterfacePack01;public class MyInterfaceDemo {public static void main(String[] args){MyInterface my= ()->System.out.println("hello world");my.show();}}
代码如下:
package MyInterfacePack01;public class RunnableDemo {public static void main(String[] args){startThread(new Runnable() {@Overridepublic void run() {System.out.println(Thread.currentThread().getName()+"线程启动了");}});startThread(()->System.out.println(Thread.currentThread().getName()+"线程启动了"));}private static void startThread(Runnable r){new Thread(r).start();}}
代码如下:
package MyInterfacePack01;import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;public class ComparatorDemo {public static void main(String[] args){ArrayList<String> array = new ArrayList<String>();array.add("hello");array.add("javaaaaa");array.add("world");System.out.println("排序前"+array);Collections.sort(array,getComparator());System.out.println("排序后"+array);}private static Comparator<String> getComparator(){
// Comparator<String> comp = new Comparator<String>() {
// @Override
// public int compare(String s1, String s2) {
// return s1.length()-s2.length();
// }
// };
// return comp;return (s1,s2)->s1.length()-s2.length();}
}
代码如下:
package SupplierPack;import java.util.function.Supplier;public class SupplierDemo {public static void main(String[] args){String s = getString(()->{return "Tom";});String s1 = getString(()->"TOM");System.out.println(s);System.out.println(s1);Integer i = getInteger(()->30);System.out.println(i);}private static String getString(Supplier<String> sup){return sup.get();}private static Integer getInteger(Supplier<Integer> sup){return sup.get();}}
代码如下:
package SupplierPack;import java.util.function.Supplier;public class SupplierTest {public static void main(String[] args){int [] arr = {19,50,28,37,64};int maxValue = getMax(()->{int max = arr[0];for (int i = 1;i<arr.length;i++){if (arr[i] > max){max = arr[i];}}return max;});System.out.println(maxValue);}private static int getMax(Supplier<Integer> sup){return sup.get();}}
代码如下:
package SupplierPack;import java.util.function.Consumer;public class ConsumerDemo {public static void main(String[] args){
// operatorString("Tom",(String s)->{
// System.out.println(s);
// });operatorString("Tom",s->System.out.println(s));operatorString("Tom",System.out::println);operatorString("Tom",s->System.out.println(new StringBuilder(s).reverse().toString()));System.out.println("------------------------------------------");operatorString("Jack",s->System.out.println(s),s->System.out.println(new StringBuilder(s).reverse().toString()));}private static void operatorString(String name,Consumer<String> con1,Consumer<String> con2){
// con1.accept(name);
// con2.accept(name);con1.andThen(con2).accept(name);}private static void operatorString(String name, Consumer<String> con){con.accept(name);}}
代码如下:
package SupplierPack;import java.util.function.Consumer;public class ConsumerTest {public static void main(String[] args){String[] strArray = {"Tom,20","Jack,23","Bom,18"};printInfo(strArray,(String str)->{String name = str.split(",")[0];System.out.print("姓名"+name);},(String str)->{int age = Integer.parseInt(str.split(",")[1]);System.out.println(",年龄"+age);});}public static void printInfo(String[] strArray, Consumer<String> con1,Consumer<String> con2){for (String str:strArray){con1.andThen(con2).accept(str);}}}
代码如下:
package SupplierPack;import java.util.function.Predicate;public class PredicateDemo01 {public static void main(String[] args){boolean b1 = checkString("hello",(String s)->{return s.length() > 8;});boolean b2 = checkString("hello",s->s.length() > 8);boolean b3 = checkString("helloworld",s->s.length() > 8);System.out.println(b1);System.out.println(b2);System.out.println(b3);}private static boolean checkString(String s, Predicate<String> pre){
// return pre.test(s);return pre.negate().test(s);}}
package PredicatePack01;import java.io.StringReader;
import java.util.function.Predicate;public class PredicateDemo01 {public static void main(String[] args){boolean b1 = checkString("hello",s->s.length() > 8);System.out.println(b1);boolean b2 = checkString("helloworld",s->s.length()> 8);System.out.println(b2);boolean b3 = checkString("hello",s->s.length() > 8 ,s->s.length()<15);System.out.println(b3);boolean b4 = checkString("helloworld",s->s.length() > 8 , s->s.length() < 15);System.out.println(b4);}private static boolean checkString(String s, Predicate<String> pre1,Predicate<String> pre2){
// boolean b1 = pre1.test(s);
// boolean b2 = pre2.test(s);
// boolean b = b1 && b2;
// return b;
// return pre1.and(pre2).test(s);return pre1.or(pre2).test(s);}private static boolean checkString(String s, Predicate<String> pre){return pre.test(s);}}
代码如下:
package PredicatePack01;import java.util.ArrayList;
import java.util.function.Predicate;public class PredicatePack {public static void main(String[] args){String[] strArray = {"Tom,30","Jack,34","Bom,49","Lily,34","Mike,33"};ArrayList<String> array = myFilter(strArray,s->s.split(",")[0].length()>2,s->Integer.parseInt(s.split(",")[1]) > 33);for (String str:array){System.out.println(str);}}private static ArrayList<String> myFilter(String[] strArray, Predicate<String> pre1,Predicate<String> pre2){ArrayList<String> array = new ArrayList<String>();for (String str:strArray){if(pre1.and(pre2).test(str))array.add(str);}return array;}
}
代码如下:
package PredicatePack01;import java.util.function.Function;public class FunctionDemo {public static void main(String[] args){
// convert("100",(String s)->{
// return Integer.parseInt(s);
// });convert("100",s->Integer.parseInt(s));convert("100",Integer::parseInt);convert(100,i->String.valueOf(i+566));convert("100",s->Integer.parseInt(s),i->String.valueOf(i+566));}private static void convert(String s,Function<String,Integer> fun1,Function<Integer,String > fun2){
// Integer i = fun1.apply(s);
// String ss = fun2.apply(i);
// System.out.println(ss);String ss = fun1.andThen(fun2).apply(s);System.out.println(ss);}private static void convert(int i,Function<Integer,String> fun){String s = fun.apply(i);System.out.println(s);}private static void convert(String s, Function<String,Integer> fun){Integer i = fun.apply(s);System.out.println(i);}}
代码如下:
package PredicatePack01;import java.util.function.Function;public class FunctionTest {public static void main(String[] args){String s = "Tom,35";convert(s,(String ss)->{return ss.split(",")[1];},(String ss)->{return Integer.parseInt(ss);},(Integer i)->{return i+70;});convert(s,ss->ss.split(",")[1],ss->Integer.parseInt(ss),i->i+70);convert(s,ss->ss.split(",")[1],Integer::parseInt,i->i+70);}private static void convert(String s, Function<String,String> fun1,Function<String,Integer> fun2,Function<Integer,Integer> fun3){Integer i = fun1.andThen(fun2).andThen(fun3).apply(s);System.out.println(i);}}