天天看點

【Java開發工具類】集合數組資料抽取工具類

集合數組資料抽取工具類

  • 功能1:抽取對象數組中的某一個字段形成的資料(去重)

1、某一字段資料抽取

描述:從一個Object對象數組中,提取Object對象中A字段形成的數組

反射工具類:

import com.google.common.base.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @description 反射工具類
 * @author: wilsonMeng
 * @create: 2021-01-15 11:06
 **/

@Slf4j
public class ReflectUtils {
    public static final Method[] EMPTY_METHOD_ARRAY = new Method[0];

    public static Method[] getAccessibleMethods(Class<?> clazz) {
        return clazz == null ? EMPTY_METHOD_ARRAY : clazz.getDeclaredMethods();
    }

    //注:Optional引入的不是jdk的包 而是com.google.common.base.Optional
    public static Optional<Method> getAccessibleMethod(final Object obj,
                                                       final String methodName,
                                                       final Class<?>... parameterTypes) {
        for (Class<?> c = obj.getClass(); c != Object.class; c = c.getSuperclass()) {
            try {
                Method method = c.getDeclaredMethod(methodName, parameterTypes);
                method.setAccessible(true);
                return Optional.of(method);
            } catch (NoSuchMethodException e) {
                // Method不在目前類定義,繼續向上轉型
            }
        }
        return Optional.absent();
    }

    public static Optional<Field> getAccessibleField(final Object obj,
                                                     final String fieldName) {
        for (Class<?> c = obj.getClass(); c != Object.class; c = c.getSuperclass()) {
            try {
                Field field = c.getDeclaredField(fieldName);
                field.setAccessible(true);
                return Optional.of(field);
            } catch (NoSuchFieldException e) {
                // Field不在目前類定義,繼續向上轉型
            }
        }
        return Optional.absent();
    }

    public static Optional<Object> getFieldValue(final Object obj, final String fieldName) {
        Optional<Field> field = getAccessibleField(obj, fieldName);

        if (!field.isPresent()) {
            throw new IllegalArgumentException("Could not find field ["
                    + fieldName + "] on target [" + obj + "]");
        }

        try {
            return Optional.fromNullable(field.get().get(obj));
        } catch (IllegalAccessException e) {
            log.error("不可能抛出的異常", e);
        }
        return Optional.absent();
    }

    //與getFieldValue比較, 字段不存在不會抛異常
    public static Optional<Object> safeGetFieldValue(final Object obj, final String fieldName) {
        Optional<Field> field = getAccessibleField(obj, fieldName);

        if (!field.isPresent()) {
            return Optional.absent();
        }

        try {
            return Optional.fromNullable(field.get().get(obj));
        } catch (IllegalAccessException e) {
            log.error("不可能抛出的異常", e);
        }
        return Optional.absent();
    }

    public static void setFieldValue(final Object obj, final String fieldName,
                                     final Object value) {
        Optional<Field> field = getAccessibleField(obj, fieldName);

        if (!field.isPresent()) {
            throw new IllegalArgumentException("Could not find field ["
                    + fieldName + "] on target [" + obj + "]");
        }

        try {
            field.get().set(obj, value);
        } catch (IllegalAccessException e) {
            log.error("不可能抛出的異常", e);
        }
    }

    //與setFieldValue比較, 字段不存在不會抛異常
    public static void safeSetFieldValue(final Object obj, final String fieldName,
                                         final Object value) {
        Optional<Field> field = getAccessibleField(obj, fieldName);

        if (!field.isPresent()) {
            return;
        }

        try {
            field.get().set(obj, value);
        } catch (IllegalAccessException e) {
            log.error("不可能抛出的異常", e);
        }
    }

