gistfile1.txt package com.wpx.demo01;import java.util.ArrayList;import java.util.Arrays;import java.util.List;import java.util.function.Consumer;import java.util.function.Function;import java.util.function.Predicate;import java.util.functio
package com.wpx.demo01; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; import org.junit.Test; /** * java 8 -_-新特性 四大内置核心函数式接口 * @author wangpx * 有去无回 consumervoid accept(T t) //消费式 * 自食其力 Supplier T get(); //供给式 * 抛砖引玉 Function R apply(T t); //函数式 * 是是非非 Predicate boolean(T t); //断言式 */ public class Test01 { //有去无回 Consumer @Test public void test01() { youquwuhui(20, (r) -> System.out.println(r+"个肉包子有去无回")); } public void youquwuhui(Integer num,Consumer con) { con.accept(num); } //自食其力 Supplier @Test public void test02() { List list=youlaiyouqu(365, () -> "是不是又白过一天"); System.out.println("白活"+list.stream().count()+"天"); } //赚多少,花多少,钱包扁的刚刚好 public List youlaiyouqu(Integer day,Supplier s) { List list=new ArrayList (); for (int i=0;i str.substring(3, 4)); System.out.println(yu); } public String paozhuanyinyu(String str,Function fun) { return fun.apply(str); } //是是非非 Predicate @Test public void test04() { List r=shishifeifei(Arrays.asList("是","是","非","非"),(e)-> e.equals("是") ); for (String string : r) { System.out.println(string); } } public List shishifeifei(List list,Predicate p){ List isList=new ArrayList (); for (String string : list) { if(p.test(string)) { isList.add(string); } } return isList; } }