ListUtil集合操作常用方法类
* 集合操作常用方法类.
* <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集合操作常用方法类的更多相关文章
- 操作集合的工具类:Collections
Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类提供了大量方法对集合进行排序.查询和修改等操作,还提供了将集合对象置为不可变.对集合对象实现同步控制等方法 ...
- 操作集合的工具类Collections
1 操作集合的工具类Collections Java提供了一个操作Set.List和Map等集合的工具类:Collections,该工具类里提供了大量方法对集合元素进行排序.查询和修改等操 ...
- java中的集合操作类(未完待续)
申明: 实习生的肤浅理解,如发现有错误之处.还望大牛们多多指点 废话 事实上我写java的后台操作,我每次都会遇到一条语句:List<XXXXX> list = new ArrayList ...
- java之操作集合的工具类--Collections
Collections是一个操作Set.List和Map等集合的工具类. Collections中提供了大量方法对集合元素进行排序.查询和修改等操作,还提供了对集合对象设置不可变.对集合对象实现同步控 ...
- Java-集合第六篇操作集合的工具类Collections
1.Java提供了一个操作Set.List.Map等集合的工具类:Collections. 工具类中提供的方法主要针对Set.List.Map的排序.查询.修改等操作,以及将集合对象设置为不可变.对集 ...
- Python 集合set添加删除、交集、并集、集合操作符号
在Python中集合set是基本数据类型的一种,它有可变集合(set)和不可变集合(frozenset)两种.创建集合set.集合set添加.集合删除.交集.并集.差集的操作都是非常实用的方法. 1. ...
- Python 集合set()添加删除、交集、并集、集合操作详解
集合:一个集合中,任何两个元素都认为是不相同的,即每个元素只能出现一次.每个元素的地位都是相同的,元素之间是无序的. 创建集合set python set类是在python的sets模块中,大家现在使 ...
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...
- ListUtils的简单集合操作和原理
1 Maven依赖 <dependency> <groupId>org.apache.commons</groupId> <artifactId>com ...
随机推荐
- hdu_1039_Easier Done Than Said_201311051511
Easier Done Than Said? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/O ...
- Expanding Rods POJ 1905 二分
Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17050 Accepted: 4503 Description When ...
- Java JVM虚拟机选项Xms/Xmx/PermSize/MaxPermSize(转)
通过JVM的这些选项:Xms/Xmx/PermSize/MaxPermSize可以牵扯出很多问题,比如性能调优等. 说明:以下转载没经过实践. 经验实例(参考): 设置每个线程的堆栈大小.JDK5.0 ...
- firdac支持的序列和还原格式
TFDStorageFormat = (sfAuto, sfXML, sfBinary, sfJSON); FIREDAC支持3种序列格式:XML,JSON和BIN.
- AngularJS:添加检查密码输入是否一致的功能
感谢作者(http://blog.brunoscopelliti.com/angularjs-directive-to-check-that-passwords-match) 利用AngularJS的 ...
- Clojure:从Java调用Clojure
我们要在Java中调用Clojure有两种方法,一种是将Clojure代码生成class文件,另外一种是通过Clojure RT方式直接在java程序中调用Clojure代码.两种方式各有优缺点, 第 ...
- POJ 2960
也算是一道模板题吧,只需按照SG函数的定义求出每个值的SG,然后异或就可以了. #include <iostream> #include <cstdio> #include & ...
- Android之QQ授权登录获取用户信息
有时候我们开发的app须要方便用户简单登录.能够让用户使用自己的qq.微信.微博登录到我们自己开发的app. 今天就在这里总结一下怎样在自己的app中集成QQ授权登录获取用户信息的功能. 首先我们打开 ...
- hdu1325 Is It A Tree?(二叉树的推断)
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) To ...
- java js url传参中文乱码
String item = this.getRequest().getParameter("item"); item = new String(item.getBytes(&quo ...