Java 高效率 List集合擴展工具類

package util;

import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 集合擴展工具類
 *
 * @author: admin
 */
public class CollectionUtil extends CollectionUtils {

    /**
     * 返回集合屬性List集合
     */
    public static <T, U> List<U> convertList(List<T> from, Function<T, U> func) {
        return from.stream().map(func).collect(Collectors.toList());
    }

    /**
     * 返回集合屬性Set集合
     */
    public static <T, U> Set<U> convertSet(List<T> from, Function<T, U> func) {
        return from.stream().map(func).collect(Collectors.toSet());
    }

    /**
     * 返回 同一對象(key->屬性 value->屬性)map
     */
    public static <T, K, V> Map<K, V> convertMap(List<T> from, Function<T, K> keyFunc, Function<T, V> valueFunc) {
        return from.stream().collect(Collectors.toMap(keyFunc, valueFunc));
    }

    /**
     * 返回 同一對象(key->屬性 value->對象)map
     */
    public static <T, K> Map<K, T> convertMap(List<T> from, Function<T, K> keyFunc) {
        return from.stream().collect(Collectors.toMap(keyFunc, item -> item));
    }

    /**
     * 返回 分組(key->屬性 value->屬性)map
     */
    public static <T, K> Map<K, List<T>> convertGroupMap(List<T> from, Function<T, K> keyFunc) {
        return from.stream().collect(Collectors.groupingBy(keyFunc));
    }

    /**
     * fromList<E>->targetList<T>
     */
    public static <E, T> List<T> toTargetObject(Class<T> t, List<E> fromList) {
        List<T> list = Lists.newArrayList();
        fromList.forEach(s -> {
            T to = null;
            if(s instanceof Map){
                try {
                    to=transMapToBean(t,(Map)s);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else{
                try {
                    to = t.getDeclaredConstructor().newInstance();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                BeanUtils.copyProperties(s, to);
            }
            list.add(to);
        });
        return list;
    }

    /**
     * 功能描述: map轉bean
     *
     * @author: admin
     */
    public static <T> T transMapToBean(Class<T> type, @SuppressWarnings("rawtypes") Map map) throws Exception {
        T obj;
        /** 創建 JavaBean 對象*/
        obj = type.getDeclaredConstructor().newInstance();
        /** 獲取類屬性*/
        BeanInfo beanInfo = Introspector.getBeanInfo(type);
        // 給 JavaBean 對象的屬性賦值
        PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor descriptor : propertyDescriptors) {
            String propertyName = descriptor.getName();
            if (map.containsKey(propertyName)) {
                // 下面一句可以 try 起來,這樣當一個屬性賦值失敗的時候就不會影響其他屬性賦值。
                Object value = map.get(propertyName);
                Object[] args = new Object[1];
                args[0] = value;
                descriptor.getWriteMethod().invoke(obj, args);
            }
        }
        return obj;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章