* 集合操作常用方法类.
* <p>
*
* @author 柯
*/
public class ListUtil { /**
* 判断List不为空,非空返回true,空则返回false
*
* @param list
* @return boolean
*/
public static boolean isNotNull(List<?> list) { if (null != list) {
if ((list.size() > 0) && !list.isEmpty()) {
return true;
}
}
return false;
} /**
* 判断List是为空,为空返回true,非空则返回false
*
* @param list
* @return boolean
*/
public static boolean isNull(List<?> list) { if (null == list || list.size() == 0 || list.isEmpty()) {
return true;
}
return false;
} /**
*
* @Title: removeDuplist @date 2016年7月17日 下午3:55:38 @Description:
* 去除集合中重复的内容 @param list @return @throws
*/
public static List<String> removeDuplist(List<String> list) {
if (list != null && list.size() > 0) {
HashSet<String> hashSet = new HashSet<String>(list);
list.clear();
list.addAll(hashSet);
}
return list;
} /**
*
* @Title: removeDuplistInt @date 2016年7月18日 下午5:16:04 @Description:
* 去除重复的值 @param list @return @throws
*/
public static List<Integer> removeDuplistInt(List<Integer> list) {
if (list != null && list.size() > 0) {
HashSet<Integer> hashSet = new HashSet<Integer>(list);
list.clear();
list.addAll(hashSet);
}
return list;
} /**
*
* @Title: getCurveValue @date 2016年8月16日 下午4:48:33 @Description:
* 计算集合中的最大值和最小值, 返回改后的最大值,间隔,最小值 @param list @return @throws
*/
public static List<Long> getCurveValue(List<Long> list) {
List<Long> curveList = new ArrayList<Long>(); Long maxValue = 0l;// 集合中最大值
Long avgValue = 0l;// 间隔值
Long minValue = 0l;// 最小值 if (ListUtil.isNotNull(list)) {
maxValue = Collections.max(list) / 100;
} if (maxValue > 10000) {
maxValue += 1000;
} else if (maxValue > 1000) {
maxValue += 300;
} else if (maxValue > 100) {
maxValue += 50;
} else if (maxValue > 10) {
maxValue += 5;
} else {
maxValue = 10l;
} avgValue = maxValue / 5; curveList.add(maxValue);
curveList.add(avgValue);
curveList.add(minValue); return curveList;
} /**
* list集合深度复制
* @param src
* @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();
return dest;
} public static void main(String[] args) {
List<String> list = new ArrayList<String>();
for (int i = 0 ; i < 19; i++) {
list.add("aa" + i);
}
int d = list.size() / 13;
int q = list.size() % 13;
if (q > 0)
d += 1; List<List<String>> datas = new ArrayList<List<String>>(d);
if (d == 1) {
datas.add(list);
} else if (d > 1){
for (int k = 0; k < d; k ++) {
List<String> sub_a = new ArrayList<String>();
if (k == d - 1)
sub_a = list.subList(k * 13, list.size());
else
sub_a = list.subList(k * 13, (k + 1) * 13); datas.add(sub_a);
}
} System.out.println(d);
for (List<String> obj : datas) {
List<String> data = obj;
for (String c : data) {
System.out.println(c);
}
}
} /**
* 获取列表总页数
*/
public static <T> int getListPages(List<T> list,int pageNum,int pageSize ){
if (isNull(list)){
return 0;
}
BaseQuery baseQuery=new BaseQuery();
baseQuery.setPageNum(pageNum);
baseQuery.setPageSize(pageSize);
//list的大小
int total = list.size();
baseQuery.setTotal(total);
return baseQuery.getPages();
} /**
* 对列表进行分页,索引左边包括,右边不包括
*/
public static <T> List<T> subListByPage(List<T> list,int pageNum,int pageSize ){
if (isNull(list)){
return Collections.emptyList();
}
BaseQuery baseQuery=new BaseQuery();
baseQuery.setPageNum(pageNum);
baseQuery.setPageSize(pageSize);
//list的大小
int total = list.size();
//对list进行截取
return list.subList(baseQuery.getStartPosition(),total-baseQuery.getStartPosition()>baseQuery.getPageSize()?baseQuery.getStartPosition()+baseQuery.getPageSize():total);
} /**
* 对列表进行索引截取,索引左边包括,右边不包括
*/
public static <T> List<T> subListByPosition(List<T> list,BaseQuery baseQuery){ if (isNull(list)){
baseQuery.setTotal(0);
return Collections.emptyList();
}
//设置列表总条数
int total = list.size();
baseQuery.setTotal(total); if ((baseQuery.getStartIndex()-1)>=total){
return Collections.emptyList();
}
//从0开始 --> 1
if (baseQuery.getStartIndex()==0){
baseQuery.setStartIndex(1);
}
//对list进行截取
return list.subList(baseQuery.getStartIndex()-1,baseQuery.getEndIndex()>total?total:baseQuery.getEndIndex());
} /**
*对列表字段进行比较排序
*/
public static <T> void sortByField(List<T> dtoList,String fieldName,String order) {
int compare=1;
if ("desc".equals(order)){
compare=-1;
}
int finalCompare = compare; Collections.sort(dtoList, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
PropertyDescriptor pd1 = null;
PropertyDescriptor pd2 = null;
Object value1 =null;
Object value2 =null;
try {
pd1 = new PropertyDescriptor(fieldName, o1.getClass());
value1 = pd1.getReadMethod().invoke(o1, null); pd2 = new PropertyDescriptor(fieldName, o2.getClass());
value2 = pd2.getReadMethod().invoke(o2, null); } catch (IntrospectionException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} if (value1.getClass().equals(Double.class)){
if ((Double)value1 > (Double)value2) {
return finalCompare;
} else if ((Double)value1 < (Double)value2) {
return -finalCompare;
}
}else if (value1.getClass().equals(Integer.class)){
if ((Integer)value1 > (Integer)value2) {
return finalCompare;
} else if ((Integer)value1 < (Integer)value2) {
return -finalCompare;
}
}
return 0;
}
});
} }

