zoukankan      html  css  js  c++  java
  • LambdaUtil

    package com.woaizhangjunyang.985211.util;
    
    import com.google.common.base.Splitter;
    import com.google.common.collect.Lists;
    import com.google.common.collect.Maps;
    import com.google.common.collect.Sets;
    import org.apache.commons.collections.CollectionUtils;
    import org.apache.commons.lang3.math.NumberUtils;
    
    import java.util.*;
    import java.util.function.BiFunction;
    import java.util.function.Function;
    import java.util.function.Predicate;
    import java.util.stream.Collectors;
    import java.util.stream.Stream;
    
    /**
     * lambda转换工具转实体类 list等
     */
    public class LambdaUtil
    {
      public static List<Integer> str2ints(String str, String splitChar) {
    /*  31 */     return (List)str2numbersOfList(str, splitChar, NumberUtils::toInt);
      }
    
      
      public static List<Long> str2longs(String str, String splitChar) {
    /*  36 */     return (List)str2numbersOfList(str, splitChar, NumberUtils::toLong);
      }
    
      
      public static Collection<Long> str2longsOfSet(String str, String splitChar) {
    /*  41 */     return (Collection)str2numbersOfSet(str, splitChar, NumberUtils::toLong);
      }
      
      private static List<? extends Number> str2numbersOfList(String str, String splitChar, Function<String, ? extends Number> f) {
    /*  45 */     if (str == null || str.length() == 0) {
    /*  46 */       return Lists.newArrayList();
        }
    /*  48 */     List<String> strings = splitToList(splitChar, str);
    /*  49 */     if (strings.size() > 0) {
    /*  50 */       return (List<? extends Number>)strings.stream().<Number>map(f).collect(Collectors.toList());
        }
    /*  52 */     return Lists.newArrayList();
      }
      
      private static Collection<? extends Number> str2numbersOfSet(String str, String splitChar, Function<String, ? extends Number> f) {
    /*  56 */     if (str == null || str.length() == 0) {
    /*  57 */       return Sets.newLinkedHashSet();
        }
    /*  59 */     List<String> strings = splitToList(splitChar, str);
    /*  60 */     if (strings.size() > 0) {
    /*  61 */       return (Collection<? extends Number>)strings.stream().<Number>map(f).collect(Collectors.toCollection(java.util.LinkedHashSet::new));
        }
    /*  63 */     return Sets.newLinkedHashSet();
      }
    
      
      public static <T> List<T> filterToList(Collection<T> from, Predicate<T> predicate) {
    /*  68 */     if (CollectionUtils.isEmpty(from)) {
    /*  69 */       return Lists.newArrayList();
        }
    /*  71 */     return (List<T>)from.stream().filter(predicate).collect(Collectors.toList());
      }
    
      
      public static <T> Set<T> filterToSet(Collection<T> from, Predicate<T> predicate) {
    /*  76 */     if (CollectionUtils.isEmpty(from)) {
    /*  77 */       return Sets.newLinkedHashSet();
        }
    /*  79 */     return (Set<T>)from.stream().filter(predicate).collect(Collectors.toSet());
      }
      
      private static List<? extends Number> str2numbers(String str, String splitChar, Function<String, ? extends Number> f) {
    /*  83 */     if (str == null || str.length() == 0) {
    /*  84 */       return Lists.newArrayList();
        }
    /*  86 */     List<String> strings = splitToList(splitChar, str);
    /*  87 */     if (strings.size() > 0) {
    /*  88 */       return (List<? extends Number>)strings.stream().<Number>map(f).collect(Collectors.toList());
        }
    /*  90 */     return Lists.newArrayList();
      }
    
      
      public static <T> String list2str(Collection<T> list, String splitChar, Function<T, Object> function) {
    /*  95 */     StringBuilder sb = new StringBuilder();
    /*  96 */     T lastT = null;
    /*  97 */     for (T t : list) {
    /*  98 */       if (sb.length() > 0 && lastT != null) {
    /*  99 */         sb.append(splitChar);
          }
    /* 101 */       if (t != null) {
    /* 102 */         Object obj = function.apply(t);
    /* 103 */         if (obj != null) {
    /* 104 */           sb.append(obj);
    /* 105 */           lastT = t;
            } 
          } 
        } 
    /* 109 */     return sb.toString();
      }
      
      public static <T> String list2strStrengthen(Collection<T> list, String splitChar, Function<T, Object> function) {
    /* 113 */     return CollectionUtils.isEmpty(list) ? "" : list2str(list, splitChar, function);
      }
    
    
    
      
      public static <T> String array2str(T[] ts, String splitChar, Function<T, Object> function) {
    /* 120 */     return list2str(Arrays.asList(ts), splitChar, function);
      }
    
      
      public static <K extends Comparable, V, R> Collection<R> map2KeySet(Map<K, V> map, Function<K, R> func) {
    /* 125 */     return (Collection<R>)map.entrySet().stream().map(kvEntry -> func.apply(kvEntry.getKey())).collect(Collectors.toCollection(java.util.LinkedHashSet::new));
      }
    
      
      public static <K extends Comparable, V, R> List<R> map2List(Map<K, V> map, BiFunction<K, V, R> func) {
    /* 130 */     return (List<R>)map.entrySet().stream().map(kvEntry -> func.apply(kvEntry.getKey(), kvEntry.getValue())).collect(Collectors.toList());
      }
    
      
      public static <K, V, R> Map<K, R> map2map(Map<K, V> map, Function<V, R> funcValue) {
    /* 135 */     Map<K, R> resultMap = Maps.newLinkedHashMap();
    /* 136 */     map.forEach((k, v) -> {
              R r = funcValue.apply(v);
              resultMap.put(k, r);
            });
    /* 140 */     return resultMap;
      }
    
    
    
      
      public static <K extends Comparable, V, R> List<R> map2ValueList(Map<K, V> map, Function<V, R> func) {
    /* 147 */     return map2List(map, (k, v) -> func.apply(v));
      }
    
      
      public static <K, R> Map<K, R> list2map(Collection<R> list, Function<R, K> func) {
    /* 152 */     return (Map<K, R>)list.stream().collect(Collectors.toMap(func, Function.identity(), (v1, v2) -> v1, java.util.LinkedHashMap::new));
      }
    
      
      public static <K, R> Map<K, List<R>> list2mapByList(Collection<R> list, Function<R, K> func) {
    /* 157 */     return (Map<K, List<R>>)list.stream().collect(Collectors.groupingBy(func));
      }
    
      
      public static <K, V, R> Map<K, V> list2map(Collection<R> list, Function<R, K> funcKey, Function<R, V> funcValue) {
    /* 162 */     return (Map<K, V>)list.stream().collect(Collectors.toMap(funcKey, funcValue, (v1, v2) -> v1, java.util.LinkedHashMap::new));
      }
    
      
      public static <T, U> List<U> list2list(Collection<T> from, Function<T, U> func) {
    /* 167 */     return (List<U>)from.stream().<U>map(func).collect(Collectors.toList());
      }
      
      public static <T, U> List<U> list2listStrengthen(Collection<T> from, Function<T, U> func) {
    /* 171 */     if (CollectionUtils.isEmpty(from)) {
    /* 172 */       return Lists.newArrayList();
        }
    /* 174 */     return (List<U>)from.stream().<U>map(func).collect(Collectors.toList());
      }
    
      
      public static <T, U> Set<U> list2set(Collection<T> from, Function<T, U> func) {
    /* 179 */     return (Set<U>)from.stream().<U>map(func).collect(Collectors.toCollection(java.util.LinkedHashSet::new));
      }
    
      
      public static <T, U> List<U> array2list(T[] from, Function<T, U> func) {
    /* 184 */     return (List<U>)Arrays.<T>stream(from).<U>map(func).collect(Collectors.toList());
      }
    
      
      public static <T> T maxValue(Collection<T> list, Comparator<T> comparator) {
    /* 189 */     return maxValue(list.stream(), comparator);
      }
    
    
      
      public static <T> T maxValue(Stream<T> stream, Comparator<T> comparator) {
    /* 195 */     Optional<T> optional = stream.max(comparator);
    /* 196 */     return optional.orElse(null);
      }
    
      
      public static <T> T minValue(Collection<T> list, Comparator<T> comparator) {
    /* 201 */     return minValue(list.stream(), comparator);
      }
    
      
      public static <T> T minValue(Stream<T> stream, Comparator<T> comparator) {
    /* 206 */     Optional<T> optional = stream.min(comparator);
    /* 207 */     return optional.orElse(null);
      }
    
      
      public static <T extends Number> long sumValue(List<T> list) {
    /* 212 */     return list.stream().mapToLong(Number::longValue).sum();
      }
    
    
    
    
    
      
      public static <T> T getOne(List<T> list, Predicate<T> predicate) {
    /* 221 */     List<T> filter = (List<T>)((List<T>)Optional.<List<T>>ofNullable(list).orElse(Lists.newArrayList())).stream().filter(predicate).collect(Collectors.toList());
        
    /* 223 */     if (CollectionUtils.isNotEmpty(filter)) {
    /* 224 */       return filter.get(0);
        }
    /* 226 */     return null;
      }
    
      private static List<String> splitToList(String separator, String str) {
          Iterable<String> iterable = Splitter.on(separator).split(str);
        return Lists.newArrayList(iterable);
      }
    }
    
    
    
  • 相关阅读:
    MySQL explain,Extra分析(转)
    MySQL explain,type分析(转)
    HTTP报文结构和内容(转)
    linux 环境 Xshell操作数据库
    服务器线上问题排查研究
    服务性能指标:PV、UV、TPS、QPS
    Git推送错误Remote: User permission denied错误解决方法
    SQL Server表关联
    SQL Server执行计划
    C#正则Groups高级使用方法
  • 原文地址:https://www.cnblogs.com/ukzq/p/13622431.html
Copyright © 2011-2022 走看看