Java数据处理工具类

2019-09-29  本文已影响0人  firefly_
    /**
     * 按指定长度分隔list
     *
     * @param list
     * @param len
     * @param <T>
     * @return 
     */
    public static <T> List<List<T>> splitList(List<T> list, int len) {
        if (list == null || list.size() == 0 || len < 1) {
            return null;
        }
        List<List<T>> result = new ArrayList<>();
        int size = list.size();
        int count = (size + len - 1) / len;
        for (int i = 0; i < count; i++) {
            List<T> subList = list.subList(i * len, ((i + 1) * len > size ? size : len * (i + 1)));
            result.add(subList);
        }
        return result;
    }

     /**
     * 按关键字去重
     *
     * @param keyExtractor
     * @param <T>
     * @return
     */
    public static <T> Predicate<T> distinctByKey(Function<? super T, Object> keyExtractor) {
        Map<Object, Boolean> map = new ConcurrentHashMap<>();
        return t -> map.putIfAbsent(keyExtractor.apply(t), Boolean.TRUE) == null;
    }

    public static <T> Map<String, Object> beanToMap(T bean) {
        Map<String, Object> map = Maps.newHashMap();
        if (bean != null) {
            BeanMap beanMap = BeanMap.create(bean);
            for (Object key : beanMap.keySet()) {
                map.put(String.valueOf(key).toLowerCase(), beanMap.get(key));
            }
        }
        return map;
    }

    public static Map<String, Object>[] objectDiffResult(Object oldModel, Object newModel) {
        Map oldMap = new BeanMap(oldModel);
        Map newMap = new BeanMap(newModel);
        Map<String, Object> newResult = new HashMap();
        Map<String, Object> oldResult = new HashMap();
        newMap.keySet().forEach((k) -> {
            Object newValue = newMap.get(k);
            Object oldValue = oldMap.get(k);
            if (newValue != null && !Objects.equals(newValue, oldValue)) {
                if (newValue instanceof BigDecimal && oldValue instanceof BigDecimal && ((BigDecimal)newValue).doubleValue() == ((BigDecimal)oldValue).doubleValue()) {
                    return;
                }
                oldResult.put(k.toString(), oldValue);
                newResult.put(k.toString(), newValue);
            }
        });
        return new Map[]{oldResult, newResult};
    }

    public static String objectDiffResultToString(Object oldModel, Object newModel) {
        Map<String, Object>[] maps = objectDiffResult(newModel, oldValue);
        if (maps.length == 2) {
            Map<String, Object> oldValueMap = maps[0];
            Map<String, Object> newValueMap = maps[1];
            return JSON.toJSONString(oldValueMap) + " -> " + JSON.toJSONString(newValueMap);
        } else {
            return "";
        }
    }


    /**
     * map安全排序
     *
     * @param map
     * @param predicate
     * @param comparator
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> safeSortMap(Map<K, V> map,
                                               Predicate<? super Map.Entry<K, V>> predicate,
                                               Comparator<? super Map.Entry<K, V>> comparator) {
        return map.entrySet().stream()
            .filter(predicate)
            .sorted(comparator)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
    }

    /**
     * map排序
     *
     * @param map
     * @param comparator
     * @param <K>
     * @param <V>
     * @return
     */
    public static <K, V> Map<K, V> sortMap(Map<K, V> map, Comparator<? super Map.Entry<K, V>> comparator) {
        return map.entrySet().stream()
            .sorted(comparator)
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (v1, v2) -> v1, LinkedHashMap::new));
    }

    /**
     * list 深拷贝
     * @param src
     * @param <T>
     * @return
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        ObjectOutputStream out = new ObjectOutputStream(byteOut);
        out.writeObject(src);
        ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
        ObjectInputStream in = new ObjectInputStream(byteIn);
        @SuppressWarnings("unchecked")
        List<T> dest = (List<T>) in.readObject();
        if (outputStream!=null) {
            outputStream.close();
        }
        if (inputStream!=null) {
            inputStream.close();
        }
        return dest;
    }

public class MyBeanUtils extends BeanUtils {

  public static <S, T> List<T> copyList(List<S> sources, Supplier<T> target) {
    return copyListProperties(sources, target, null);
  }

  public static <S, T> List<T> copyList(List<S> sources, Supplier<T> target,
      BiConsumer<S, T> biConsumer, String... ignorePropertyNames) {
    List<T> list = new ArrayList<>(sources.size());
    for (S source : sources) {
      T t = target.get();
      copyProperties(source, t, ignorePropertyNames);
      list.add(t);
      if (biConsumer != null) {
        biConsumer.accept(source, t);
      }
    }
    return list;
  }

}
上一篇 下一篇

猜你喜欢

热点阅读