栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java 动态 lambda

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java 动态 lambda

最近有需求,需要根据配置文件,

动态的 过滤+聚合 数据

想想就写了动态的lambda,方便使用。

目前只有 filter和group。并且没有测试过性能。

如果大家使用的话,先将就一下,或者自己改改。

一,主要方法类

通过反射,来组装lambda。

主要使用方法:

getFiledValue
getDataListFilter
getDataListGroup
package com.leadtrans.report.common;

import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;




public class ReflectionUtil {

    

    public static Object invokeMethod(Object owner,String methodName,Class[] argsClass,Object[] args) throws Exception{
        Object objRtn=null;
        Class ownerClass = owner.getClass();
        Method method = ownerClass.getMethod(methodName, argsClass);
        objRtn = method.invoke(owner, args);
        return objRtn;
    }

    public static Object getFiledValue(Object obj,String filedName){
        Object objValue=null;
        try {
            Field field = obj.getClass().getDeclaredField(filedName);
            field.setAccessible(true);
            objValue = field.get(obj);
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        finally {
            return objValue;
        }
    }

    
    public static  List getDataListFilter(List list, Map map) {
        Supplier> st = () -> list.stream();
        for (Map.Entry item : map.entrySet()) {
            Supplier> stFilter = st;
            st = () -> stFilter.get().filter(x -> ReflectionUtil.getFiledValue(x, item.getKey()) == item.getValue());
        }
        List rList = st.get().collect(Collectors.toList());
        return rList;
    }

    
    public static   Map> getDataListGroup(List list,List groups){
        Map> map= list.stream().collect(Collectors.groupingBy(x -> {
            String groupItem ="";
            for (String y : groups){
                groupItem+=ReflectionUtil.getFiledValue(x, y)+"_";
            }
            return groupItem;
        }));
        return map;
    }
}

二,测试:

@Test
    public void test() throws NoSuchFieldException, IllegalAccessException {
//filter 条件
        Map map = new HashMap<>();
        map.put("fileType", 1);
        map.put("reportName", "b");
//原数据
        List list = new ArrayList<>() {
            {
                add(new FileTypeVO(1, "a", "aPath"));
                add(new FileTypeVO(1, "b", "bPath"));
                add(new FileTypeVO(1, "b", "cPath"));
                add(new FileTypeVO(4, "c", "dPath"));
            }
        };
//group 字段
        List groups=new ArrayList<>(){{
            add("reportName");
            add("filePath");
        }};

//filter 数据
        List rList = ReflectionUtil.getDataListFilter(list, map);
//group 数据
        Map> rMap=ReflectionUtil.getDataListGroup(rList,groups);
        System.out.println(rList.size());

    }

测试实体:FileTypeVo

package com.leadtrans.report.model;

import io.swagger.annotations.ApiModelProperty;




public class FileTypeVO {
    private int fileType;
    private String reportName;
    private String filePath;
    private String order;

    public FileTypeVO(){}
    public FileTypeVO(int fileType,String reportName,String filePath){
        this.fileType=fileType;
        this.reportName=reportName;
        this.filePath=filePath;
    }

    public FileTypeVO(int fileType,String reportName,String filePath,String order){
        this(fileType,reportName,filePath);
        this.order=order;
    }

    public int getFileType() {
        return fileType;
    }

    public void setFileType(int fileType) {
        this.fileType = fileType;
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }

    public String getReportName() {
        return reportName;
    }

    public void setReportName(String reportName) {
        this.reportName = reportName;
    }

    public String getOrder() {
        return order;
    }

    public void setOrder(String order) {
        this.order = order;
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/666733.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号