java List的相关工具类
2024-09-04 15:17:17
1、
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>18.0</version>
</dependency>
2、java代码
package xxxxxx; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import org.apache.commons.lang.StringUtils; import com.google.common.base.Function;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap; public class ListUtils { /**
* list 中有一个为主键key
*
* @param list
* @param function
* @return
*/
public static <K, V> Map<K, V> listToMap(List<V> list, Function<V, K> function) {
Map<K, V> map = new HashMap<K, V>();
if (list == null)
return map;
for (V value : list) {
K k = function.apply(value);
map.put(k, value);
}
return map;
} /**
* list 元素转为另一个元素的list
*/
public static <F, R> List<R> listConvert(List<F> list, Function<F, R> function) {
List<R> listN = new ArrayList<R>();
if (list == null)
return listN;
for (F value : list) {
R r = function.apply(value);
if (r != null)
listN.add(r);
}
return listN;
} /**
* list 元素过滤另一个元素的list
*/
public static <F> List<F> filter(List<F> list, Function<F, Boolean> function) {
List<F> listN = new ArrayList<F>();
if (list == null)
return listN;
for (F value : list) {
Boolean r = function.apply(value);
if (r)
listN.add(value);
}
return listN;
} /**
* list 元素转为另一个元素的list并且去重,适合list数据量较小
*/
public static <F, R> List<R> listConvertAndUnique(List<F> list, Function<F, R> function) {
List<R> listN = new ArrayList<R>();
if (list == null)
return listN;
for (F value : list) {
R r = function.apply(value);
if (r != null && !listN.contains(r))
listN.add(r);
}
return listN;
} /**
* list 元素转为以seg分割的字符串
*/
public static String split(List list, String seg) {
StringBuilder sb = new StringBuilder();
for (Object value : list) {
sb.append(value.toString() + seg);
}
String t = sb.toString();
if (t.endsWith(seg)) {
int end = t.length() - seg.length();
t = t.substring(0, end);
}
return t;
} public static <V> Map<Integer, V> listToMapByHashCode(List<V> list) {
Map<Integer, V> map = new HashMap<Integer, V>();
for (V value : list) {
map.put(Integer.valueOf(value.hashCode()), value);
}
return map;
} public static <K, V> Map<K, V> listToMapByFunction(List<V> list,Function<V,K> function) {
Map<K, V> map = new HashMap<K, V>();
for (V value : list) {
map.put(function.apply(value), value);
}
return map;
} // list to ListMultimap
public static <K, V> ListMultimap<K, V> listToListMultiMap(List<V> list, Function<V, K> function) {
ListMultimap listMultimap = ArrayListMultimap.<K, V> create();
for (V value : list) {
listMultimap.put(function.apply(value), value);
}
return listMultimap;
} public static <V> List<V> pageList(List<V> list, int pageIndex, int pageNum) {
int size = list.size();
int fromIndex = pageIndex * pageNum;
if (fromIndex > size) {
fromIndex = size;
}
int toIndex = fromIndex + pageNum;
if (toIndex > size) {
toIndex = size;
return list.subList(fromIndex, toIndex);
} return list.subList(fromIndex, toIndex);
} public static <E> boolean isEmpty(List<E> list) { return list == null || list.isEmpty();
} public static List<String> list(String s, String spe) { ArrayList<String> list = new ArrayList<>();
if (StringUtils.isBlank(s))
return list;
String[] e = s.split(spe);
for (String i : e) {
list.add(i);
}
return list; } public static <T> List<T> toList(T... a) { if (a == null)
return null;
List<T> l = new ArrayList<>();
for (T e : a) {
l.add(e);
} return l;
} /**
* 通过某个字段进行排序
*/
public static <K, V> List<V> sortList(List<V> list) { return null;
} }
3、在需要的类中调用即可
List<UserAccountDto> maList = null; //实际不能辅助为null,应该获取一个list实例
List<TopMobileAccountVo> mavoList = ListUtils.listConvert(maList,new Function<UserAccountDto, TopMobileAccountVo>() {
public TopMobileAccountVo apply(UserAccountDto ma) {
TopMobileAccountVo vo = new TopMobileAccountVo();
vo.convertPOToVO(ma);
return vo;
}
});
最新文章
- PowerShell中的基础数据类型
- CDN 实现原理
- ffmpeg relocation error
- JUnit笔记
- AngularJS 实现简单购物车
- [Effective JavaScript 笔记]第5章:数组和字典--个人总结
- tag标签调取
- JBPM工作流入门总结
- eclipse中 com.sun.image.codec.jpeg.JPEGCodec 无法编译通过问题
- java的动态绑定与双分派(规避instanceof)
- One git command may cause you hacked(CVE-2014-9390)
- dblink如果很慢可以用这种方式优化
- [其他]Jboss容器开启调试模式
- Spring-web中的web.xml为Servlet提供的配置选项说明
- java foreach遍历的前提条件
- BZOJ 2111: [ZJOI2010]Perm 排列计数 [Lucas定理]
- 我的那些年(9)~我来团队了,Mvc兴起了
- NPOI 的使用姿势
- window下mongodb的安装和环境搭建
- SQL Server 索引自动组织维护
热门文章
- 【Hadoop】Hadoop MR 如何实现倒排索引算法?
- 转:深度学习斯坦福cs231n 课程笔记
- VM虚拟机 Windows虚拟机中linux鼠标不能动怎么办
- ios多线程操作(四)—— GCD核心概念
- iOS 引入外部字体 otf/ttf/ttc
- ionic准备之angular基础——$watch,$apply,$timeout方法(5)
- perftools查看堆外内存并解决hbase内存溢出
- 浅谈struts2标签中的2个非经常常使用的标签的使用方法(radio和select)
- Mysql 创建权限较小的用户(只对特定数据库有操作权限)
- CoreImage的模糊滤镜