* 集合操作常用方法类.
* <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. [bzoj1901][Zju2112]Dynamic Rankings_主席树

    Dynamic Rankings bzoj-1901 Zju-2112 题目大意:给定一个n个数的序列,m个操作,支持:单点修改:查询区间k小值. 注释:$1\le n,m\le 10^4$. 想法: ...

  2. MySQL:解决MySQL无法启动的问题

    MySQL无法启动的原因有多种,这里是我遇到的一种情况和解决方法. 起因: 最近项目需要使用MySQL,于是想在MAC上安装一个本地的数据库,但是其实忘了已经安装过一个版本了,结果发现新的服务器怎么也 ...

  3. input子系统驱动学习之中的一个

        刚開始学习linux这门课就被分配编写一个设备的input子系统驱动.这对我的确有点困难.只是实际的操作中发现困难远比我想象的要大的多.本以为依照老师课上的步骤就行非常快的完毕这项任务.后来发 ...

  4. 大数据处理之道(实验方法&lt;二&gt;)

    一:交叉验证(crossvalidation)(附实验的三种方法)方法简单介绍   (1) 定义:交叉验证(Cross-validation)主要用于建模应用中,比如PCR(Principal Com ...

  5. Android updater-scripts(Edify Script)各函数详细说明【转】

    本文转载自:http://blog.csdn.net/kwuwei/article/details/40616909 这是Android系统来运行updater-scripts的Edify语言的基本介 ...

  6. CodeForces--626C--Block Towers (二分)

    Block Towers Time Limit: 2000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit ...

  7. Reactive Native开发环境搭建

    root@zhongzhenhua-virtual-machine:~/AndroidCode# repo init -u https://android.googlesource.com/platf ...

  8. thinkphp session db配置

    这篇文章主要介绍了ThinkPHP实现将SESSION存入MYSQL的方法,需要的朋友可以参考下   本文以实例讲解了ThinkPHP实现将SESSION存入MYSQL的方法,所采用的运行环境是Thi ...

  9. 8.19noip模拟题

      2017 8.19 NOIP模拟赛 by coolyangzc 共3道题目,时间3小时 题目名 高级打字机 不等数列 经营与开发 源文件 type.cpp/c/pas num.cpp/c/pas ...

  10. Python 42 mysql用户管理 、pymysql模块

    一:mysql用户管理 什么是mysql用户管理 mysql是一个tcp服务器,应用于操作服务器上的文件数据,接收用户端发送的指令,接收指令时需要考虑到安全问题, ATM购物车中的用户认证和mysql ...