函数式接口:只有一个抽象方法的接口
一、lambda表达式(方法参数为函数式接口)
1.无参
public interface MyFunctionalInterface { void method();}
public class TestJdk8 { public static void show(MyFunctionalInterface myInter) { myInter.method(); } public static void main(String[] args) { show(() -> System.out.println("使用无参lambda表达式")); }}
2.创建线程
public class TestJdk8 { public static void startThread(Runnable runnable) { new Thread(runnable).start(); } public static void main(String[] args) { startThread(() -> System.out.println(Thread.currentThread().getName() + " --> 线程启动了")); }}
3.Supplier接口
public class TestJdk8 { public static String getStr(Suppliersup) { return sup.get(); } public static void main(String[] args) { String str = getStr(() -> "头条"); System.out.println(str); }}
4.Consumer接口
public class TestJdk8 { public static void method(String name, Consumercon) { con.accept(name); } public static void main(String[] args) { method("百度", name -> System.out.println(name)); }}
5.Predicate接口
public class TestJdk8 { public static boolean checkStr(String str, Predicatepre) { return pre.test(str); } public static void main(String[] args) { boolean b = checkStr("abcde", str -> str.length() > 5); System.out.println(b); }}
6.Function接口(类型转换)
public class TestJdk8 { public static void change(String str, Functionfun) { Integer i = fun.apply(str); System.out.println(i + 1); } public static void main(String[] args) { change("1234", s -> Integer.parseInt(s)); }}
二、stream流
1.foreach
public class TestJdk8 { public static void main(String[] args) { Streamstream = Stream.of("张三", "李四", "王五", "赵六", "田七"); stream.forEach(name -> System.out.println(name)); }}
2.filter(底层是Predicate接口)
public class TestJdk8 { public static void main(String[] args) { Streamstream = Stream.of("张三丰", "张无忌", "赵敏", "周芷若", "张翠山", "张三"); stream.filter(name -> name.startsWith("张")) .filter(name -> name.length() > 2) .forEach(name -> System.out.println(name)); }}
3.map(底层是Function接口)
public class TestJdk8 { public static void main(String[] args) { Streamstream = Stream.of("1", "2", "3", "4"); stream.map(s -> Integer.parseInt(s) + 2) .forEach(i -> System.out.println(i)); }}
4.count
public class TestJdk8 { public static void main(String[] args) { Streamstream = Stream.of("张三丰", "张无忌", "赵敏", "周芷若", "张翠山", "张三"); System.out.println(stream.count()); }}
5.limit(skip方法与之相反,不再赘述)
public class TestJdk8 { public static void main(String[] args) { ArrayListlist = new ArrayList<>(); list.add("美羊羊"); list.add("喜羊羊"); list.add("懒羊羊"); list.add("沸羊羊"); list.add("灰太狼"); list.add("红太狼"); list.stream().limit(5).forEach(s -> System.out.println(s)); }}
6.collect(转换为集合)
ListplatformChannelInfos = platformChannelPOs.stream() .map(platformChannelPO -> PlatformChannelPoConverter.po2Bo(platformChannelPO)) .collect(Collectors.toList());
三、方法引用
1.sout
public class TestJdk8 { public static void main(String[] args) { ArrayListlist = new ArrayList<>(); list.add("美羊羊"); list.add("喜羊羊"); list.add("懒羊羊"); list.stream().forEach(System.out::println); }}
2.静态方法
public interface Calculate { int calAbs(int num);}
public class TestJdk8 { public static int method(int num, Calculate c) { return c.calAbs(num); } public static void main(String[] args) { System.out.println(method(-8, Math::abs)); }}
3.构造方法
public class Person { private String name; public Person() { } public Person(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; }}
public interface PersonBuilder { Person buildPerson(String name);}
public class TestJdk8 { public static void buildPerson(String name, PersonBuilder pb) { Person person = pb.buildPerson(name); System.out.println(person + person.getName()); } public static void main(String[] args) { buildPerson("张学友", Person::new); }}
4.构造数组
public interface ArrayBuilder { int[] buildArray(int length);}
public class TestJdk8 { public static int[] creatArray(int length, ArrayBuilder ab) { return ab.buildArray(length); } public static void main(String[] args) { int[] array = creatArray(12, int[]::new); System.out.println(array.length); }}