list的一些操作

1、subList()截取

List<Object> list = new ArrayList<>();
List<Object> subList = list.subList(0, 3);

subList(0, 3)取得的是下标为0到2的元素,不包含下标为3的元素

2、addAll()合并

List<Object> a = new ArrayList<>();
List<Object> b = new ArrayList<>();
a.addAll(b);

3.List<Integer>取最大值最小值排序

List<Integer> list = new ArrayList<>();
System.out.println(Collections.max(list));
System.out.println(Collections.min(list));
Collections.sort(list);

4、分组

List<User> list = new ArrayList<>();
//分组
Map<String, List<User>> groupBySexList = list.stream().collect(Collectors.groupingBy(User::getSex));
//遍历分组
for (Map.Entry<String, List<User>> entryUser : groupBySexList.entrySet()) {
     String key = entryUser.getKey();
     List<User> userList = entryUser.getValue();
}

5、过滤

//过滤、取出性别为女的用户
List<User> list = new ArrayList<>();
List<User> userList = list.stream().filter(a -> "女".equals(a.getSex())).collect(Collectors.toList());

6、批量设置list列表字段为同一个值

list.stream().forEach(a -> a.setName("0"));

7、获取list某个字段组装新list

//获取list对象的某个字段组装成新list
List<Long> userIdList = list.stream().map(a -> a.getId()).collect(Collectors.toList());

8、去重

//去重
List<Long> idList = new ArrayList<Long>();
List<Long> distinctList = idList.stream().distinct().collect(Collectors.toList());
// 多字段
ArrayList<RptEngineServerInfo> collect = queryServerList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(o -> o.getIpAddresses() + "" + o.getPort()))), ArrayList::new));
ArrayList<RptEngineServerInfo> collect1 =queryServerList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(f -> f.getIpAddresses()+f.getPort()))), ArrayList::new));
		

9、排序

//单字段排序,根据id排序
userList.sort(Comparator.comparing(User::getId));
//多字段排序,根据id,年龄排序
userList.sort(Comparator.comparing(User::getId).thenComparing(User::getAge));
10、List 转map
/**
 * List -> Map
 * 需要注意的是:
 * toMap 如果集合对象有重复的key,会报错Duplicate key ....
 *  user1,user2的id都为1。
 *  可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
 */
Map<Long, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, a -> a,(k1,k2)->k1));

11、最值

//最小
Date minDate = userList.stream().map(User::getEntryDate).min(Date::compareTo).get();
        
//最大
Date maxDate = userList.stream().map(User::getEntryDate).max(Date::compareTo).get();

12、求和

//基本类型
int sumAge = userList.stream().mapToInt(User::getAge).sum();
//BigDecimal求和
BigDecimal totalQuantity = userList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimal::add);
//不能过滤bigDecimal对象为null的情况,可能会报空指针,可以用filter方法过滤,或者重写求和方法
// 判断对象空
stream.filter(x -> x!=null)
stream.filter(Objects::nonNull)
// 判断字段空
stream.filter(x -> x.getDateTime()!=null)
// 重写
BigDecimal totalQuantity2 = userList.stream().map(User::getFamilyMemberQuantity).reduce(BigDecimal.ZERO, BigDecimalUtils::sum);
package com.vvvtimes.util;
 
import java.math.BigDecimal;
 
public class BigDecimalUtils {
 
    public static BigDecimal ifNullSet0(BigDecimal in) {
        if (in != null) {
            return in;
        }
        return BigDecimal.ZERO;
    }
 
    public static BigDecimal sum(BigDecimal ...in){
        BigDecimal result = BigDecimal.ZERO;
        for (int i = 0; i < in.length; i++){
            result = result.add(ifNullSet0(in[i]));
        }
        return result;
    }
}

13、map转list

Map<String, String> map = new HashMap<>();
 
// map的所有key转List
List<String> result = new ArrayList(map.keySet());
 
// map的所有values 转List
List<String> result2 = new ArrayList(map.values());
 
// map的所有key转List(Java8流式转换)
List<String> result3 = map.keySet().stream()
	.collect(Collectors.toList());
 
// map的所有values 转List(Java8流式转换)
List<String> result4 = map.values().stream()
	.collect(Collectors.toList());
 
List<String> result5 = map.values().stream()
	.filter(x -> !"apple".equalsIgnoreCase(x))
	.collect(Collectors.toList());
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值