CollectionUtils工具类的常用方法[list排序、list去重、map遍历、转换、分片等]

集合判断:

例1: 判断集合是否为空:

CollectionUtils.isEmpty(null): true
CollectionUtils.isEmpty(new ArrayList()): true  
CollectionUtils.isEmpty({a,b}): false

例2: 判断集合是否不为空:

CollectionUtils.isNotEmpty(null): false
CollectionUtils.isNotEmpty(new ArrayList()): false
CollectionUtils.isNotEmpty({a,b}): true

并集

@Test
public void testUnion(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取并集 
    System.out.println(ArrayUtils.toString(CollectionUtils.union(listA, listB)));
    //[A, B, C, D, E, F, G, H, K]
}

交集

@Test
public void testIntersection(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 
    System.out.println(ArrayUtils.toString(CollectionUtils.intersection(listA, listB)));
    //[B, D, F]
}

交集的补集(析取)

@Test
public void testDisjunction(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //2个数组取交集 的补集
    System.out.println(ArrayUtils.toString(CollectionUtils.disjunction(listA, listB)));
    //[A, C, E, G, H, K]
}

差集(扣除)

@Test
public void testSubtract(){
    String[] arrayA = new String[] { "A", "B", "C", "D", "E", "F" };  
    String[] arrayB = new String[] { "B", "D", "F", "G", "H", "K" };
    List<String> listA = Arrays.asList(arrayA);
    List<String> listB = Arrays.asList(arrayB);
    //arrayA扣除arrayB
    System.out.println(ArrayUtils.toString(CollectionUtils.subtract(listA, listB)));
    //[A, C, E]
}

集合是否相等

@Test
public void testIsEqual(){
    class Person{}
    class Girl extends Person{
    }
    List<Integer> first = new ArrayList<>();
    List<Integer> second = new ArrayList<>();
    first.add(1);
    first.add(2);
    second.add(2);
    second.add(1);
    Girl goldGirl = new Girl();
    List<Person> boy1 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy1.add(new Girl());
    List<Person> boy2 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy2.add(new Girl());
    //比较两集合值
    System.out.println(CollectionUtils.isEqualCollection(first,second));   //true
    System.out.println(CollectionUtils.isEqualCollection(first,boy1));   //false
    System.out.println(CollectionUtils.isEqualCollection(boy1,boy2));   //false

    List<Person> boy3 = new ArrayList<>();
    //每个男孩心里都装着一个女孩
    boy3.add(goldGirl);
    List<Person> boy4 = new ArrayList<>();
    boy4.add(goldGirl);
    System.out.println(CollectionUtils.isEqualCollection(boy3,boy4));   //true
}

不可修改的集合

我们对c进行操作,s也同样获得了和c相同的内容,这样就可以避免其他人员修改这个s对象。有时候需要对它进行保护,避免返回结果被人修改。

@Test
public void testUnmodifiableCollection(){
    Collection<String> c = new ArrayList<>();
    Collection<String> s = CollectionUtils.unmodifiableCollection(c);
    c.add("boy");
    c.add("love");
    c.add("girl");
    //! s.add("have a error");
    System.out.println(s);
}

Collections.unmodifiableCollection可以得到一个集合的镜像,它的返回结果是不可直接被改变,否则会提示错误

java.lang.UnsupportedOperationException
at org.apache.commons.collections.collection.UnmodifiableCollection.add(UnmodifiableCollection.java:75)

