Java8 List的常用操作方法

1、List排序(list中包含对象,根据对象属性进行排序)

2020/03/01 22:30 
//升序 
List<ParamVO>paramVOList=resultList.stream()
        .sorted(Comparator.comparingDouble(ParamVO::getDateIndex))
        .collect(Collectors.toList());
//降序 关键方法:reversed()
List<ParamVO>paramVOList=resultList.stream()
        .sorted(Comparator.comparingInt(ParamVO::getDateIndex).reversed())
        .collect(Collectors.toList());

2、List截取(根据List长度,截取对应的集合)

2020/03/02 11:33 
List<Integer> findList = equipmentIdList.subList(0, 5);
//0:开始的下标,5:结束的下标(不包含自身)

3、Java8 localDateTime设置,及格式化

2020/03/02 14:22 
//创建格式化对象
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.S");
//创建时间--自定义
LocalDateTime startLocalDateTime = LocalDateTime.of(2020, 2, 4, 0, 0, 0); 
LocalDateTime endLocalDateTime = LocalDateTime.of(2020, 2, 10, 23, 59, 59); 
//格式化操作转String
String startTime = startLocalDateTime.format(formatter); 
String endTime = endLocalDateTime.format(formatter);

4、各种日期获取时间戳的方法

2020/03/07 08:34 
long time0 = System.currentTimeMillis(); 
long time1 = new Date().getTime(); 
long time2 = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); 
long time3 = Calendar.getInstance().getTimeInMillis(); 
System.out.println(time0); 
System.out.println(time1);
System.out.println(time2); 
System.out.println(time3);

5、List排除空值

2020/03/09 15:52
//主要方法 filter 过滤器 
List<Integer> collect =integerList.stream().
                            filter(Objects::nonNull).collect(Collectors.toList());

6、List根据对象某一个属性求和

//主要方法:sum()。mapToInt、mapToDoule等
Integer eqTotal = consumeVOS.stream()
                    .mapToInt(SingleEquipmentEnergyConsumeVO::getEq).sum();

7、生成随机数

2020/03/11 10:03 
//生成随机int值,0-1,不包含2。 
int num=random.nextInt(2) 

//生成随机double值,最小值50,往上取60的范围 
double num=50 + random.nextDouble() * 60 

//将数字格式化为保留两位小数。货币类型三位一个逗号,保留两位小数的format:",###.##" 
String strNum= new DecimalFormat("0.00").format(num)

8、List根据filter过滤数据

注:可以自定义静态方法,入参为List的泛型,进行业务判断,返回布尔值,返回true的对象stream会进行收集,然后使用collect转为List,filter里面只能放静态方法。

2020/03/16 15:57 
List<String> strs= strList.stream
                    .filter(StringUtils::isNotBlank).collect(Collectors.toList());

9、List根据对象属性去重

注:根据对象属性去重,主要方法:collectingAndThen。

2020/03/16 16:41 
List <User> users = new ArrayList<>(); 
users.stream().collect(Collectors.collectingAndThen(Collectors
     .toCollection(() -> 
        new TreeSet<>(Comparator.comparing(User::getName))), ArrayList::new));

10、List根据对象属性生成新的List

List<User> users = new ArrayList<>(); 

for (int i = 0; i < 10; i++) { 
    users.add(new User((long) i, "AnswerAIL" + i, i)); 
} 

// 获取 users 集合中的 id 集合 
List<Long> ids = users.stream().map(User::getId).collect(Collectors.toList()); 

// 获取 users 集合中的 id 集合并转为字符串, 通过 , 拼接 
String idsTxt = users.stream().map(User::getId).map(String::valueOf)
        .collect(Collectors.joining(",")); 

//获取 users 集合中的 name 集合 
List<String> names = users.stream().map(User::getName).collect(Collectors.toList()); 

//将name通过“,”拼接,并在开始和结尾拼接"(", ")" 
String txt = names.stream().collect(Collectors.joining(",", "(", ")")); 

//替换方法,List转String 
String.join(",", list)

11、List转Map

(key1, key2) -> key2:指定覆盖规则

返回对象本身: Function.identity(),变量名->变量名

2020/03/19 16:14 
Map<Long, String> maps = userList.stream() 
            .collect(Collectors.toMap(User::getId, User::getAge, (key1, key2) -> key2));

12、List根据对象属性分组

2020/04/15 16:46 
Map<Integer, List<PropertyTree>> groupByTenantId = propertyTreeList.stream()
        .collect(Collectors.groupingBy(PropertyTree::getTenantId));

13、MySQL按时间分组格式化。

2020/04/10 17:18 
select * from sys_user as su group by DATE_FORMAT(su.start_time,'%m') 

%M 月 
%W 周 
%D 有英语前缀的月份的日期 
%Y 年,数字4位 
%y 年,数字2位 
%a,缩写的星期名字 
%d,月份中的天数,数字(00-31) 
%e,月份中的天数,数字(0-31) 
%m,月,数字(01-12)
%c,月,数字(1-12) 
%b,缩写的月份名字(Jan-Dec)
%j,一年中的天数(001-366) 
%H,小时(00-23) 
%k,小时(0-23) 
%h,小时(01-12) 
%I,小时(1-12) 
%i,分钟,数字(00-59) 
%r,时间,12小时(hh:mm:ss[AP]M) 
%T,时间,24小时(hh:mm:ss) 
%S,秒(00-59) 
%s,秒(0-59) 
%p,AM或PM 
%w,一个星期中的天数(0=Sunday,0=Saturday) 
%U,星期(0-52),周末第一天 
%u,星期(0-52),周一第一天 
%%一个文字“%”。
扫描二维码关注公众号,回复: 17233962 查看本文章

猜你喜欢

转载自blog.csdn.net/qq_42080073/article/details/105610916