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

Java8通过Stream对list对象某个属性去重

文章目录

      • 前言
      • 正文
        • 数据对象
        • 1. 对象整体去重
        • 2. 单个属性去重(只返回属性)
        • 3. 单个属性去重
        • 4. 多个属性去重
        • 验证方法
        • 运行结果
      • 结尾

前言

对于list集合进行去重操作,多种情况进行记录.

只记录在学习与实践过程中需要对集合操作的场景,方便下次能够直接快速的回忆起来.

当然这些方法的使用与操作皆是对于Java8stream操作,TreeSet不重复特性进行的运用.

如果能够更好更快的达成目的,也是不必拘泥于某一些固定的方式去处理数据,以效率与程序开销优先.

正文

数据对象

将数据对象写成静态不可变的list来方便我们对于程序进行调用验证

private static final List<SyncBalance> list = asList(
        new SyncBalance(BigDecimal.ZERO, "12345678", 0, 1, "GGBond", 32, 0),
        new SyncBalance(BigDecimal.ZERO, "22345678", 0, 1, "GGBond", 32, 0),
        new SyncBalance(BigDecimal.ZERO, "22345678", 1, 1, "GGBond", 32, 0),
        new SyncBalance(BigDecimal.ZERO, "22345678", 1, 1, "GGBond", 32, 0)
);
1. 对象整体去重
/**
 * 1. 对所有属性一样的数据进行去重
 */
public static void allColumnDistinct() {
    List<SyncBalance> allColumnDistinct = list.stream().distinct().collect(Collectors.toList());
}
2. 单个属性去重(只返回属性)
/**
 * 2. 对单个属性去重,只返回去重的属性
 */
public static void columnDistinct() {
    List<String> columnDistinct = list.stream().map(SyncBalance::getMobile).distinct().collect(Collectors.toList());
}
3. 单个属性去重
/**
 * 3-1. 对单个属性一样的数据进行去重,下面是对`mobile`去重
 */
public static void oneColumnDistinctMethodOne() {
    List<SyncBalance> oneColumnDistinctMethodOne = list.stream().collect(Collectors.collectingAndThen(
            Collectors.toCollection(() -> new TreeSet<>(
                    Comparator.comparing(SyncBalance::getMobile))), ArrayList::new));
}
/**
 * 3-2. 对单个属性一样的数据进行去重,下面是对`mobile`去重(通过自定义属性去重方法)
 */
public static void oneColumnDistinctMethodTwo() {
    List<SyncBalance> oneColumnDistinctMethodTwo = list.stream().filter(distinctByKey(i -> i.getMobile())).collect(Collectors.toList());
}

/**
* list对象单个个字段去重(自定义属性)
* @param keyExtractor 去重的对象字段
* @return
* @param <T>
*/
public static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
    Map<Object, Boolean> seen = new ConcurrentHashMap<>();
    return t -> seen.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
}
/**
 * 3-3. 对单个属性一样的数据进行去重,下面是对`mobile`去重
 * 利用TreeSet进行去重
 */
public static void oneColumnDistinctMethodThree() {
    TreeSet<SyncBalance> oneColumnDistinctMethodThree = new TreeSet<>(Comparator.comparing(i -> i.getMobile()));
    list.forEach(a -> oneColumnDistinctMethodThree.add(a));
}
4. 多个属性去重
/**
 * 4. 多个字段条件去重
 */
public static void twoColumnDistinct() {
    List<SyncBalance> twoColumnDistinct = list.stream().collect(Collectors.collectingAndThen(
            Collectors.toCollection(() -> new TreeSet<>(
                    Comparator.comparing(p -> p.getMobile() + ";" + p.getStatus()))), ArrayList::new));
}
验证方法

对于验证方法添加了一些分隔的数据

/**
 * 验证的main方法
 * @param args
 */
public static void main(String[] args) {
    allColumnDistinct();
    columnDistinct();
    oneColumnDistinctMethodOne();
    oneColumnDistinctMethodTwo();
    oneColumnDistinctMethodThree();
    twoColumnDistinct();
}
运行结果
allColumnDistinct---v
SyncBalance(accountBalance=0, mobile=12345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
SyncBalance(accountBalance=0, mobile=22345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
SyncBalance(accountBalance=0, mobile=22345678, status=1, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
allColumnDistinct---^
columnDistinct---v
12345678
22345678
columnDistinct---^
oneColumnDistinctMethodOne---v
SyncBalance(accountBalance=0, mobile=12345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
SyncBalance(accountBalance=0, mobile=22345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
oneColumnDistinctMethodOne---^
oneColumnDistinctMethodTwo---v
SyncBalance(accountBalance=0, mobile=12345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
SyncBalance(accountBalance=0, mobile=22345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
oneColumnDistinctMethodTwo---^
oneColumnDistinctMethodThree---v
SyncBalance(accountBalance=0, mobile=12345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
SyncBalance(accountBalance=0, mobile=22345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
oneColumnDistinctMethodThree---^
twoColumnDistinct---v
SyncBalance(accountBalance=0, mobile=12345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
SyncBalance(accountBalance=0, mobile=22345678, status=0, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
SyncBalance(accountBalance=0, mobile=22345678, status=1, sysId=1, loginAccountName=GGBond, keepOrUpdate=32, id=0)
twoColumnDistinct---^

image-20240518151444939

结尾

集合的操作不止拘泥于stream流TreeSet可以处理,其他一下如果可以实现皆可使用.

以上,皆可直接使用.

http://www.dtcms.com/a/94048.html

相关文章:

  • 【计科】从操作系统到虚拟化技术(进程调度,内存映射,设备IO,文件、网络管理)
  • 每日总结3.27
  • linux服务器配置jupyter或python上安装字体
  • 单片机时钟树中RTC和IWDG讲解
  • LeetCode hot 100—零钱兑换
  • Open WebUI自定义OpenWebUI图标
  • 基于springcloud微服务架构的巡游出租管理平台
  • SQL优化 | 精准区分 trace_id、sql_id、plan_id(二)
  • HarmonyOS-ArkUI Navigation (导航组件)-第一部分
  • 【网络丢包】原因排查及优化
  • PTA 7-16 一元多项式求导
  • leetcode1248. 统计「优美子数组」
  • JavaScript获取元素及事件5种方法
  • 软考《信息系统运行管理员》- 5.5 信息系统数据资源的开发与利用
  • CLion配置问题解决
  • UML事务、关系、UML图(高软54)
  • 批量将多个 XPS 文档转换为 PDF 格式
  • AI PPT哪家强?2025年4款高效工具深度测评
  • android-enableJetifier作用
  • Manus智能体具体是指什么
  • 【前端】【面试】前端 Diff 相关考题及答案
  • Unity 编辑器中动画分割/创建动画剪辑
  • 分布式队列(java)
  • UML 图六种箭头含义详解:泛化、实现、依赖、关联、聚合、组合
  • 【力扣hot100题】(005)三数之和
  • CrossNorm与SelfNorm的具体实现
  • 【Python】编程50个经典操作
  • 向量数据库的适用场景与局限性分析
  • R²AIN SUITE 助力医药企业服务管理数智化转型
  • jmeter 镜像构建