    public static Object invokeMethod(final Object obj,
                                      final String methodName,
                                      final Class<?>[] parameterTypes,
                                      final Object[] args) {
        Optional<Method> method = getAccessibleMethod(obj, methodName, parameterTypes);
        if (!method.isPresent()) {
            throw new IllegalArgumentException("Could not find method ["
                    + methodName + "] on target [" + obj + "]");
        }

        try {
            return method.get().invoke(obj, args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Object invokeGetter(Object obj, String propertyName) {
        String getterMethodName = "get" + StringUtils.capitalize(propertyName);
        return invokeMethod(obj, getterMethodName, new Class[] {},
                new Object[] {});
    }

    public static void invokeSetter(Object obj, String propertyName, Object value) {
        invokeSetter(obj, propertyName, value, null);
    }

    public static void invokeSetter(Object obj, String propertyName,
                                    Object value, Class<?> propertyType) {
        Class<?> type = propertyType != null ? propertyType : value.getClass();
        String setterMethodName = "set" + StringUtils.capitalize(propertyName);
        invokeMethod(obj, setterMethodName, new Class[] { type }, new Object[] { value });
    }

    public static Object getFieldValue(Object object, Field field)
    {
        try {
            field.setAccessible(true);// 設定些屬性是可以通路的
            return field.get(object);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

           

資料抽取工具類:

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.common.utils.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @description 資料抽取工具類
 * @author: wilsonMeng
 * @create: 2021-01-15 11:03
 **/

@Slf4j
public class ExtractUtils {
    /**
     * 從複雜結構清單中抽取出指定字段的清單
     *
     * @param list      源資料清單
     * @param fieldName 欲抽取字段名
     * @param clz       抽取字段類型
     * @return 去重結果
     */
    public static <T, R> List<R> extractField(List<T> list, String fieldName, Class<R> clz) {
        if (CollectionUtils.isEmpty(list) || StringUtils.isBlank(fieldName)) {
            return Collections.emptyList();
        }

        Function<T, R> func = (item) -> {
            Object obj = null;
            try {
//                注:ReflectUtils工具類是自定義的工具類
                obj = ReflectUtils.invokeGetter(item, fieldName);
            } catch (Exception e) {
                log.info("extractField list:{}, fieldName:{}, type:{} error->", list, fieldName, clz, e);
//                可抛出自定義業務異常
//                throw new SysException("反射處理異常");
                throw e;
            }
            return (R) obj;
        };

        return extractField(list, func);
    }

    public static <T, R> List<R> extractField(List<T> list, Function<T, R> func) {
        if (CollectionUtils.isEmpty(list)) {
            return Collections.emptyList();
        }
        return list.stream().map(func).filter(Objects::nonNull).distinct().collect(Collectors.toList());
    }

}

           

功能測試

PayOrderReqVo對象數組,提取其中字段orderNo形成的數組。

PayOrderReqVo結構:

public class PayOrderReqVo extends TradeReqVo {

    private static final long serialVersionUID = -6973527623323791153L;

    /**
     * 訂單号
     */
    private String orderNo;

    /**
     * 支付工具
     */
    private String payTool;

    /**
     * 支付用戶端
     */
    private String clientCode;

    private String payIp;

    /**
     * 用于擷取微信的openId
     */
    private String accessToken;

    /**
     * 用于擷取微信的openId
     */
    private String platform;

}
           

測試方法:

public static void main(String[] args) {
        List<PayOrderReqVo> list = new ArrayList<>();
        PayOrderReqVo reqVo = new PayOrderReqVo();
        reqVo.setOrderNo("12315");
        reqVo.setPayTool("" + 3);
        reqVo.setClientCode("");
        reqVo.setPayIp("" + Math.random());
        reqVo.setAccessToken("token");
        reqVo.setPlatform("");
        list.add(reqVo);

        for (int i = 0; i < 30; i++) {
            PayOrderReqVo req = new PayOrderReqVo();
            req.setOrderNo(i+"" + System.currentTimeMillis());
            req.setPayTool("" + i);
            req.setClientCode("");
            req.setPayIp("" + Math.random());
            req.setAccessToken("token");
            req.setPlatform("");
            list.add(req);
        }

        //為了測試相同orderNo字段轉出來的結果(是否會去重)
        PayOrderReqVo vo = new PayOrderReqVo();
        vo.setOrderNo("12315");
        vo.setPayTool("" + 5);
        vo.setClientCode("");
        vo.setPayIp("" + Math.random());
        vo.setAccessToken("token");
        vo.setPlatform("");
        list.add(vo);
        System.out.println("資料轉換前數組大小" + list.size() + ",轉換前數組資料list:" + JSON.toJSONString(list));

        List<String> orderNoList = ExtractUtils.extractField(list, "orderNo", String.class);
        System.out.println("list過濾後的得到的orderNoList:" + JSON.toJSONString(orderNoList) + "。orderNoList數組大小" + orderNoList.size());
    }
           

輸出結果:

資料轉換前數組大小32,轉換前數組資料list:[{"accessToken":"token","clientCode":"","orderNo":"12315","payIp":"0.7319353274805084","payTool":"3","platform":""},{"accessToken":"token","clientCode":"","orderNo":"01610692569861","payIp":"0.012545651716888484","payTool":"0","platform":""},{"accessToken":"token","clientCode":"","orderNo":"11610692569861","payIp":"0.26339676428871783","payTool":"1","platform":""},{"accessToken":"token","clientCode":"","orderNo":"21610692569861","payIp":"0.5202475284694749","payTool":"2","platform":""},{"accessToken":"token","clientCode":"","orderNo":"31610692569861","payIp":"0.7144883769668229","payTool":"3","platform":""},{"accessToken":"token","clientCode":"","orderNo":"41610692569861","payIp":"0.5146063920932031","payTool":"4","platform":""},{"accessToken":"token","clientCode":"","orderNo":"51610692569861","payIp":"0.46032932326829046","payTool":"5","platform":""},{"accessToken":"token","clientCode":"","orderNo":"61610692569861","payIp":"0.931770152667655","payTool":"6","platform":""},{"accessToken":"token","clientCode":"","orderNo":"71610692569861","payIp":"0.8921508911769656","payTool":"7","platform":""},{"accessToken":"token","clientCode":"","orderNo":"81610692569861","payIp":"0.21276393722149833","payTool":"8","platform":""},{"accessToken":"token","clientCode":"","orderNo":"91610692569861","payIp":"0.7451690440110974","payTool":"9","platform":""},{"accessToken":"token","clientCode":"","orderNo":"101610692569861","payIp":"0.2924706209466982","payTool":"10","platform":""},{"accessToken":"token","clientCode":"","orderNo":"111610692569861","payIp":"0.6749044172214175","payTool":"11","platform":""},{"accessToken":"token","clientCode":"","orderNo":"121610692569861","payIp":"0.5450882391908459","payTool":"12","platform":""},{"accessToken":"token","clientCode":"","orderNo":"131610692569861","payIp":"0.4383017987249407","payTool":"13","platform":""},{"accessToken":"token","clientCode":"","orderNo":"141610692569861","payIp":"0.8643864125838964","payTool":"14","platform":""},{"accessToken":"token","clientCode":"","orderNo":"151610692569861","payIp":"0.40798415922817355","payTool":"15","platform":""},{"accessToken":"token","clientCode":"","orderNo":"161610692569861","payIp":"0.39064480800985046","payTool":"16","platform":""},{"accessToken":"token","clientCode":"","orderNo":"171610692569861","payIp":"0.08789025176777698","payTool":"17","platform":""},{"accessToken":"token","clientCode":"","orderNo":"181610692569861","payIp":"0.2688411932903757","payTool":"18","platform":""},{"accessToken":"token","clientCode":"","orderNo":"191610692569861","payIp":"0.0026810412563650354","payTool":"19","platform":""},{"accessToken":"token","clientCode":"","orderNo":"201610692569861","payIp":"0.3448160063484018","payTool":"20","platform":""},{"accessToken":"token","clientCode":"","orderNo":"211610692569861","payIp":"0.8885717741918573","payTool":"21","platform":""},{"accessToken":"token","clientCode":"","orderNo":"221610692569861","payIp":"0.5037519283274727","payTool":"22","platform":""},{"accessToken":"token","clientCode":"","orderNo":"231610692569861","payIp":"0.7471909625533617","payTool":"23","platform":""},{"accessToken":"token","clientCode":"","orderNo":"241610692569862","payIp":"0.8199711574191122","payTool":"24","platform":""},{"accessToken":"token","clientCode":"","orderNo":"251610692569862","payIp":"0.6548524669456443","payTool":"25","platform":""},{"accessToken":"token","clientCode":"","orderNo":"261610692569862","payIp":"0.225997276583776","payTool":"26","platform":""},{"accessToken":"token","clientCode":"","orderNo":"271610692569862","payIp":"0.5060376445334694","payTool":"27","platform":""},{"accessToken":"token","clientCode":"","orderNo":"281610692569862","payIp":"0.6949046818946648","payTool":"28","platform":""},{"accessToken":"token","clientCode":"","orderNo":"291610692569862","payIp":"0.2613369799754708","payTool":"29","platform":""},{"accessToken":"token","clientCode":"","orderNo":"12315","payIp":"0.15166112979283997","payTool":"5","platform":""}]
list過濾後的得到的orderNoList:["12315","01610692569861","11610692569861","21610692569861","31610692569861","41610692569861","51610692569861","61610692569861","71610692569861","81610692569861","91610692569861","101610692569861","111610692569861","121610692569861","131610692569861","141610692569861","151610692569861","161610692569861","171610692569861","181610692569861","191610692569861","201610692569861","211610692569861","221610692569861","231610692569861","241610692569862","251610692569862","261610692569862","271610692569862","281610692569862","291610692569862"]。orderNoList數組大小31
           

2、對象數組集合抽取為Map結構

轉換工具類:

import com.google.common.base.Optional;
import org.apache.dubbo.common.utils.CollectionUtils;

import java.util.*;

/**
 * @description
 * @author: wilsonMeng
 * @create: 2021-01-15 14:47
 **/


public class ConvMapUtils {
    /**
     * 将清單轉化成Map. 相同key會被覆寫
     *
     * @param beanList
     * @param fieldName
     * @return
     */
    public static <K, T> Map<K, T> toMap(List<T> beanList, String fieldName) {
        if (CollectionUtils.isEmpty(beanList)) {
            return Collections.emptyMap();
        }
        Map<K, T> map = new HashMap();
        for (T bean : beanList) {
            Optional keyOptional = ReflectUtils.getFieldValue(bean, fieldName);
            K key = keyOptional.isPresent() ? (K) keyOptional.get() : null;
            map.put(key, bean);
        }

        return map;
    }

    public static <K, T> Map<K, List<T>> toListMap(List<T> beanList, String fieldName) {
        if (CollectionUtils.isEmpty(beanList)) {
            return Collections.emptyMap();
        }

        Map<K, List<T>> map = new HashMap<>();
        for (T bean : beanList) {
            com.google.common.base.Optional keyOptional = ReflectUtils.getFieldValue(bean, fieldName);
            K key = keyOptional.isPresent() ? (K) keyOptional.get() : null;

            if (key != null) {
                map.computeIfAbsent(key, k -> new ArrayList<>()).add(bean);
            }
        }
        return map;
    }
}
           

描述:将List對象轉換為Map結構,key為Object中的字段,value為該Object

測試用例:

public static void main(String[] args) {
        List<PayOrderReqVo> list = new ArrayList<>();
        PayOrderReqVo reqVo = new PayOrderReqVo();
        reqVo.setOrderNo("12315");
        reqVo.setPayTool("" + 3);
        reqVo.setClientCode("");
        reqVo.setPayIp("" + Math.random());
        reqVo.setAccessToken("token");
        reqVo.setPlatform("");
        list.add(reqVo);

        for (int i = 0; i < 30; i++) {
            PayOrderReqVo req = new PayOrderReqVo();
            req.setOrderNo(i + "" + System.currentTimeMillis());
            req.setPayTool("" + i);
            req.setClientCode("");
            req.setPayIp("" + Math.random());
            req.setAccessToken("token");
            req.setPlatform("");
            list.add(req);
        }

        //為了測試相同orderNo字段轉出來的結果(是否會去重)
        PayOrderReqVo vo = new PayOrderReqVo();
        vo.setOrderNo("12315");
        vo.setPayTool("" + 5);
        vo.setClientCode("");
        vo.setPayIp("" + Math.random());
        vo.setAccessToken("token");
        vo.setPlatform("");
        list.add(vo);
        System.out.println("資料轉換前數組大小" + list.size() + ",轉換前數組資料list:" + JSON.toJSONString(list));

        System.out.println();

        //key為對象字段的資料類型
        Map<String, PayOrderReqVo> map = ConvMapUtils.toMap(list, "orderNo");
        System.out.println("轉換後的map結構map:" + JSON.toJSONString(map));
        System.out.println("轉換後的map結構map大小:" + map.size());
    }
           

測試結果:

資料轉換前數組大小32,轉換前數組資料list:[{"accessToken":"token","clientCode":"","orderNo":"12315","payIp":"0.6428154611022263","payTool":"3","platform":""},{"accessToken":"token","clientCode":"","orderNo":"01610693765877","payIp":"0.3886283964329481","payTool":"0","platform":""},{"accessToken":"token","clientCode":"","orderNo":"11610693765877","payIp":"0.7457176812020152","payTool":"1","platform":""},{"accessToken":"token","clientCode":"","orderNo":"21610693765877","payIp":"0.36209548593693286","payTool":"2","platform":""},{"accessToken":"token","clientCode":"","orderNo":"31610693765877","payIp":"0.15479352381376787","payTool":"3","platform":""},{"accessToken":"token","clientCode":"","orderNo":"41610693765877","payIp":"0.5874754699194571","payTool":"4","platform":""},{"accessToken":"token","clientCode":"","orderNo":"51610693765877","payIp":"0.26849878762200896","payTool":"5","platform":""},{"accessToken":"token","clientCode":"","orderNo":"61610693765877","payIp":"0.30258211553700665","payTool":"6","platform":""},{"accessToken":"token","clientCode":"","orderNo":"71610693765877","payIp":"0.06467825114338466","payTool":"7","platform":""},{"accessToken":"token","clientCode":"","orderNo":"81610693765877","payIp":"0.6181569581276251","payTool":"8","platform":""},{"accessToken":"token","clientCode":"","orderNo":"91610693765877","payIp":"0.93164380854642","payTool":"9","platform":""},{"accessToken":"token","clientCode":"","orderNo":"101610693765878","payIp":"0.0654513851999543","payTool":"10","platform":""},{"accessToken":"token","clientCode":"","orderNo":"111610693765878","payIp":"0.05849044690148275","payTool":"11","platform":""},{"accessToken":"token","clientCode":"","orderNo":"121610693765878","payIp":"0.6067538835801044","payTool":"12","platform":""},{"accessToken":"token","clientCode":"","orderNo":"131610693765878","payIp":"0.4179031422194368","payTool":"13","platform":""},{"accessToken":"token","clientCode":"","orderNo":"141610693765878","payIp":"0.227986104905615","payTool":"14","platform":""},{"accessToken":"token","clientCode":"","orderNo":"151610693765878","payIp":"0.3061080413554135","payTool":"15","platform":""},{"accessToken":"token","clientCode":"","orderNo":"161610693765878","payIp":"0.41428843261410453","payTool":"16","platform":""},{"accessToken":"token","clientCode":"","orderNo":"171610693765878","payIp":"0.8936994647235474","payTool":"17","platform":""},{"accessToken":"token","clientCode":"","orderNo":"181610693765878","payIp":"0.20377628948406068","payTool":"18","platform":""},{"accessToken":"token","clientCode":"","orderNo":"191610693765878","payIp":"0.9450580674341462","payTool":"19","platform":""},{"accessToken":"token","clientCode":"","orderNo":"201610693765878","payIp":"0.6301609744744059","payTool":"20","platform":""},{"accessToken":"token","clientCode":"","orderNo":"211610693765878","payIp":"0.7058147394428811","payTool":"21","platform":""},{"accessToken":"token","clientCode":"","orderNo":"221610693765878","payIp":"0.6011086013081506","payTool":"22","platform":""},{"accessToken":"token","clientCode":"","orderNo":"231610693765878","payIp":"0.37578749108265386","payTool":"23","platform":""},{"accessToken":"token","clientCode":"","orderNo":"241610693765878","payIp":"0.15609521735838394","payTool":"24","platform":""},{"accessToken":"token","clientCode":"","orderNo":"251610693765878","payIp":"0.7831258898383072","payTool":"25","platform":""},{"accessToken":"token","clientCode":"","orderNo":"261610693765878","payIp":"0.5357709467784529","payTool":"26","platform":""},{"accessToken":"token","clientCode":"","orderNo":"271610693765878","payIp":"0.0737652327587559","payTool":"27","platform":""},{"accessToken":"token","clientCode":"","orderNo":"281610693765878","payIp":"0.1457592808900413","payTool":"28","platform":""},{"accessToken":"token","clientCode":"","orderNo":"291610693765878","payIp":"0.9140736449087868","payTool":"29","platform":""},{"accessToken":"token","clientCode":"","orderNo":"12315","payIp":"0.7733032236265234","payTool":"5","platform":""}]

轉換後的map結構map:{"91610693765877":{"accessToken":"token","clientCode":"","orderNo":"91610693765877","payIp":"0.93164380854642","payTool":"9","platform":""},"61610693765877":{"accessToken":"token","clientCode":"","orderNo":"61610693765877","payIp":"0.30258211553700665","payTool":"6","platform":""},"51610693765877":{"accessToken":"token","clientCode":"","orderNo":"51610693765877","payIp":"0.26849878762200896","payTool":"5","platform":""},"71610693765877":{"accessToken":"token","clientCode":"","orderNo":"71610693765877","payIp":"0.06467825114338466","payTool":"7","platform":""},"221610693765878":{"accessToken":"token","clientCode":"","orderNo":"221610693765878","payIp":"0.6011086013081506","payTool":"22","platform":""},"81610693765877":{"accessToken":"token","clientCode":"","orderNo":"81610693765877","payIp":"0.6181569581276251","payTool":"8","platform":""},"181610693765878":{"accessToken":"token","clientCode":"","orderNo":"181610693765878","payIp":"0.20377628948406068","payTool":"18","platform":""},"191610693765878":{"accessToken":"token","clientCode":"","orderNo":"191610693765878","payIp":"0.9450580674341462","payTool":"19","platform":""},"171610693765878":{"accessToken":"token","clientCode":"","orderNo":"171610693765878","payIp":"0.8936994647235474","payTool":"17","platform":""},"211610693765878":{"accessToken":"token","clientCode":"","orderNo":"211610693765878","payIp":"0.7058147394428811","payTool":"21","platform":""},"151610693765878":{"accessToken":"token","clientCode":"","orderNo":"151610693765878","payIp":"0.3061080413554135","payTool":"15","platform":""},"201610693765878":{"accessToken":"token","clientCode":"","orderNo":"201610693765878","payIp":"0.6301609744744059","payTool":"20","platform":""},"161610693765878":{"accessToken":"token","clientCode":"","orderNo":"161610693765878","payIp":"0.41428843261410453","payTool":"16","platform":""},"12315":{"accessToken":"token","clientCode":"","orderNo":"12315","payIp":"0.7733032236265234","payTool":"5","platform":""},"111610693765878":{"accessToken":"token","clientCode":"","orderNo":"111610693765878","payIp":"0.05849044690148275","payTool":"11","platform":""},"121610693765878":{"accessToken":"token","clientCode":"","orderNo":"121610693765878","payIp":"0.6067538835801044","payTool":"12","platform":""},"141610693765878":{"accessToken":"token","clientCode":"","orderNo":"141610693765878","payIp":"0.227986104905615","payTool":"14","platform":""},"131610693765878":{"accessToken":"token","clientCode":"","orderNo":"131610693765878","payIp":"0.4179031422194368","payTool":"13","platform":""},"231610693765878":{"accessToken":"token","clientCode":"","orderNo":"231610693765878","payIp":"0.37578749108265386","payTool":"23","platform":""},"241610693765878":{"accessToken":"token","clientCode":"","orderNo":"241610693765878","payIp":"0.15609521735838394","payTool":"24","platform":""},"261610693765878":{"accessToken":"token","clientCode":"","orderNo":"261610693765878","payIp":"0.5357709467784529","payTool":"26","platform":""},"01610693765877":{"accessToken":"token","clientCode":"","orderNo":"01610693765877","payIp":"0.3886283964329481","payTool":"0","platform":""},"11610693765877":{"accessToken":"token","clientCode":"","orderNo":"11610693765877","payIp":"0.7457176812020152","payTool":"1","platform":""},"251610693765878":{"accessToken":"token","clientCode":"","orderNo":"251610693765878","payIp":"0.7831258898383072","payTool":"25","platform":""},"41610693765877":{"accessToken":"token","clientCode":"","orderNo":"41610693765877","payIp":"0.5874754699194571","payTool":"4","platform":""},"291610693765878":{"accessToken":"token","clientCode":"","orderNo":"291610693765878","payIp":"0.9140736449087868","payTool":"29","platform":""},"21610693765877":{"accessToken":"token","clientCode":"","orderNo":"21610693765877","payIp":"0.36209548593693286","payTool":"2","platform":""},"101610693765878":{"accessToken":"token","clientCode":"","orderNo":"101610693765878","payIp":"0.0654513851999543","payTool":"10","platform":""},"271610693765878":{"accessToken":"token","clientCode":"","orderNo":"271610693765878","payIp":"0.0737652327587559","payTool":"27","platform":""},"281610693765878":{"accessToken":"token","clientCode":"","orderNo":"281610693765878","payIp":"0.1457592808900413","payTool":"28","platform":""},"31610693765877":{"accessToken":"token","clientCode":"","orderNo":"31610693765877","payIp":"0.15479352381376787","payTool":"3","platform":""}}
轉換後的map結構map大小:31
           

3、對象數組集合抽取為Map結構2

描述:将List對象轉換為Map結構,根據Object中字段A的值進行分組,key為字段A的值,value為List對象

測試方法:

public static void main(String[] args) {
        List<PayOrderReqVo> list = new ArrayList<>();
        PayOrderReqVo reqVo = new PayOrderReqVo();
        reqVo.setOrderNo("12315");
        reqVo.setPayTool("" + 3);
        reqVo.setClientCode("");
        reqVo.setPayIp("" + Math.random());
        reqVo.setAccessToken("token");
        reqVo.setPlatform("");
        list.add(reqVo);

        PayOrderReqVo reqVo1 = new PayOrderReqVo();
        reqVo1.setOrderNo("12315");
        reqVo1.setPayTool("tool1");
        reqVo1.setClientCode("code1");
        reqVo1.setPayIp("payIp1");
        reqVo1.setAccessToken("token1");
        reqVo1.setPlatform("platform1");
        list.add(reqVo1);

        PayOrderReqVo reqVo2 = new PayOrderReqVo();
        reqVo2.setOrderNo("2234");
        reqVo2.setPayTool("tool2");
        reqVo2.setClientCode("code2");
        reqVo2.setPayIp("payIp2");
        reqVo2.setAccessToken("token2");
        reqVo2.setPlatform("platform2");
        list.add(reqVo2);

        PayOrderReqVo reqVo3 = new PayOrderReqVo();
        reqVo3.setOrderNo("4321");
        reqVo3.setPayTool("tool3");
        reqVo3.setClientCode("code3");
        reqVo3.setPayIp("payIp3");
        reqVo3.setAccessToken("token3");
        reqVo3.setPlatform("platform3");
        list.add(reqVo3);

        PayOrderReqVo reqVo4 = new PayOrderReqVo();
        reqVo4.setOrderNo("4321");
        reqVo4.setPayTool("tool4");
        reqVo4.setClientCode("code4");
        reqVo4.setPayIp("payIp4");
        reqVo4.setAccessToken("token4");
        reqVo4.setPlatform("platform4");
        list.add(reqVo4);

        System.out.println("資料轉換前數組大小" + list.size() + ",轉換前數組資料list:" + JSON.toJSONString(list));

        System.out.println();

        //key為對象字段的資料類型
        Map<String, List<PayOrderReqVo>> map = ConvMapUtils.toListMap(list, "orderNo");
        System.out.println("轉換後的map結構map:" + JSON.toJSONString(map));
        System.out.println("轉換後的map結構map大小:" + map.size());
    }

           

測試結果:

資料轉換前數組大小5,轉換前數組資料list:[{"accessToken":"token","clientCode":"","orderNo":"12315","payIp":"0.5397354360602487","payTool":"3","platform":""},{"accessToken":"token1","clientCode":"code1","orderNo":"12315","payIp":"payIp1","payTool":"tool1","platform":"platform1"},{"accessToken":"token2","clientCode":"code2","orderNo":"2234","payIp":"payIp2","payTool":"tool2","platform":"platform2"},{"accessToken":"token3","clientCode":"code3","orderNo":"4321","payIp":"payIp3","payTool":"tool3","platform":"platform3"},{"accessToken":"token4","clientCode":"code4","orderNo":"4321","payIp":"payIp4","payTool":"tool4","platform":"platform4"}]

轉換後的map結構map:{"4321":[{"accessToken":"token3","clientCode":"code3","orderNo":"4321","payIp":"payIp3","payTool":"tool3","platform":"platform3"},{"accessToken":"token4","clientCode":"code4","orderNo":"4321","payIp":"payIp4","payTool":"tool4","platform":"platform4"}],"2234":[{"accessToken":"token2","clientCode":"code2","orderNo":"2234","payIp":"payIp2","payTool":"tool2","platform":"platform2"}],"12315":[{"accessToken":"token","clientCode":"","orderNo":"12315","payIp":"0.5397354360602487","payTool":"3","platform":""},{"accessToken":"token1","clientCode":"code1","orderNo":"12315","payIp":"payIp1","payTool":"tool1","platform":"platform1"}]}
轉換後的map結構map大小:3