Java8---Lambda函数编程练习

来源:互联网 发布:知否剧组照片 编辑:程序博客网 时间:2024/05/16 10:52

1、依据Collection.sort对员工类进行排序(先按年龄排序,在按姓名排序)

public class Run {public static void main(String[] args){List<Employee> emps = Arrays.asList(new Employee( "张三", 18, 9999.99),new Employee("李四", 59, 6666.66),new Employee( "王五", 28, 3333.33),new Employee( "赵六", 8, 7777.77),new Employee( "田七", 38, 5555.55));Collections.sort(emps,(x,y)->{//排序if(x.getAge()==y.getAge()){return x.getName().compareTo(y.getName());}else{return Integer.compare(x.getAge(), y.getAge());}});for (Employee employee : emps) {System.out.println(employee.getName()+"\t"+employee.getAge()+"\t"+employee.getSalary());}}}

赵六87777.77张三189999.99王五283333.33田七385555.55李四596666.66


2、字符串去空格,字符串获取子串

@FunctionalInterfaceinterface MyFuntion{public String getValue(String val);}public class Run {public static String method(String val,MyFuntion mf){return mf.getValue(val);}public static String methodSub(String val,MyFuntion mf){return mf.getValue(val);}public static void main(String[] args){System.out.println(method("\t\t\thello  \t",(x)->x.trim()));System.out.println(methodSub("abcdefg he", (x)->x.substring(1, 3)));
hellobc

3、声明一个泛型函数接口,<T,R>其中R是返回值,T是参数,计算两个long的和和积

@FunctionalInterfaceinterface MyFunction<T,R>{public R getR(T x,T y);}public class Run {public static Long add(Long x,Long y,MyFunction<Long,Long> mf){return mf.getR(x,y);}public static Long mul(Long x,Long y,MyFunction<Long,Long> mf){return mf.getR(x,y);}public static void main(String[] args){System.out.println(add(10L, 20L, (x,y)->x+y));System.out.println(add(10L, 20L,(x,y)->x*y));}}

4.四大内置接口的例子

 Function<T,R>函数型接口    R   apply(T t)

Predicate<T>断言型接口    boolean  test( T t)

Suppiler<T> 供给型接口  T get()

Consumer<T>消费型接口  void accept(T t)



public class Run {public static void conMethod(double money,Consumer<Double> c){c.accept(money);}//产生一些整数 放到集合中public static List<Integer> supMethod(int num,Supplier<Integer> supplier){List<Integer> list=new ArrayList();for(int i=0;i<num;i++){list.add(supplier.get());}return list;}public static String funMethod(String str,Function<String,String> f){return f.apply(str);}public static List<String > preMethod(List<String> strs,Predicate<String> predicate){List<String> list=new ArrayList();for(String str:strs){if(predicate.test(str)) list.add(str);}return list;}public static void main(String[] args){conMethod(100d,(money)->System.out.println("消费"+money+"元"));System.out.println("-----------------------------");List<Integer> list=supMethod(20,()->{Random r=new Random();return r.nextInt(100);});for (Integer integer : list) {System.out.print(integer+" ");}System.out.println("-----------------------------");System.out.println(funMethod("helloworld",(str)->str.replace('o', 'O')));System.out.println("-----------------------------");List<String> strs=Arrays.asList("hello","world","abc","   x","hahahahaha");List<String> strList=preMethod(strs,(str)->str.length()>3);for(String s:strList){System.out.println(s);}}}



消费100.0元-----------------------------42 58 75 87 63 25 81 39 12 15 99 97 30 65 22 80 47 54 63 26 -----------------------------hellOwOrld-----------------------------helloworld   xhahahahaha



0 0
原创粉丝点击