java工具--对象比较和对象集合去重(重复的保留一个)

java 复制代码
package com.zhongma.people.car.fitting.tool;

import com.zhongma.people.car.fitting.bean.FittingResult;
import com.zhongma.people.car.fitting.core.BeanStore;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

/**
 * @author konglcwqy
 * @description 对象工具类
 * @date 2023/8/21 10:05
 */
public class BeanTool {

    /**
     * 功能描述:对象属性值比较
     *
     * @param me
     * @param target
     * @param fields
     * @return boolean
     * @author konglcwqy
     * @date 2023/8/21 10:07
     */
    public static <T> boolean equal(T me, T target, String... fields) throws Exception {
        if (fields == null || fields.length == 0) {
            throw new Exception("field is null");
        }
        List<String> list = Arrays.asList(fields);
        Field[] declaredFields = me.getClass().getDeclaredFields();
        if (declaredFields == null || declaredFields.length == 0) {
            return true;
        }
        Object me_value = null;
        Object target_value = null;
        for (Field declaredField : declaredFields) {
            if (list.contains(declaredField.getName())) {
                declaredField.setAccessible(true);
                me_value = declaredField.get(me);
                target_value = declaredField.get(target);
                if (!ObjectUtils.nullSafeEquals(me_value, target_value)) {
                    return false;
                }
            }
        }
        return true;
    }

    /**
     * 功能描述:集合去重
     *
     * @param list
     * @param fields
     * @return java.util.List<T>
     * @author konglcwqy
     * @date 2023/8/21 10:33
     */
    public static <T> List<T> beanListDistinct(List<T> list, String... fields) throws Exception {
        List<T> result = new ArrayList<T>();
        Iterator<T> iterator = null;
        boolean flag = false;
        T me;
        out:
        for (T target : list) {
            iterator = result.iterator();
            inner:
            while (iterator.hasNext()) {
                me = iterator.next();
                if (equal(me, target, fields)) {
                    continue out;
                }
            }
            result.add(target);
        }
        return result;
    }

    public static void main(String[] args) throws Exception {
        FittingResult fittingResult1 = new FittingResult("1", 1, "a", 100, BeanStore.WEAK_BING);
        FittingResult fittingResult2 = new FittingResult("1", 1, "a", 100, BeanStore.WEAK_BING);
        FittingResult fittingResult3 = new FittingResult("1", 1, "a", 100, BeanStore.WEAK_BING);
        FittingResult fittingResult4 = new FittingResult("1", 1, "a", 100, BeanStore.WEAK_BING);

        FittingResult fittingResult5 = new FittingResult("1", 1, "b", 90, BeanStore.WEAK_BING);
        FittingResult fittingResult6 = new FittingResult("1", 1, "b", 90, BeanStore.WEAK_BING);
        FittingResult fittingResult7 = new FittingResult("1", 1, "b", 90, BeanStore.WEAK_BING);
        FittingResult fittingResult8 = new FittingResult("1", 1, "b", 90, BeanStore.WEAK_BING);

        List<FittingResult> list = new ArrayList<FittingResult>();
        list.add(fittingResult1);
        list.add(fittingResult2);
        list.add(fittingResult3);
        list.add(fittingResult4);
        list.add(fittingResult5);
        list.add(fittingResult6);
        list.add(fittingResult7);
        list.add(fittingResult8);

        list = beanListDistinct(list, "eventId", "eventType", "carNum");

        System.out.println(list.size());
    }
}
相关推荐
2401_8574396918 分钟前
SpringBoot框架在资产管理中的应用
java·spring boot·后端
怀旧66619 分钟前
spring boot 项目配置https服务
java·spring boot·后端·学习·个人开发·1024程序员节
李老头探索21 分钟前
Java面试之Java中实现多线程有几种方法
java·开发语言·面试
芒果披萨26 分钟前
Filter和Listener
java·filter
qq_49244844631 分钟前
Java实现App自动化(Appium Demo)
java
阿华的代码王国39 分钟前
【SpringMVC】——Cookie和Session机制
java·后端·spring·cookie·session·会话
找了一圈尾巴1 小时前
前后端交互通用排序策略
java·交互
哎呦没4 小时前
SpringBoot框架下的资产管理自动化
java·spring boot·后端
m0_571957586 小时前
Java | Leetcode Java题解之第543题二叉树的直径
java·leetcode·题解
魔道不误砍柴功8 小时前
Java 中如何巧妙应用 Function 让方法复用性更强
java·开发语言·python