Comparator 的使用有两种方式

 Collections.sort(list,Comparator<T>;
 list.sort(Comparator<T>);

其实主要是看 Comparator 接口的实现,重写里面的 compare 方法。代码如下:

		//自定义排序1
        Collections.sort(list, new Comparator<Student>() {
		@Override
		public int compare(Student o1, Student o2) {
        	return o1.getId() - o2.getId();
        	}
        });

compare(Student o1, Student o2) 方法的返回值跟 Comparable<> 接口中的 compareTo(Student o) 方法 返回值意思相同。另一种写法如下:

 	//自定义排序2
    list.sort(new Comparator<Student>() {
	@Override
		public int compare(Student o1, Student o2) {
       	 	return o1.getId() - o2.getId();
        	}
        });

List转String数组【String数组转成list各种格式】

注意split数组为空的情况

public static final String SEGMENTATION = ",";

    /**
     * 将String 转换成 List<Long>
     *
     * @param str
     * @return
     */
    public static List<Long> splitStrToListLong(String str) {
        List<Long> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(str)) {
            String[] split = StringUtils.split(str, SEGMENTATION);
            for (String string : split) {
                list.add(NumberUtils.toLong(string, -1l));
            }
        }
        return list;
    }

    /**
     * 将String 转换成 List<Integer>
     *
     * @param str
     * @return
     */
    public static List<Integer> splitStrToListInt(String str) {
        List<Integer> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(str)) {
            String[] split = StringUtils.split(str, SEGMENTATION);
            for (String string : split) {
                list.add(NumberUtils.toInt(string, -1));
            }
        }
        return list;
    }

    /**
     * 将String 转换成 List<String>
     *
     * @param str
     * @return
     */
    public static List<String> splitStrToListString(String str) {
        List<String> list = new ArrayList<>();
        if (StringUtils.isNotEmpty(str)) {
            String[] split = StringUtils.split(str, SEGMENTATION);
            //参数中只有一个值
             if(split == null){
                list.add(str);
                return list ;
            }
            for (String string : split) {
                list.add(string);
            }
        }
        return list;
    }

    /**
     * list<String>转string
     * @param list
     * @param separator
     * @return
     */
    public static String listToString(List list, char separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);
        }
        return sb.toString().substring(0, sb.toString().length() - 1);
    }

-------遍历arrays List-------

import java.util.*;
public class Test{
 public static void main(String[] args) {
     List<String> list=new ArrayList<String>();
     list.add("Hello");
     list.add("World");
     list.add("HAHAHAHA");
     //第一种遍历方法使用foreach遍历List
     for (String str : list) {            //也可以改写for(int i=0;i<list.size();i++)这种形式
        System.out.println(str);
     }
 
     //第二种遍历,把链表变为数组相关的内容进行遍历
     String[] strArray=new String[list.size()];
     list.toArray(strArray);
     for(int i=0;i<strArray.length;i++) //这里也可以改写为  foreach(String str:strArray)这种形式
     {
        System.out.println(strArray[i]);
     }
     
    //第三种遍历 使用迭代器进行相关遍历
     
     Iterator<String> ite=list.iterator();
     while(ite.hasNext())//判断下一个元素之后有值
     {
         System.out.println(ite.next());
     }
 }
}

解析
三种方法都是用来遍历ArrayList集合,第三种方法是采用迭代器的方法,该方法可以不用担心在遍历的过程中会超出集合的长度。
-------遍历Map-------

import java.util.*;
public class Test{
     public static void main(String[] args) {
      Map<String, String> map = new HashMap<String, String>();
      map.put("1", "value1");
      map.put("2", "value2");
      map.put("3", "value3");
      //第一种:普遍使用,二次取值
      System.out.println("通过Map.keySet遍历key和value:");
      for (String key : map.keySet()) {
       System.out.println("key= "+ key + " and value= " + map.get(key));
      }
      //第二种
      System.out.println("通过Map.entrySet使用iterator遍历key和value:");
      Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
      while (it.hasNext()) {
       Map.Entry<String, String> entry = it.next();
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }
      //第三种:推荐,尤其是容量大时
      System.out.println("通过Map.entrySet遍历key和value");
      for (Map.Entry<String, String> entry : map.entrySet()) {
       System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
      }
      //第四种
      System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
      for (String v : map.values()) {
       System.out.println("value= " + v);
      }
     }
}
   	//string 转为map转为list
    Gson gson = new Gson();
    Map<String, Object> map = new HashMap<String, Object>();
    map = gson.fromJson(propertyLabelVO.getLabelReason(), map.getClass());
    List<WorkflowPropertyLabelResonListVO> workflowPropertyLabelResonListVOList = new ArrayList<>();
    WorkflowPropertyLabelResonListVO workflowPropertyLabelResonListVO = null;
    for (Map.Entry<String, Object> entry : map.entrySet()) {
         workflowPropertyLabelResonListVO = new WorkflowPropertyLabelResonListVO();
         workflowPropertyLabelResonListVO.setIsreach(entry.getValue());
         workflowPropertyLabelResonListVO.setTitle(entry.getKey());
         workflowPropertyLabelResonListVOList.add(workflowPropertyLabelResonListVO);
    }

