zoukankan      html  css  js  c++  java
  • java List的相关工具类

    1、

    1 <dependency>
    2     <groupId>com.google.guava</groupId>
    3     <artifactId>guava</artifactId>
    4     <version>18.0</version>
    5 </dependency>

    2、java代码

      1 package xxxxxx;
      2 
      3 import java.util.ArrayList;
      4 import java.util.HashMap;
      5 import java.util.List;
      6 import java.util.Map;
      7 
      8 import org.apache.commons.lang.StringUtils;
      9 
     10 import com.google.common.base.Function;
     11 import com.google.common.collect.ArrayListMultimap;
     12 import com.google.common.collect.ListMultimap;
     13 
     14 public class ListUtils {
     15 
     16     /**
     17      * list 中有一个为主键key
     18      * 
     19      * @param list
     20      * @param function
     21      * @return
     22      */
     23     public static <K, V> Map<K, V> listToMap(List<V> list, Function<V, K> function) {
     24         Map<K, V> map = new HashMap<K, V>();
     25         if (list == null)
     26             return map;
     27         for (V value : list) {
     28             K k = function.apply(value);
     29             map.put(k, value);
     30         }
     31         return map;
     32     }
     33 
     34     /**
     35      * list 元素转为另一个元素的list
     36      */
     37     public static <F, R> List<R> listConvert(List<F> list, Function<F, R> function) {
     38         List<R> listN = new ArrayList<R>();
     39         if (list == null)
     40             return listN;
     41         for (F value : list) {
     42             R r = function.apply(value);
     43             if (r != null)
     44                 listN.add(r);
     45         }
     46         return listN;
     47     }
     48 
     49     /**
     50      * list 元素过滤另一个元素的list
     51      */
     52     public static <F> List<F> filter(List<F> list, Function<F, Boolean> function) {
     53         List<F> listN = new ArrayList<F>();
     54         if (list == null)
     55             return listN;
     56         for (F value : list) {
     57             Boolean r = function.apply(value);
     58             if (r)
     59                 listN.add(value);
     60         }
     61         return listN;
     62     }
     63 
     64     /**
     65      * list 元素转为另一个元素的list并且去重,适合list数据量较小
     66      */
     67     public static <F, R> List<R> listConvertAndUnique(List<F> list, Function<F, R> function) {
     68         List<R> listN = new ArrayList<R>();
     69         if (list == null)
     70             return listN;
     71         for (F value : list) {
     72             R r = function.apply(value);
     73             if (r != null && !listN.contains(r))
     74                 listN.add(r);
     75         }
     76         return listN;
     77     }
     78 
     79     /**
     80      * list 元素转为以seg分割的字符串
     81      */
     82     public static String split(List list, String seg) {
     83         StringBuilder sb = new StringBuilder();
     84         for (Object value : list) {
     85             sb.append(value.toString() + seg);
     86         }
     87         String t = sb.toString();
     88         if (t.endsWith(seg)) {
     89             int end = t.length() - seg.length();
     90             t = t.substring(0, end);
     91         }
     92         return t;
     93     }
     94 
     95     public static <V> Map<Integer, V> listToMapByHashCode(List<V> list) {
     96         Map<Integer, V> map = new HashMap<Integer, V>();
     97         for (V value : list) {
     98             map.put(Integer.valueOf(value.hashCode()), value);
     99         }
    100         return map;
    101     }
    102     
    103     public static <K, V> Map<K, V> listToMapByFunction(List<V> list,Function<V,K> function) {
    104         Map<K, V> map = new HashMap<K, V>();
    105         for (V value : list) {
    106             map.put(function.apply(value), value);
    107         }
    108         return map;
    109     }
    110 
    111     // list to ListMultimap
    112     public static <K, V> ListMultimap<K, V> listToListMultiMap(List<V> list, Function<V, K> function) {
    113         ListMultimap listMultimap = ArrayListMultimap.<K, V> create();
    114         for (V value : list) {
    115             listMultimap.put(function.apply(value), value);
    116         }
    117         return listMultimap;
    118     }
    119 
    120     public static <V> List<V> pageList(List<V> list, int pageIndex, int pageNum) {
    121         int size = list.size();
    122         int fromIndex = pageIndex * pageNum;
    123         if (fromIndex > size) {
    124             fromIndex = size;
    125         }
    126         int toIndex = fromIndex + pageNum;
    127         if (toIndex > size) {
    128             toIndex = size;
    129             return list.subList(fromIndex, toIndex);
    130         }
    131 
    132         return list.subList(fromIndex, toIndex);
    133     }
    134 
    135     public static <E> boolean isEmpty(List<E> list) {
    136 
    137         return list == null || list.isEmpty();
    138     }
    139 
    140     public static List<String> list(String s, String spe) {
    141 
    142         ArrayList<String> list = new ArrayList<>();
    143         if (StringUtils.isBlank(s))
    144             return list;
    145         String[] e = s.split(spe);
    146         for (String i : e) {
    147             list.add(i);
    148         }
    149         return list;
    150 
    151     }
    152 
    153     public static <T> List<T> toList(T... a) {
    154 
    155         if (a == null)
    156             return null;
    157         List<T> l = new ArrayList<>();
    158         for (T e : a) {
    159             l.add(e);
    160         }
    161 
    162         return l;
    163     }
    164 
    165     /**
    166      * 通过某个字段进行排序
    167      */
    168     public static <K, V> List<V> sortList(List<V> list) {
    169 
    170         return null;
    171     }
    172 
    173 }

    3、在需要的类中调用即可

    1 List<UserAccountDto> maList = null; //实际不能辅助为null,应该获取一个list实例
    2 List<TopMobileAccountVo> mavoList = ListUtils.listConvert(maList,new Function<UserAccountDto, TopMobileAccountVo>() {
    3     public TopMobileAccountVo apply(UserAccountDto ma) {
    4         TopMobileAccountVo vo = new TopMobileAccountVo();
    5         vo.convertPOToVO(ma);
    6         return vo;
    7     }
    8 });
  • 相关阅读:
    用word2007发布带图片博客windows服务程序
    ActiveRecord与GridView,ObjectDataSource配合使用时的问题
    Spring.NET & NHibernate文章收集
    myOleDbConnection.GetOleDbSchemaTable异常
    想看本书《CSS禅意花园》
    一个考生表,现在要随机生成考号,遇到sql疑问,请教各位大虾
    选中与复制表格
    ActiveRecord多表查询
    DotNetNuke与MemberShip的结合(五年版)
    IIS+php5.0+mysql架设
  • 原文地址:https://www.cnblogs.com/kongxianghao/p/6877478.html
Copyright © 2011-2022 走看看