Java使用流Stream对集合List进行过滤去重,对象和基本数据类型-数据去重

Java使用流Stream对集合List进行过滤去重,对象和基本数据类型-数据去重

  • 工具方法引用,可提供为工具类
    /**
     * 使用属性过滤集合对象重复数据
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

  • 1、创建工具类 、、、、2、如,本方法测试,直接复制到类引用

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * @Description: CollectionUtil工具类
 * @Author: LeMenPan
 * @Date:   2021-01-17
 * @Version: V-1.0
 */
public class CollectionUtilsPan {

    /**
     * 使用属性过滤集合对象重复数据
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> seen = new ConcurrentHashMap<>();
        return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }
}


  • 方法引用和示例
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.jd.open.api.sdk.internal.util.StringUtil;
import com.kingmed.km2c.baidu.vo.organ.NcpOrganWorkTimeVo;
import com.kingmed.km2c.organ.entity.NcpOrganWorkdate;
import com.kingmed.km2c.util.CollectionUtils2C;
import com.kingmed.km2c.util.DateUtils2C;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.PasswordUtil;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Data
public class TestPan {
    private String id;
    private String name;
   public static void main(String[] args) throws Exception{
        List<TestPan> testPanList = new ArrayList<>();

        TestPan testPan1 = new TestPan();
        testPan1.setId("1");
        testPan1.setName("555");

        TestPan testPan2 = new TestPan();
        testPan2.setId("2");
        testPan2.setName("66");

        TestPan testPan3 = new TestPan();
        testPan3.setId("3");
        testPan3.setName("77");

        TestPan testPan4 = new TestPan();
        testPan4.setId("2");
        testPan4.setName("99");

        testPanList.add(testPan1);
        testPanList.add(testPan2);
        testPanList.add(testPan3);
        testPanList.add(testPan4);

        long count = testPanList.stream().filter(obj -> obj.getId().equals("1")).count();  // TODO获取ID等于 1 的个数

        int size = testPanList.size();
		//进行对象去重,通过ID字段属性过滤相同的对象数据,返回就是过滤后集合对象 collect 
    List<TestPan> collect = testPanList.stream().filter(CollectionUtilsPan .distinctByKey(TestPan::getId)).collect(Collectors.toList());

        System.out.println(size+"本身集合大小");
        System.out.println("*-***********************************");
        System.out.println(collect.size()+"去重大小");

       System.out.println("获取ID等于 1 的个数为:"+count);
   }
}

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值