java collection 常用类_java 常用工具类Collection集合

/*** 初始化ArrayList

*

*@paramelements

*@return

*/@SafeVarargspublic static ListcreateArrayList(T... elements) {

List list = new ArrayList();for(T element : elements) {

list.add(element);

}returnlist;

}public static boolean isEmpty(Collection>collection) {return (collection == null ||collection.isEmpty());

}public static boolean isNotEmpty(Collection>collection) {return (collection != null && !collection.isEmpty());

}/*** 获取两个集合的差集

*

*@parambig

* 大集合

*@paramsmall

* 小集合

*@return两个集合的差集*/

public static Collection getDiffSection(Collection big, Collectionsmall) {

Set differenceSet =Sets.difference(Sets.newHashSet(big), Sets.newHashSet(small));returnLists.newArrayList(differenceSet);

}public static List getDiffSection(Collectionbig, T obj) {

Set small = new HashSet();

small.add(obj);

Set differenceSet =Sets.difference(Sets.newHashSet(big), small);returnLists.newArrayList(differenceSet);

}/*** 获取两个集合的交集

*

*@paramc1

*@paramc2

*@return

*/

public static List getInterSection(Collection c1, Collectionc2) {

Set intersections =Sets.intersection(Sets.newHashSet(c1), Sets.newHashSet(c2));returnLists.newArrayList(intersections);

}/*** 获取两个集合的合集

*

*@paramc1

*@paramc2

*@return

*/

public static List getUnionSection(Collection c1, Collectionc2) {

c1.addAll(c2);

Set newHashSet =Sets.newHashSet(c1);returnLists.newArrayList(newHashSet);

}public static List> splitList(List list, intpageSize) {int listSize = list.size(); //list的大小

int page = (listSize + (pageSize - 1)) / pageSize; //页数

List> listArray = new ArrayList>(); //创建list数组 ,用来保存分割后的list

for (int i = 0; i < page; i++) { //按照数组大小遍历

List subList = new ArrayList(); //数组每一位放入一个分割后的list

for (int j = 0; j < listSize; j++) { //遍历待分割的list

int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize; //当前记录的页码(第几页)

if (pageIndex == (i + 1)) { //当前记录的页码等于要放入的页码时

subList.add(list.get(j)); //放入list中的元素到分割后的list(subList)

}if ((j + 1) == ((j + 1) * pageSize)) { //当放满一页时退出当前循环

break;

}

}

listArray.add(subList);//将分割后的list放入对应的数组的位中

}returnlistArray;

}public static boolean isListEqual(Collection l0, Collectionl1) {if (l0 ==l1) {return true;

}if (l0 == null && l1 == null) {return true;

}if (l0 == null || l1 == null) {return false;

}if (l0.size() !=l1.size()) {return false;

}for(Object o : l0) {if (!l1.contains(o)) {return false;

}

}for(Object o : l1) {if (!l0.contains(o)) {return false;

}

}return true;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值