1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public class LambdaTest2 {
@Test public void test1(){
happyTime(500, new Consumer<Double>() { @Override public void accept(Double aDouble) { System.out.println("学习太累了,去天上人间买了瓶矿泉水,价格为:" + aDouble); } });
System.out.println("********************");
happyTime(400,money -> System.out.println("学习太累了,去天上人间喝了口水,价格为:" + money)); }
public void happyTime(double money, Consumer<Double> con){ con.accept(money); }
@Test public void test2(){ List<String> list = Arrays.asList("北京","南京","天津","东京","西京","普京");
List<String> filterStrs = filterString(list, new Predicate<String>() { @Override public boolean test(String s) { return s.contains("京"); } });
System.out.println(filterStrs);
List<String> filterStrs1 = filterString(list,s -> s.contains("京")); System.out.println(filterStrs1); }
public List<String> filterString(List<String> list, Predicate<String> pre){
ArrayList<String> filterList = new ArrayList<>();
for(String s : list){ if(pre.test(s)){ filterList.add(s); } }
return filterList;
}
}
|