当前位置: 首页 > news >正文

Lambda 表达式练习

目录

sorted() 四种排序

List 转 Map

map 映射

对象中 String 类型属性为空的字段赋值为 null

BiConsumer,>

T reduce(T identity, BinaryOperator accumulator)

allMatch(Predicate p)

groupingBy(Function f)

flatMap(Function f)

Optional.ofNullable(T t) 和 T orElse(T other)


先来写个基本类

public class LambdaTest {@Dataclass Student {private Integer id;private String name;private Integer age;public Student(Integer id, String name, Integer age) {this.id = id;this.name = name;this.age = age;}}public List<Student> getStudents() {List<Student> list = new ArrayList<>();list.add(new Student(100, "小明", 10));list.add(new Student(101, "小红", 11));list.add(new Student(102, "小李", 12));list.add(new Student(103, "小王", 13));list.add(new Student(104, "小张", 14));list.add(new Student(105, "小五", 15));list.add(new Student(106, "小华", 16));return list;}
}

sorted() 四种排序

@Test
public void sortedTest() {List<Student> students = this.getStudents();//正序:常规写法List<Student> collect = students.stream().sorted((a1, a2) -> Integer.compare(a1.getAge(), a2.getAge())).collect(Collectors.toList());collect.forEach(System.out::println);System.out.println();//正序:lambda 简写List<Student> collectComparator = students.stream().sorted(Comparator.comparing(Student::getAge)).collect(Collectors.toList());collectComparator.forEach(System.out::println);System.out.println();//倒序:常规写法List<Student> collectReversed = students.stream().sorted((a1, a2) -> Integer.compare(a2.getAge(), a1.getAge())).collect(Collectors.toList());collectReversed.forEach(System.out::println);System.out.println();//正序:lambda 简写List<Student> collectComparatorReversed = students.stream().sorted(Comparator.comparing(Student::getAge).reversed()).collect(Collectors.toList());collectComparatorReversed.forEach(System.out::println);
}

List 转 Map

@Test
public void listToMapTest() {List<Student> students = this.getStudents();Map<Integer, Student> collect = students.stream().collect(Collectors.toMap(Student::getId, s -> s, (s1, s2) -> s1));collect.keySet().forEach(key -> System.out.println(String.format("key:%s,value:%s", key, collect.get(key))));
}