------集合框架优化【list.removeAll】大数据量优化--------

求两个集合的补集,考虑到collection类有removeAll方法,决定采用这种方式;结果程序卡死;
数据量是两个集合的数据差不多都有60万,直接导致程序处于假死状态(程序当然是还在运行);
又给程序修改为先用retainAll求交集,然后再removeAll的方式,效果不明显(事实是也假死了),我的应用场景还要求实时性,没办法只能从其他方面找寻思路了;

  1. removeAll执行效率低,改成循环remove试试看,结果是效率上有了一定的提升(在此需要注意list要倒序循环,因为remove是删除了下标,删除了之后后边的元素会前移),但是效果仍然不明显;
  2. 另外一种是采用Iterator迭代器,这种方式我们仅需要对iterator进行循环,然后对需要删除的元素执行iterator.remove(iterator.next()),而无需关注下标的问题;
    结合list的特性,LinkedList插入更新效率高,ArrayList查询效率高,对这里的使用场景我们显然需要将集合转换成LinkedList
    以下list的优化工具类,代码如下
package com.wyg.collection;
 
import java.util.List;
import java.util.LinkedList;
import java.util.HashSet;
import java.util.Iterator;
 
 
public class RemoveAllProfile{
	public static List removeAll(List src,List oth){
		LinkedList result = new LinkedList(src);//大集合用linkedlist
		HashSet othHash = new HashSet(oth);//小集合用hashset
		Iterator iter = result.iterator();//采用Iterator迭代器进行数据的操作
		while(iter.hasNext()){
			if(othHash.contains(iter.next())){
				iter.remove();			
			}	
		}
		return result;
	}
}

List分片处理

一. 按固定大小分片

1.直接用guava库的partition方法。

import com.google.common.collect.Lists;
 
public class ListsPartitionTest {
    public static void main(String[] args) {
        List<String> ls = Arrays.asList("1,2,3,4,5,6,7,8,9,1,2,4,5,6,7,7,6,6,6,6,6,66".split(","));
        log.info(Lists.partition(ls, 20));
    }
}
//  [[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 4, 5, 6, 7, 7, 6, 6, 6, 6], [6, 66]]

2.使用apache.commons.collection实现

import org.apache.commons.collections4.ListUtils;
public class ListsPartitionTest2 {
    public static void main(String[] args) {
       List<String> intList = Arrays.asList("1,2,3,4,5,6,7,8,9,1,2,4,5,6,7,7,6,6,6,6,6,66".split(","));
        System.out.println(ListUtils.partition(intList, 3));
    }
}
 
 
// [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 4], [5, 6, 7], [7, 6, 6], [6, 6, 6], [66]]

二、将list 平均分成n份

/**
     * 将一个list均分成n个list,主要通过偏移量来实现的
     *
     * @param source
     * @return
     */
    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int remaider = source.size() % n;  //(先计算出余数)
        int number = source.size() / n;  //然后是商
        int offset = 0;//偏移量
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remaider > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remaider--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

简化版

/**
     * 将一个list分成n个list, 
     *   每个list放的是商的个数,最后除数的余数放入最后一个list里
     *
     * @param source
     * @return
     */
    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        int number = source.size() / n;  //商
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (i == n - 1) {
                value = source.subList(i * number, source.size());
            } else {
                value = source.subList(i * number, (i + 1) * number);
            }
            result.add(value);
        }
        return result;
    }

-------去重操作-------

 Set set = new HashSet();
 List<ResumeView> listNew = new ArrayList<>();
 set.addAll(list);
 listNew.addAll(set);
 list = listNew;
  • 3
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值