* 集合操作常用方法类.
* <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. nyoj_18_The Triangle_201312071533

    The Triangle 时间限制:1000 ms  |           内存限制:65535 KB 难度:4   描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure ...

  2. 【ACM】nyoj_47_过桥问题_201308151616

    过河问题时间限制:1000 ms  |  内存限制:65535 KB 难度:5描述 在漆黑的夜里,N位旅行者来到了一座狭窄而且没有护栏的桥边.如果不借助手电筒的话,大家是无论如何也不敢过桥去的.不幸的 ...

  3. 洛谷—— P2424 约数和

    https://www.luogu.org/problem/show?pid=2424 题目背景 Smart最近沉迷于对约数的研究中. 题目描述 对于一个数X,函数f(X)表示X所有约数的和.例如:f ...

  4. Flash-制作空心文字

    常常看到站点上用很多空心文字作为站点的名称或者特色项目的名称等等,那这些空心文字是怎么做出来的呢? 用Flash事实上非常快就能做出来.过程例如以下: (1)新建空白文件.工具箱中选择"文本 ...

  5. Shredding Company (hdu 1539 dfs)

    Shredding Company Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  6. NPM 3 Beta为Windows用户带来利好消息

    本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2015/06/angular-2-react-native-roadmap 近日,np ...

  7. 关于ffmpeg的安装具体步骤和说明

    一.windows安装: 直接下载地址: https://ffmpeg.zeranoe.com/builds/ https://ffmpeg.zeranoe.com/builds/win64/stat ...

  8. 实例介绍Cocos2d-x中Box2D物理引擎:碰撞检測

    在Box2D中碰撞事件通过实现b2ContactListener类函数实现,b2ContactListener是Box2D提供的抽象类,它的抽象函数:virtual void BeginContact ...

  9. 0507-php独立环境的安装与配置

    1.在一个纯英文目录下新建三个文件夹 2.安装apache(选择好版本) 过程中该填的按格式填好,其余的只更改安装目录即可 如果报错1901是安装版本的问题. 检查:安装完成后localhost打开为 ...

  10. js点击特效

    //点击效果博客页面点击就可以看到 <script type="text/javascript"> !function(e, t, a) { function n() ...