map 映射

 @Testpublic void mapTest() {List<Student> students = this.getStudents();
//        List<Student> students = Lists.newArrayList(new Student(null, "小炎姬", 5));Integer id = students.stream().map(Student::getId).max(Integer::compareTo).orElse(0);System.out.println(id);}

对象中 String 类型属性为空的字段赋值为 null

public static <T> void stringEmptyToNull(T t) {Class<?> clazz = t.getClass();Field[] fields = clazz.getDeclaredFields();Arrays.stream(fields).filter(f -> f.getType() == String.class).filter(f -> {try {f.setAccessible(true);String value = (String) f.get(t);return StringUtils.isEmpty(value);} catch (Exception ignore) {return false;}}).forEach(field -> {try {field.setAccessible(true);field.set(t, null);} catch (Exception ignore) {}});
}

BiConsumer<T, U>

<!-- lombok -->
<dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version>
</dependency><!-- guava -->
<dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>28.2-jre</version>
</dependency>
/*** @author vincent*/
public class LambdaTest {@Dataprivate static class Student {private Integer id;private String name;private Integer age;private Integer interestType;private List<Hobby> hobbies;private List<Fancy> fancies;private Subject subject;private Student(Integer id, String name, Integer age, Integer interestType, List<Hobby> hobbies, List<Fancy> fancies, Subject subject) {this.id = id;this.name = name;this.age = age;this.interestType = interestType;this.hobbies = hobbies;this.fancies = fancies;this.subject = subject;}}enum InterestType {HOBBY(1, "兴趣"),FANCY(2, "喜好");private Integer code;private String message;InterestType(Integer code, String message) {this.code = code;this.message = message;}public Integer getCode() {return code;}public static InterestType getInterestTypeByCode(int code) {return Arrays.stream(InterestType.values()).filter(interestType -> interestType.getCode() == code).findFirst().orElse(null);}}@Dataprivate static class Hobby {private String basketball;private String running;private String drinking;private Hobby(String basketball, String running, String drinking) {this.basketball = basketball;this.running = running;this.drinking = drinking;}}@Dataprivate static class Fancy {private String dance;private String takePhotos;private String meetGirls;private Fancy(String dance, String takePhotos, String meetGirls) {this.dance = dance;this.takePhotos = takePhotos;this.meetGirls = meetGirls;}}@Dataprivate static class Subject {private String english;private String chinese;private String mathematics;private Subject(String english, String chinese, String mathematics) {this.english = english;this.chinese = chinese;this.mathematics = mathematics;}}private List<Student> getStudent() {List<Student> list = Lists.newArrayList();list.add(new Student(100, "小明", 10, 1,Lists.newArrayList(new Hobby("篮球", "跑步", "喝酒"),new Hobby("篮球_1", "跑步_1", "喝酒_1"),new Hobby("篮球_2", "跑步_2", "喝酒_2"),new Hobby("篮球_3", "跑步_3", "喝酒_3")),Lists.newArrayList(new Fancy("街舞", "摄影", "泡妞"),new Fancy("街舞_1", "摄影_1", "泡妞_1"),new Fancy("街舞_2", "摄影_2", "泡妞_2"),new Fancy("街舞_3", "摄影_3", "泡妞_3")),new Subject("英语", "语文", "数学")));list.add(new Student(200, "小红", 10, 2,Lists.newArrayList(new Hobby("篮球", "跑步", "喝酒"),new Hobby("篮球_1", "跑步_1", "喝酒_1"),new Hobby("篮球_2", "跑步_2", "喝酒_2"),new Hobby("篮球_3", "跑步_3", "喝酒_3")),Lists.newArrayList(new Fancy("街舞", "摄影", "泡妞"),new Fancy("街舞_1", "摄影_1", "泡妞_1"),new Fancy("街舞_2", "摄影_2", "泡妞_2"),new Fancy("街舞_3", "摄影_3", "泡妞_3")),new Subject("英语", "语文", "数学")));return list;}@Dataprivate static class Person {private Integer pid;private String pname;private Integer page;private String interest;private String subject;}private final static BiConsumer<Person, Student> HOBBY = (person, student) -> {Optional.ofNullable(student.getHobbies()).flatMap(hobbies -> hobbies.stream().findFirst()).ifPresent(hobby -> person.setInterest(hobby.getDrinking()));Optional.ofNullable(student.subject).ifPresent(subject -> person.setSubject(subject.getEnglish()));};private final static BiConsumer<Person, Student> FANCY = (person, student) -> {Optional.ofNullable(student.getFancies()).flatMap(fancies -> fancies.stream().findFirst()).ifPresent(fancy -> person.setInterest(fancy.getDance()));Optional.ofNullable(student.subject).ifPresent(subject -> person.setSubject(subject.getMathematics()));};private final static ImmutableMap<InterestType, BiConsumer<Person, Student>> OF = ImmutableMap.of(InterestType.HOBBY, HOBBY,InterestType.FANCY, FANCY);/*** BiConsumer<T, U> 实例*/@Testpublic void t() {List<Student> studentList = getStudent();List<Object> collect = studentList.stream().map(student -> {Person person = new Person();Optional.ofNullable(student).ifPresent(stu -> {person.setPid(stu.getId());person.setPname(stu.getName());person.setPage(stu.getAge());Integer interestType = student.getInterestType();InterestType interestTypeByCode = InterestType.getInterestTypeByCode(interestType);person.setInterest(interestTypeByCode.message);OF.get(interestTypeByCode).accept(person, student);});return person;}).collect(Collectors.toList());System.out.println(collect);}
}

T reduce(T identity, BinaryOperator<T> accumulator)

<!-- commons-lang3 -->
<dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.9</version>
</dependency>

/*** @author qfxl*/
public class LambdaTest {@Dataprivate static class trip {private String departure;private String destination;private trip(String departure, String destination) {this.departure = departure;this.destination = destination;}}/*** reduce 规则:* 例子:1.上海 -> 北京*      2.北京 -> 上海*      3.天津 -> 西安*      4.拉萨 -> 灵芝*      5.灵芝 -> 兰州*      6.兰州 -> 西宁* 展示效果:上海-北京-上海,天津-西安,拉萨-灵芝-兰州-西宁*/private static final BinaryOperator<String> ACCUMULATOR = (v1, v2) -> {if (StringUtils.isEmpty(v1)) {return v2;}String[] item = StringUtils.split(v1, ",");String[] lastOfItem = StringUtils.split(item[item.length - 1], "-");String lastElement = lastOfItem[lastOfItem.length - 1];String[] nextItem = StringUtils.split(v2, "-");String startElement = nextItem[0];if (StringUtils.equals(lastElement, startElement)) {return v1 + "-" + nextItem[nextItem.length - 1];}return v1 + "," + v2;};@Testpublic void t2() {List<trip> list = Lists.newArrayList(new trip("上海", "北京"),new trip("北京", "上海"),new trip("天津", "西安"),new trip("拉萨", "灵芝"),new trip("灵芝", "兰州"),new trip("兰州", "西宁"));//[上海-北京-上海,天津-西安,拉萨-灵芝-兰州-西宁]String reduce = list.stream().map(t -> String.format("%s-%s", t.getDeparture(), t.getDestination())).reduce("", ACCUMULATOR);System.out.println(reduce);}
}

allMatch(Predicate p)

public static final String YYYYMMDD = "yyyyMMdd";
public static final String YYYY_MM_DD = "yyyy-MM-dd";
public static final String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
public static final String YYYY__MM__DD = "yyyy/MM/dd";
public static final String YYYY__MM__DD_HH_MM = "yyyy/MM/dd HH:mm";
public static final String YYYY__MM__DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";@Test
public void t3() {@Dataclass DateValidate {private String date;private String dateFormat;public DateValidate(String date, String dateFormat) {this.date = date;this.dateFormat = dateFormat;}}List<DateValidate> list = Lists.newArrayList(new DateValidate("202086", YYYYMMDD),new DateValidate("2020086", YYYYMMDD),new DateValidate("2020806", YYYYMMDD),new DateValidate("20200806", YYYYMMDD),new DateValidate("20200806 19", null),new DateValidate("20200806 19:00", null),new DateValidate("20200806 19:00:00", null),new DateValidate("2020-8-06", YYYY_MM_DD),new DateValidate("2020-08-6", YYYY_MM_DD),new DateValidate("2020-08-06", YYYY_MM_DD),new DateValidate("2020-08-06 19", null),new DateValidate("2020-08-06 19:00", YYYY_MM_DD_HH_MM),new DateValidate("2020-08-06 19:00:00", YYYY_MM_DD_HH_MM_SS),new DateValidate("2020/8/06", YYYY__MM__DD),new DateValidate("2020/08/6", YYYY__MM__DD),new DateValidate("2020/08/06", YYYY__MM__DD),new DateValidate("2020/08/06 19", null),new DateValidate("2020/08/06 19:00", YYYY__MM__DD_HH_MM),new DateValidate("2020/08/06 19:00:00", YYYY__MM__DD_HH_MM_SS));list.forEach(item -> {boolean matchDateTime = matchDateTime(item.getDate());System.out.println(item.getDate() + " (" + item.getDateFormat() + "):matchDateTime -> " + matchDateTime);});
}/*** 时间校验* @param dateTime* @return*/
public static boolean matchDateTime(String dateTime) {if (StringUtils.isEmpty(dateTime)) {return false;}String[] dt = dateTime.split("\\s+");if (dt.length == 1) {return dateMatch(dt[0], "/") || dateMatch(dt[0], "-");} else {String date = dt[0];String time = dt[1];return (dateMatch(date, "/") || dateMatch(date, "-")) && timeMatch(time, ":");}
}private static boolean timeMatch(String s, String split) {if (StringUtils.isEmpty(s)) {return true;}s = StringUtils.trim(s);String[] time = StringUtils.split(s, split);boolean isNumber = Arrays.stream(time).anyMatch(StringUtils::isNumeric);if (!isNumber) {return false;}if (time.length != 3) {return false;}if (time[0].length() > 2 || Integer.parseInt(time[0]) > 24) {return false;}if (time[1].length() > 2 || Integer.parseInt(time[1]) > 60) {return false;}if (time[2].length() > 2 || Integer.parseInt(time[2]) > 60) {return false;}return true;
}private static boolean dateMatch(String s, String spl) {if (StringUtils.isEmpty(s)) {return false;}s = StringUtils.trim(s);String[] date = StringUtils.split(s, spl);boolean isNumber = Arrays.stream(date).anyMatch(StringUtils::isNumeric);if (!isNumber) {return false;}if (date.length != 3) {return false;}if (date[0].length() != 4) {return false;}if (Integer.parseInt(date[1]) > 12) {return false;}if (Integer.parseInt(date[2]) > 31) {return false;}return true;
}

显示结果

202086 (yyyyMMdd):matchDateTime -> false
2020086 (yyyyMMdd):matchDateTime -> false
2020806 (yyyyMMdd):matchDateTime -> false
20200806 (yyyyMMdd):matchDateTime -> false
20200806 19 (null):matchDateTime -> false
20200806 19:00 (null):matchDateTime -> false
20200806 19:00:00 (null):matchDateTime -> false
2020-8-06 (yyyy-MM-dd):matchDateTime -> true
2020-08-6 (yyyy-MM-dd):matchDateTime -> true
2020-08-06 (yyyy-MM-dd):matchDateTime -> true
2020-08-06 19 (null):matchDateTime -> false
2020-08-06 19:00 (yyyy-MM-dd HH:mm):matchDateTime -> false
2020-08-06 19:00:00 (yyyy-MM-dd HH:mm:ss):matchDateTime -> true
2020/8/06 (yyyy/MM/dd):matchDateTime -> true
2020/08/6 (yyyy/MM/dd):matchDateTime -> true
2020/08/06 (yyyy/MM/dd):matchDateTime -> true
2020/08/06 19 (null):matchDateTime -> false
2020/08/06 19:00 (yyyy/MM/dd HH:mm):matchDateTime -> false
2020/08/06 19:00:00 (yyyy/MM/dd HH:mm:ss):matchDateTime -> true

groupingBy(Function f)

@Test
public void t5() {@Dataclass Stu {private Integer id;private String name;private Long money;public Stu(Integer id, String name, Long money) {this.id = id;this.name = name;this.money = money;}}List<Stu> list = Lists.newArrayList(new Stu(1, "小明", 100L),new Stu(1, "小红", 200L),new Stu(2, "小黄", 200L),new Stu(2, "小紫", 200L));Map<Integer, List<Stu>> collect = list.stream().collect(Collectors.groupingBy(Stu::getId));System.out.println(JSON.toJSONString(collect, SerializerFeature.PrettyFormat));
}

显示结果

{1:[{"id":1,"money":100,"name":"小明"},{"id":1,"money":200,"name":"小红"}],2:[{"id":2,"money":200,"name":"小黄"},{"id":2,"money":200,"name":"小紫"}]
}

flatMap(Function f)

@Data
private static class TravelInfo {private String trip;private String hotelName;private List<Order> orders;public TravelInfo(String trip, String hotelName, List<Order> orders) {this.trip = trip;this.hotelName = hotelName;this.orders = orders;}
}@Data
private static class Order {private Long orderId;private List<Travellers> travellers;public Order(Long orderId, List<Travellers> travellers) {this.orderId = orderId;this.travellers = travellers;}
}@Data
private static class Travellers {private String userName;private String email;public Travellers() {}public Travellers(String userName, String email) {this.userName = userName;this.email = email;}
}/*** flatMap(Function f):扁平化*/
@Test
public void t5() {TravelInfo travelInfo = new TravelInfo("三人行", "天上人间",Lists.newArrayList(new Order(123456789L, Lists.newArrayList(new Travellers("zhangSanFirst", "zhangSanFirst@qq.com"),new Travellers("zhangSanSecond", "zhangSanSecond@qq.com"))),new Order(987654321L, Lists.newArrayList(new Travellers("liSiFirst", "zhangSanFirst@qq.com"),new Travellers("liSiSecond", "zhangSanSecond@qq.com"))),new Order(987654322L, Lists.newArrayList(new Travellers("wangWu", "wangWu@qq.com"))),new Order(987654323L, Lists.newArrayList()),new Order(987654323L, null)));System.out.println(JSON.toJSONString(travelInfo, SerializerFeature.PrettyFormat));System.out.println();List<String> email = travelInfo.getOrders().stream().filter(Objects::nonNull).map(Order::getTravellers).filter(CollectionUtils::isNotEmpty).flatMap(Collection::stream).filter(Objects::nonNull).map(Travellers::getEmail).collect(Collectors.toList());System.out.println(email);
}

显示结果

{"hotelName":"天上人间","orders":[{"orderId":123456789,"travellers":[{"email":"zhangSanFirst@qq.com","userName":"zhangSanFirst"},{"email":"zhangSanSecond@qq.com","userName":"zhangSanSecond"}]},{"orderId":987654321,"travellers":[{"email":"zhangSanFirst@qq.com","userName":"liSiFirst"},{"email":"zhangSanSecond@qq.com","userName":"liSiSecond"}]},{"orderId":987654322,"travellers":[{"email":"wangWu@qq.com","userName":"wangWu"}]},{"orderId":987654323,"travellers":[]},{"orderId":987654323}],"trip":"三人行"
}[zhangSanFirst@qq.com, zhangSanSecond@qq.com, zhangSanFirst@qq.com, zhangSanSecond@qq.com, wangWu@qq.com]

Optional.ofNullable(T t) 和 T orElse(T other)

@Data
private class PaymentDto {private Long id;private String liqAccountName;/*** 来款业务:COMPANY-企业来款;OFFLINE-订单线下收款*/private String paymentMode;private List<ClaimDto> claimDto;private List<OrderClaimDto> orderClaimDto;public PaymentDto(Long id, String liqAccountName, String paymentMode, List<ClaimDto> claimDto, List<OrderClaimDto> orderClaimDto) {this.id = id;this.liqAccountName = liqAccountName;this.paymentMode = paymentMode;this.claimDto = claimDto;this.orderClaimDto = orderClaimDto;}
}@Data
private static class ClaimDto {private Long paymentId;private String partnerName;private Integer amount;public ClaimDto(Long paymentId, String partnerName, Integer amount) {this.paymentId = paymentId;this.partnerName = partnerName;this.amount = amount;}
}@Data
private static class OrderClaimDto {private Long paymentId;private Integer amount;public OrderClaimDto(Long paymentId, Integer amount) {this.paymentId = paymentId;this.amount = amount;}
}@Data
private static class PaymentApiDto {private Long id;private String liqAccountName;private String paymentMode;private Long paymentId;private String partnerName;private Integer amount;
}private List<PaymentDto> getPaymentDto() {List<PaymentDto> list = Lists.newArrayList();list.add(new PaymentDto(123456789L, "收款账户_COMPANY", "COMPANY",Lists.newArrayList(new ClaimDto(123456789L, "企业名称1", 999),new ClaimDto(123456789L, "企业名称2", 888),new ClaimDto(123456789L, "企业名称3", 777)),Lists.newArrayList(new OrderClaimDto(123456789L, 666),new OrderClaimDto(123456789L, 555))));list.add(new PaymentDto(987654321L, "收款账户_OFFLINE", "OFFLINE",Lists.newArrayList(new ClaimDto(987654321L, "企业名称1", 999),new ClaimDto(987654321L, "企业名称2", 888)),Lists.newArrayList(new OrderClaimDto(987654321L, 666),new OrderClaimDto(987654321L, 555))));list.add(new PaymentDto(888888888L, "收款账户", null, null, null));return list;
}/*** Optional.ofNullable(T t) 和 T orElse(T other)*/
@Test
public void t6() {List<PaymentDto> paymentDtos = getPaymentDto();paymentDtos.forEach(System.out::println);System.out.println();/*** 根据 paymentMode 把 claimDto、orderClaimDto 集合数据查分为单条数据*/List<PaymentApiDto> collect = paymentDtos.stream().map(paymentDto -> {PaymentApiDto apiDto = new PaymentApiDto();apiDto.setId(paymentDto.getId());apiDto.setLiqAccountName(paymentDto.getLiqAccountName());apiDto.setPaymentMode(paymentDto.getPaymentMode());ImmutableList<PaymentApiDto> defaultList = ImmutableList.of(apiDto);if (StringUtils.equals(paymentDto.getPaymentMode(), "COMPANY")) {return Optional.ofNullable(paymentDto.getClaimDto()).map(claimDtos -> claimDtos.stream().map(claimDto -> {PaymentApiDto paymentApiDto = new PaymentApiDto();paymentApiDto.setId(paymentDto.getId());paymentApiDto.setLiqAccountName(paymentDto.getLiqAccountName());paymentApiDto.setPaymentMode(paymentDto.getPaymentMode());paymentApiDto.setPaymentId(claimDto.getPaymentId());paymentApiDto.setPartnerName(claimDto.getPartnerName());paymentApiDto.setAmount(claimDto.getAmount());return paymentApiDto;}).collect(Collectors.toList())).orElse(defaultList);}if (StringUtils.equals(paymentDto.getPaymentMode(), "OFFLINE")) {return Optional.ofNullable(paymentDto.getOrderClaimDto()).map(orderClaimDtos -> orderClaimDtos.stream().map(orderClaimDto -> {PaymentApiDto paymentApiDto = new PaymentApiDto();paymentApiDto.setId(paymentDto.getId());paymentApiDto.setLiqAccountName(paymentDto.getLiqAccountName());paymentApiDto.setPaymentMode(paymentDto.getPaymentMode());paymentApiDto.setAmount(orderClaimDto.getAmount());return paymentApiDto;}).collect(Collectors.toList())).orElse(defaultList);}return defaultList;}).flatMap(Collection::stream).collect(Collectors.toList());collect.forEach(System.out::println);
}

显示结果

LambdaTest.PaymentDto(id=123456789, liqAccountName=收款账户_COMPANY, paymentMode=COMPANY, claimDto=[LambdaTest.ClaimDto(paymentId=123456789, partnerName=企业名称1, amount=999), LambdaTest.ClaimDto(paymentId=123456789, partnerName=企业名称2, amount=888), LambdaTest.ClaimDto(paymentId=123456789, partnerName=企业名称3, amount=777)], orderClaimDto=[LambdaTest.OrderClaimDto(paymentId=123456789, amount=666), LambdaTest.OrderClaimDto(paymentId=123456789, amount=555)])
LambdaTest.PaymentDto(id=987654321, liqAccountName=收款账户_OFFLINE, paymentMode=OFFLINE, claimDto=[LambdaTest.ClaimDto(paymentId=987654321, partnerName=企业名称1, amount=999), LambdaTest.ClaimDto(paymentId=987654321, partnerName=企业名称2, amount=888)], orderClaimDto=[LambdaTest.OrderClaimDto(paymentId=987654321, amount=666), LambdaTest.OrderClaimDto(paymentId=987654321, amount=555)])
LambdaTest.PaymentDto(id=888888888, liqAccountName=收款账户, paymentMode=null, claimDto=null, orderClaimDto=null)LambdaTest.PaymentApiDto(id=123456789, liqAccountName=收款账户_COMPANY, paymentMode=COMPANY, paymentId=123456789, partnerName=企业名称1, amount=999)
LambdaTest.PaymentApiDto(id=123456789, liqAccountName=收款账户_COMPANY, paymentMode=COMPANY, paymentId=123456789, partnerName=企业名称2, amount=888)
LambdaTest.PaymentApiDto(id=123456789, liqAccountName=收款账户_COMPANY, paymentMode=COMPANY, paymentId=123456789, partnerName=企业名称3, amount=777)
LambdaTest.PaymentApiDto(id=987654321, liqAccountName=收款账户_OFFLINE, paymentMode=OFFLINE, paymentId=null, partnerName=null, amount=666)
LambdaTest.PaymentApiDto(id=987654321, liqAccountName=收款账户_OFFLINE, paymentMode=OFFLINE, paymentId=null, partnerName=null, amount=555)
LambdaTest.PaymentApiDto(id=888888888, liqAccountName=收款账户, paymentMode=null, paymentId=null, partnerName=null, amount=null)

相关文章:

Lambda 表达式练习

目录 sorted() 四种排序 List 转 Map map 映射 对象中 String 类型属性为空的字段赋值为 null BiConsumer,> T reduce(T identity, BinaryOperator accumulator) allMatch(Predicate p) groupingBy(Function f) flatMap(Function f) Optional.ofNullable(T t) 和 …...

JavaScript第七讲:数组,及练习题

目录 今天话不多说直接进入正题&#xff01; 1. 创建数组对象 2. 数组长度 3. 遍历一个数组 4. 连接数组 5. 通过指定分隔符&#xff0c;返回一个数组的字符串表达 6. 分别在最后的位置插入数据和获取数据(获取后删除) 7. 分别在最开始的位置插入数据和获取数据(获取后删…...

从docker镜像反推Dockerfile

在项目运维的过程中&#xff0c;偶尔会遇到某个docker image打包时候的Dockerfile版本管理不善无法与image对应的问题&#xff0c;抑或需要分析某个三方docker image的构建过程&#xff0c;这时&#xff0c;就希望能够通过image反推构建时的instruction. 想实现这个过程可以使…...

车载软件架构 - AUTOSAR 的信息安全框架

车载软件架构 - AUTOSAR 的信息安全架构 我是穿拖鞋的汉子&#xff0c;魔都中坚持长期主义的汽车电子工程师。 老规矩&#xff0c;分享一段喜欢的文字&#xff0c;避免自己成为高知识低文化的工程师&#xff1a; 屏蔽力是信息过载时代一个人的特殊竞争力&#xff0c;任何消耗…...

欧洲版“OpenAI”——Mistral 举办的 AI 大模型马拉松

近期&#xff0c;法国的 Mistral AI 举办了一场别开生面的 AI 大模型马拉松。要知道&#xff0c;Mistral 可是法国对 OpenAI 的有力回应&#xff0c;而且其技术还是完全开源的呢&#xff01;这场在巴黎举行的黑客马拉松&#xff0c;规模空前盛大&#xff0c;竟然有超过 1000 名…...

Java | Leetcode Java题解之第128题最长连续序列

题目&#xff1a; 题解&#xff1a; class Solution {public int longestConsecutive(int[] nums) {Set<Integer> num_set new HashSet<Integer>();for (int num : nums) {num_set.add(num);}int longestStreak 0;for (int num : num_set) {if (!num_set.contai…...

C++的List

List的使用 构造 与vector的区别 与vector的区别在于不支持 [ ] 由于链表的物理结构不连续,所以只能用迭代器访问 vector可以排序,list不能排序(因为快排的底层需要随机迭代器,而链表是双向迭代器) (算法库里的排序不支持)(需要单独的排序) list存在vector不支持的功能 链…...

网易有道QAnything使用CPU模式和openAI接口安装部署

网易有道QAnything可以使用本地部署大模型&#xff08;官网例子为qwen&#xff09;也可以使用大模型接口(OPENAI或者其他大模型AI接口 )的方式&#xff0c;使用在线大模型API接口好处就是不需要太高的硬件配置。 本机环境windows11 首先安装WSL环境, 安装方法参考https://zhuan…...

量子加速超级计算简介

本文转载自&#xff1a;量子加速超级计算简介(2024年 3月 13日) By Mark Wolf https://developer.nvidia.cn/zh-cn/blog/an-introduction-to-quantum-accelerated-supercomputing/ 文章目录 一、概述二、量子计算机的构建块&#xff1a;QPU 和量子位三、量子计算硬件和算法四、…...

Unity3D 基于YooAssets的资源管理详解

前言 Unity3D 是一款非常流行的游戏开发引擎&#xff0c;它提供了丰富的功能和工具来帮助开发者快速创建高质量的游戏和应用程序。其中&#xff0c;资源管理是游戏开发中非常重要的一部分&#xff0c;它涉及到如何有效地加载、管理和释放游戏中的各种资源&#xff0c;如模型、…...

Linux 自动化升级Jar程序,指定Jar程序版本进行部署脚本

文章目录 一、环境准备二、脚本1. 自动化升级Jar程序2. 指定Jar程序版本进行部署总结一、环境准备 本文在 CentOS 7.9 环境演示,以springboot为例,打包后生成文件名加上版本号,如下打包之后为strategy-api-0.3.2.jar: pom.xml<?xml version="1.0" encoding=&…...

python练习五

Title1&#xff1a;请实现一个装饰器&#xff0c;每次调用函数时&#xff0c;将函数名字以及调用此函数的时间点写入文件中 代码&#xff1a; import time time time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) # 获取当前的时间戳 # 定义一个有参装饰器来实…...

YOLOv1深入解析与实战:目标检测算法原理

参考&#xff1a; https://zhuanlan.zhihu.com/p/667046384 https://blog.csdn.net/weixin_41424926/article/details/105383064 https://arxiv.org/pdf/1506.02640 1. 算法介绍 学习目标检测算法&#xff0c;yolov1是必看内容&#xff0c;不同于生成模型&#xff0c;没有特别…...

Apache Calcite - 自定义标量函数

前言 上一篇文章中我们介绍了calcite中内置函数的使用。实际需求中会遇到一些场景标准内置函数无法满足需求&#xff0c;这时候就需要用到自定义函数。在 Apache Calcite 中添加自定义函数&#xff0c;以便在 SQL 查询中使用自定义的逻辑。这对于执行特定的数据处理或分析任务…...

STM32作业实现(四)光敏传感器

目录 STM32作业设计 STM32作业实现(一)串口通信 STM32作业实现(二)串口控制led STM32作业实现(三)串口控制有源蜂鸣器 STM32作业实现(四)光敏传感器 STM32作业实现(五)温湿度传感器dht11 STM32作业实现(六)闪存保存数据 STM32作业实现(七)OLED显示数据 STM32作业实现(八)触摸按…...

HTML+CSS 文本动画卡片

效果演示 实现了一个图片叠加文本动画效果的卡片&#xff08;Card&#xff09;布局。当鼠标悬停在卡片上时&#xff0c;卡片上的图片会变为半透明&#xff0c;同时显示隐藏在图片上的文本内容&#xff0c;并且文本内容有一个从左到右的渐显动画效果&#xff0c;伴随着一个白色渐…...

MongoDB CRUD操作: 在本地实例进行文本搜索查询

MongoDB CRUD操作&#xff1a; 在本地实例进行文本搜索查询 文章目录 MongoDB CRUD操作&#xff1a; 在本地实例进行文本搜索查询举例创建集合创建文本索引精准搜索排除短语结果排序 在本地实例运行文本搜索查询前&#xff0c;必须先在集合上建立文本索引。MongoDB提供文本索引…...

文档智能开源软件

文档智能介绍&#xff1a; 文档智能通常指的是利用人工智能技术来处理和分析文档内容&#xff0c;以实现自动化、智能化的文档管理。文档智能的应用领域非常广泛&#xff0c;包括但不限于&#xff1a; 1. **文档识别**&#xff1a;使用OCR&#xff08;光学字符识别&#xff0…...

[C][可变参数列表]详细讲解

目录 1.宏含义及使用2.宏原理分析1.原理2.宏理解 1.宏含义及使用 依赖库stdarg.hva_list 其实就是char*类型&#xff0c;方便后续按照字节进行指针移动 va_start(arg, num) 使arg指向可变参数部分(num后面) va_arg(arg, int) 先让arg指向下个元素&#xff0c;然后使用相对位置…...

54. 螺旋矩阵【rust题解】

题目 给你一个 m 行 n 列的矩阵 matrix &#xff0c;请按照 顺时针螺旋顺序 &#xff0c;返回矩阵中的所有元素。 示例 示例 1 输入&#xff1a;matrix [[1,2,3],[4,5,6],[7,8,9]] 输出&#xff1a;[1,2,3,6,9,8,7,4,5] 示例 2 输入&#xff1a;matrix [[1,2,3,4],[5,6,…...

C++:std::is_convertible

C++标志库中提供is_convertible,可以测试一种类型是否可以转换为另一只类型: template <class From, class To> struct is_convertible; 使用举例: #include <iostream> #include <string>using namespace std;struct A { }; struct B : A { };int main…...

Java多线程实现之Callable接口深度解析

Java多线程实现之Callable接口深度解析 一、Callable接口概述1.1 接口定义1.2 与Runnable接口的对比1.3 Future接口与FutureTask类 二、Callable接口的基本使用方法2.1 传统方式实现Callable接口2.2 使用Lambda表达式简化Callable实现2.3 使用FutureTask类执行Callable任务 三、…...

高等数学(下)题型笔记(八)空间解析几何与向量代数

目录 0 前言 1 向量的点乘 1.1 基本公式 1.2 例题 2 向量的叉乘 2.1 基础知识 2.2 例题 3 空间平面方程 3.1 基础知识 3.2 例题 4 空间直线方程 4.1 基础知识 4.2 例题 5 旋转曲面及其方程 5.1 基础知识 5.2 例题 6 空间曲面的法线与切平面 6.1 基础知识 6.2…...

相机从app启动流程

一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...

EtherNet/IP转DeviceNet协议网关详解

一&#xff0c;设备主要功能 疆鸿智能JH-DVN-EIP本产品是自主研发的一款EtherNet/IP从站功能的通讯网关。该产品主要功能是连接DeviceNet总线和EtherNet/IP网络&#xff0c;本网关连接到EtherNet/IP总线中做为从站使用&#xff0c;连接到DeviceNet总线中做为从站使用。 在自动…...

【JavaSE】绘图与事件入门学习笔记

-Java绘图坐标体系 坐标体系-介绍 坐标原点位于左上角&#xff0c;以像素为单位。 在Java坐标系中,第一个是x坐标,表示当前位置为水平方向&#xff0c;距离坐标原点x个像素;第二个是y坐标&#xff0c;表示当前位置为垂直方向&#xff0c;距离坐标原点y个像素。 坐标体系-像素 …...

Element Plus 表单(el-form)中关于正整数输入的校验规则

目录 1 单个正整数输入1.1 模板1.2 校验规则 2 两个正整数输入&#xff08;联动&#xff09;2.1 模板2.2 校验规则2.3 CSS 1 单个正整数输入 1.1 模板 <el-formref"formRef":model"formData":rules"formRules"label-width"150px"…...

均衡后的SNRSINR

本文主要摘自参考文献中的前两篇&#xff0c;相关文献中经常会出现MIMO检测后的SINR不过一直没有找到相关数学推到过程&#xff0c;其中文献[1]中给出了相关原理在此仅做记录。 1. 系统模型 复信道模型 n t n_t nt​ 根发送天线&#xff0c; n r n_r nr​ 根接收天线的 MIMO 系…...

在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?

uni-app 中 Web-view 与 Vue 页面的通讯机制详解 一、Web-view 简介 Web-view 是 uni-app 提供的一个重要组件&#xff0c;用于在原生应用中加载 HTML 页面&#xff1a; 支持加载本地 HTML 文件支持加载远程 HTML 页面实现 Web 与原生的双向通讯可用于嵌入第三方网页或 H5 应…...

纯 Java 项目(非 SpringBoot)集成 Mybatis-Plus 和 Mybatis-Plus-Join

纯 Java 项目&#xff08;非 SpringBoot&#xff09;集成 Mybatis-Plus 和 Mybatis-Plus-Join 1、依赖1.1、依赖版本1.2、pom.xml 2、代码2.1、SqlSession 构造器2.2、MybatisPlus代码生成器2.3、获取 config.yml 配置2.3.1、config.yml2.3.2、项目配置类 2.4、ftl 模板2.4.1、…...