ListUtil集合操作常用方法类的更多相关文章

  1. 操作集合的工具类:Collections

    Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对集合对象实现同步控制等方法 ...

  2. 操作集合的工具类Collections

    1       操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...

  3. java中的集合操作类(未完待续)

    申明: 实习生的肤浅理解,如发现有错误之处.还望大牛们多多指点 废话 事实上我写java的后台操作,我每次都会遇到一条语句:List<XXXXX> list = new ArrayList ...

  4. java之操作集合的工具类--Collections

    Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...

  5. Java-集合第六篇操作集合的工具类Collections

    1.Java提供了一个操作Set.List.Map等集合的工具类:Collections. 工具类中提供的方法主要针对Set.List.Map的排序.查询.修改等操作,以及将集合对象设置为不可变.对集 ...

  6. Python 集合set添加删除、交集、并集、集合操作符号

    在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 1. ...

  7. Python 集合set()添加删除、交集、并集、集合操作详解

    集合:一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次.每个元素的地位都是相同的,元素之间是无序的. 创建集合set python set类是在python的sets模块中,大家现在使 ...

  8. Java集合——Collections工具类

    Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...

  9. ListUtils的简单集合操作和原理

    1 Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <artifactId>com ...

随机推荐

  1. hdu_hpu第八次周赛_1002 大菲波数_201310270958

    大菲波数 Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submissi ...

  2. pg 学习资料

    文/谭峰 DBA,PostgreSQL专家 开源数据库 PostgreSQL 中文资料非常缺乏,很多社区朋友苦于上手的中文资料少,因此汇总收集以下 PostgreSQL 中文资料,包括 Postgre ...

  3. Clojure:通过ZeroMQ推送消息

    通过ZeroMQ的pub/sub模式,我们可以实现发送推送消息的功能.以下为示例代码(入门可参考此文:http://www.cnblogs.com/ilovewindy/p/3984269.html) ...

  4. maven打包需要设置main-class的插件写法

    maven打包需要设置main-class的插件写法 <build> <plugins> <plugin> <groupId>org.apache.ma ...

  5. Apache OFbiz service engine 源代码解读

    上一篇看完了ofbiz entity engine,这篇再来过一下ofbiz的service engine.service engine层在设计模式的使用上跟entity engine有些相似,最典型 ...

  6. Delphi 中控件路径加入不进去解决方法

    使用notepa++打开project中的*.dproj文件,在里面找到相似例如以下的区域 <DCC_UnitSearchPath>T:\BusinessSkinForm1006Sourc ...

  7. ExecutorCompletionService原理具体解释

    在JDK并发包中有这么一个类ExecutorCompletionService,提交任务后,能够按任务返回结果的先后顺序来获取各任务运行后的结果. 该类实现了接口CompletionService: ...

  8. 《Qt on Android核心编程》前言:为什么写作本书

    2008年.我開始在CSDN写技术博客. 在此之前,我的理想是写出受人待见的小说来.我也以前在网络论坛上笔耕不辍获得一些成绩,也以前发表过一些散文以及小说.而那一年,当我再次拾起笔来写东西时.却选择了 ...

  9. C#获取本机Sql Serverserver名

    private void Form2_Load(object sender, EventArgs e) { listBox1.Items.Clear(); SQLDMO.Application SQL ...

  10. FFT模板——copied from hzwer

    /* Welcome Hacking Wish You High Rating */ #include<iostream> #include<cstdio> #include& ...