Java lambda表达式
代码搬运工@ 人气:0一、实现接口
调用一个接口中的方法,传统方法:
接口类A:
package lombda; /** * @author yeqv * @program A2 * @Classname A * @Date 2022/1/25 20:38 * @Email w16638771062@163.com */ public interface A { int po(int i); }
实现接口:
package lombda; /** * @author yeqv * @program A2 * @Classname B * @Date 2022/1/25 20:43 * @Email w16638771062@163.com */ public class B implements A { public int po(int i) { return i * i; } }
调用接口中的方法po()
package lombda; /** * @author yeqv * @program A2 * @Classname LB1 * @Date 2022/1/25 20:35 * @Email w16638771062@163.com */ public class LB1 { public static void main(String[] args) { A ab = new B(); System.out.println(ab.po(5)); } }
运用lambda表达式后的代码可以直接调用接口A中的方法,不用再创建个类实现接口。
package lombda; /** * @author yeqv * @program A2 * @Classname LB1 * @Date 2022/1/25 20:35 * @Email w16638771062@163.com */ public class LB1 { public static void main(String[] args) { var ab = (A) x -> x * x; System.out.println(ab.po(5)); } }
二、数字排序
package lombda; import java.util.Arrays; import java.util.Random; /** * @author yeqv * @program A2 * @Classname LB1 * @Date 2022/1/25 20:35 * @Email w16638771062@163.com */ public class LB1 { public static void main(String[] args) { //定义数组 Integer[] integers = new Integer[]{1, 23, 6, 8, 2, 3}; System.out.println(Arrays.toString(integers)); //正序排序 Arrays.sort(integers); System.out.println(Arrays.toString(integers)); //逆序排序 Arrays.sort(integers, (a, b) -> b - a); System.out.println(Arrays.toString(integers)); //乱序排序 Random random = new Random(); Arrays.sort(integers, (a, b) -> random.nextBoolean() ? 1 : -1); System.out.println(Arrays.toString(integers)); } }
三、字符串排序方法
package lombda; import java.util.Arrays; import java.util.Random; /** * @author yeqv * @program A2 * @Classname LB1 * @Date 2022/1/25 20:35 * @Email w16638771062@163.com */ public class LB1 { public static void main(String[] args) { /* //定义数组 Integer[] integers = new Integer[]{1, 23, 6, 8, 2, 3}; System.out.println(Arrays.toString(integers)); //正序排序 Arrays.sort(integers); System.out.println(Arrays.toString(integers)); //逆序排序 Arrays.sort(integers, (a, b) -> b - a); System.out.println(Arrays.toString(integers)); //乱序排序 Random random = new Random(); Arrays.sort(integers, (a, b) -> random.nextBoolean() ? 1 : -1); System.out.println(Arrays.toString(integers));*/ //定义字符串数组 String[] strings = new String[]{"asd", "rtyuj", "vgj", "bdf"}; System.out.println(Arrays.toString(strings)); //正序 Arrays.sort(strings, (a, b) -> a.compareTo(b)); System.out.println(Arrays.toString(strings)); //逆序 Arrays.sort(strings, (a, b) -> b.compareTo(a)); System.out.println(Arrays.toString(strings)); //乱序 Random random = new Random(); Arrays.sort(strings, (a, b) -> random.nextBoolean() ? 1 : -1); System.out.println(Arrays.toString(strings)); } }
四、对象排序
先建立个User类
package lombda; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; /** * @author yeqv * @program A2 * @Classname User * @Date 2022/1/25 21:26 * @Email w16638771062@163.com */ @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; }
再进行排序
package lombda; import java.util.ArrayList; import java.util.List; /** * @author yeqv * @program A2 * @Classname LB2 * @Date 2022/1/25 21:27 * @Email w16638771062@163.com */ public class LB2 { public static void main(String[] args) { //定义一个集合 List<User> list = new ArrayList<User>(List.of(new User(1, "张三"), new User(5, "张四"), new User(2, "张五"), new User(3, "张六"))); //以user对象的id来排序 list.sort((a, b) -> a.getId() - b.getId()); System.out.println(list); } }
总结
加载全部内容