Java8

常规操作

1.如果user实例不为空,则打印年龄

Optional.ofNullable(user).ifPresent(o -> System.out.println(o.getAge()));

在这里插入图片描述

2.如果user实例不为空,则获取年龄,如果为空,新建一个实例,并获取年龄,此时获取的年龄为null

 Integer age = Optional.ofNullable(user).orElse(new User()).getAge();

3.如果user实例不为空,则获取年龄,如果为空,返回年龄为0

 Integer age = Optional.ofNullable(user).map(User::getAge).orElse(0);

4.判断对象是否有值

User user = null;
boolean present = Optional.ofNullable(user).isPresent();
false
JAVA8操作
List<Object> lists = Lists.newArrayList();

1.根据某一元素去重

List<Object> list =  lists.stream().collect(Collectors.collectingAndThen(
        Collectors.toCollection(() -> new TreeSet<>(
                Comparator.comparing(
                        Object::getId))), ArrayList::new));

2.根据name,sex两个属性去重

List<Person> unique = persons.stream().collect(Collectors. collectingAndThen(
                    Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getName() + ";" + o.getSex()))), ArrayList::new)
);

3.去重对象list

List<Object> listAll = lists.stream().distinct().collect(Collectors.toList());

4.过滤

List<Object> listFilter  = lists.stream().filter(item -> StringUtils.isEmpty(item.getPatentName()) || "0".equals(item..getLevel()))
            .collect(Collectors.toList());

5.提取出list对象中的一个属性并去重

List<String> receiptNoList= lists.stream().map(Object::getPatentName)
                        .distinct()//去重
                        .collect(Collectors.toList());

6.集合List转Map

Map<String, String> collect = list.stream().collect(Collectors.toMap(p -> p.getId(), p -> p.getName()));

7.遍历map

items.forEach((k,v) -> System.out.println(k + "---" + v));

8.数值比较

List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
IntSummaryStatistics stats = numbers.stream().mapToInt((x) ->x).summaryStatistics();
System.out.println("列表中最大的数 : " + stats.getMax());
System.out.println("列表中最小的数 : " + stats.getMin());
System.out.println("所有数之和 : " + stats.getSum());
System.out.println("平均数 : " + stats.getAverage());

9.删除list中符合条件的值

students.removeIf(student -> "湖北".equals(student.getAddr()));

10.allmatch 全部匹配

//判断学生年龄是否都大于18
        boolean flag = list.stream().allMatch(stu -> stu.getAge() > 18);

11.anyMatch 部分符合条件

//判断学生年龄是否存在大于18的
        boolean flag = list.stream().anyMatch(stu -> stu.getAge() > 18);

12.findFirst 与 findAny

findFirst是从流Stream中找出第一个元素。而findAny则是从并行流parallelStream中找出任意一个元素。
students.parallelStream().findAny().get();
students.stream().findFirst().get();

13.ifPresent is Present

14 取集合字段求和

int total = list.stream().mapToInt(User::getAge).sum();
//上下等同 total=ageSum
int ageSum = userList.stream().collect(Collectors.summingInt(User::getAge));
JAVA8时间操作
public static void main(String[] args) {        
    // 获取当天日期时间    
    LocalDate today = LocalDate.now();    
    print("获取当天日期时间: ", today); 
    // 加一天 
    LocalDate tomorrow = today.plusDays(1);  
    print("加一天: ", tomorrow);  
    // 加一个月    
    LocalDate nextMonth = today.plusMonths(1);
    print("加一个月: ", nextMonth);    
    // 减一天    
    LocalDate yesterday = today.minusDays(1);    
    print("减一天: ", yesterday);  
    // 减一个月   
    LocalDate lastMonth = today.minusMonths(1);   
    print("减一个月: ", lastMonth);   
    // 获取今天是本月第几天  
    int dayOfMonth = today.getDayOfMonth();  
    print("获取今天是本月第几天: ", dayOfMonth);   
    // 获取今天是本周第几天    
    int dayOfWeek = today.getDayOfWeek().getValue();   
    print("获取今天是本周第几天: ", dayOfWeek);   
    // 获取今天是本年第几天      
    int dayOfYear = today.getDayOfYear();    
    print("获取今天是本年第几天: ", dayOfYear);    
    // 获取本月天数。   
    int daysOfMonth = today.lengthOfMonth();    
    print("获取本月天数: ", daysOfMonth);       
    // 获取本年天数      
    int daysOfYear = today.lengthOfYear();  
    print("获取本年天数: ", daysOfYear);   
    // 获取本月指定的第n天 
    LocalDate date1 = today.withDayOfMonth(15);   
    print("获取本月指定的第n天: ", date1);   
    // 获取本月的最后一天   
    LocalDate lastDaysOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());  
    print("获取本月的最后一天: ", lastDaysOfMonth);      
    // 日期字符串解析。 严格按照ISO yyyy-MM-dd 验证    
    LocalDate date = LocalDate.parse("2021-01-17");  
    print("日期字符串解析: ", date);    
    // 日期字符串解析。 自定义格式      
    DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy-M-dd");    
    LocalDate date2 = LocalDate.parse("2021-1-17", dft);   
    print("日期字符串解析(日期字符串解析): ", date2);    
    // 格式化日期    
    String dateStr = today.format(dft);    
    print("格式化日期: ", dateStr);     
    // 自定义日期     
    LocalDate cusDate = LocalDate.of(2020, 8, 14);     
    print("自定义日期: ", cusDate);    
    // 日期比较      
    boolean before = today.isBefore(tomorrow);      
    print("今天是否比明天早: ", before);   
    boolean before1 = today.isBefore(yesterday);    
    print("今天是否比昨天早: ", before1);     
    boolean after = today.isAfter(tomorrow);    
    print("今天是否比明天晚: ", after);   
    boolean after1 = today.isAfter(yesterday);   
    print("今天是否比昨天晚: ", after1);    
    // 获取两个时间相差多少天/周/月...  根据单位不同返回不同    
    long until = today.until(nextMonth, ChronoUnit.WEEKS);    
    print("今天到下个月相差几周: ", until);    
    Month month = today.getMonth();     
    print("月份:", month);     
    print("月份: ", month.getValue());    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值