JDK8新特性、Lambda表达式主要用在函数式接口中、StreamApi、远程调试
远程调试
1.给项目添加一个远程remote执行地址和端口
2.打包,发布
3.执行命令:java -Xdebug -aqent1ib:idwp=transport=dt_socket,server=y,suspend=n,address=8999 -jar ./demo-1.0.jar
4.启动ide中的remote服务。然后就可以正常访问页面进行调试
package com.cn86trading.trading; import org.junit.jupiter.api.Test; /** * Created with IntelliJ IDEA. * * @author : 小黑 * @version : 1.0 * @Project : CN86 * @Package : com.cn86trading.trading * @ClassName : Test02.java * @createTime : 2022/10/28 17:54 * @Email : 747731461@qq.com * @公众号 : 小黑侠 * @Website : https://cunyu1943.github.io * @Description : */ public class Test02 { interface Printer{ int printer(String val); } int Printer(String val, Printer printer) { return printer.printer(val); } public static void main(String[] args) { Test02 test02 = new Test02(); Printer printer = new Printer() { @Override public int printer(String val) { System.out.println(val); return 1; } }; String str = "你好吗"; System.out.println(test02.Printer(str, printer)); Printer printer1 = val -> {System.out.println(val); return 1; }; int i = test02.Printer(str, (val) -> { System.out.println(val); return 1;} ); //System.out.println(); } }
@Data @AllArgsConstructor public class Employee { private Integer id; private Integer age; private String gender; private String firstName; private String lastName; //定义谓词逻辑 public static Predicate<Employee> ageGreaterThan70 = x -> x.getAge() > 70; //定义谓词逻辑 public static Predicate<Employee> genderM = x -> "M".equals(x.getGender()); } List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5); List<Employee> empoyeeList = employees.stream() .filter(e -> e.getAge()>70 && e.getGender().equals("M")) .collect(Collectors.toList()); //可以复用的谓词逻辑 List<Employee> employeeList2 = employees.stream() .filter(Employee.ageGreaterThan70.and(Employee.genderM)) .collect(Collectors.toList()); //可以复用的谓词逻辑 List<Employee> employeeList3 = employees.stream() .filter(Employee.ageGreaterThan70.or(Employee.genderM)) .collect(Collectors.toList()); //negate()取反 List<Employee> employeeList3 = employees.stream() .filter(Employee.ageGreaterThan70.or(Employee.genderM).negate()) .collect(Collectors.toList()); boolean isExistAgeThan70 = employees.stream().anyMatch(e -> e.getAge() > 70);
boolean isExistAgeThan70 = employees.stream().anyMatch(Employee.ageGreaterThan70);//同上一样
boolean isExistAgeThan70 = employees.stream().allMatch(e -> e.getAge() > 10);//所有。员工年龄都大于10岁
boolean isExistAgeThan70 = employees.stream().noneMatch(e -> e.getAge() < 18);//不存在。是否存在年龄小于18岁的童工
Optional<Employee> optionalEmployee = employees.stream().filter(e -> e.getAge() > 40).findFirst();//查找第一个年龄大于40的员工,不存时抛异常
System.out.println(optionalEmployee.get());
boolean isExist = employees.stream().filter(e -> e.getAge() > 40).findFirst().isPresent()//判断是否存在
findFirst().ifPresent();
.findAny()//返回任意一个年龄大于40的员工
findFirst().orElse(new Employee(0, "F", "", ""));//不存在,返回一个默认值
//这种写法叫作:类的方法引用 String::toUpperCase
Stream.of("Monkey", "Lion", "Giraffe", "Lemur")
.mapToInt(String::length)
.forEach(System.out::println);
//map需要返回处理后的数组。 List<Employee> maped = employees.stream() map(e -> { e.setAge(e.getAge()+1); e.setGender(e.getGender().equals("M")?"male":"female"); return e; }).collect(Collectors.toList()); //peek是引用,无需返回数组 List<Employee> maped = employees.stream() peek(e -> { e.setAge(e.getAge()+1); e.setGender(e.getGender().equals("M")?"male":"female"); }).collect(Collectors.toList()); System.out.println(maped); //数组嵌套数组的遍历,map无法遍历 List<String> words = Arrays.asList("Hello", "word"); words.stream().map(w -> w.split("")).forEach(System.out::println); List<String> words = Arrays.asList("Hello", "word"); words.stream().map(w -> Arrays.stream(w.split(""))).forEach(System.out::println); //数组嵌套数组的遍历,使用flatMap解决遍历 //Arrays.stream转换为多维数组 List<String> words = Arrays.asList("Hello", "word"); words.stream().flatMap(w -> Arrays.stream(w.split(""))).forEach(System.out::println);
并行操作(有状态操作时,不要用并行操作)适合于:ArrayList,HashMap等。很好的利用CPU性能
串行操作适合有无状态都可以,适合于:LinkedList和BlockingQueue和IO
List<String> cities = Arrays.asList("Milan", "london", "San Francisco", "Tokyo", "New Delhi"); System.out.println(cities); cities.sort(String.CASE_INSENSITIVE_ORDER);//不区分大小写排序 System.out.println(cities); cities.sort(Comparator.naturalOrder());//区分大小写排序。先大写后小写进行排序 也称为自然排序 System.out.println(cities); cities.sort(Comparator.reverseOrder());//倒序排序 System.out.println(cities); List<Employee> employees = Arrays.asList(e1, e2, e3, e4, e5); employees.sort( Comparator.comparing(Employee::getGender) //.reversed() //和sql排序不一样 order by gender DESC, age DESC .thenComparingInt(Employee::getAge) .reversed()//倒序,在最后面加。 ); //都是正序,不加reversed //都是倒序,最后面加一个reserved //先是倒序(加reserved),然后正序 //先是正序(加reserved),然后倒序(加reserved) employees.forEach(System.out::println);
employees.sort((o1, o2) -> {
return o1.getAge() - o2.getAge();
});
/
List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6); Integer total = integers.stream().reduce(0, (subtotal, element) -> subtotal + element); System.out.println(total); total = integers.stream().reduce(0, Integer::sum); System.out.println(total); List<String> letters = Arrays.asList("H", "e", "l", "l", "o", ",", "w", "o", "r", "l", "d"); String str = letters.stream().reduce("", String::concat); System.out.println(str); Integer ageSum = employees.stream().map(Employee.getAge).reduce(0, Integer::sum);//1.初始值,2累加器 System.out.println(ageSum); //并行流 Integer ageSum2 = employees.parallelStream().map(Employee.getAge).reduce(0, Integer::sum, Integer::sum);//1.初始值,2累加器,3合并器 Integer ageSum3 = employees.stream().reduce(0, (subTotal, emp) -> subTotal+emp.getAge(), Integer::sum);