# CollectionUtils 常用工具类

CollectionUtils 工具类


自己写的

集合为空
public static boolean isNotEmpty(Collection<?> list){
    //判断集合为空
    if(list==null||list.size()==0 ){
        return false;
    }
    return true;
}
集合不为空
public static boolean isEmpty(Collection<?> collection){
    if( collection==null || collection.size()==0 ){
        return true;
    }
    return false;
}
集合的并集
public static List union(List<String> list1, List<String> list2) {
    if(list1.size()>list2.size()){
        for(int i=0;i<list2.size();i++){

            list1.add(list2.get(i));
        }
        return list1;
    }else{
        for(int i=0;i<list1.size();i++){
            Object content=list1.get(i);
            list2.add(list1.get(i));
        }
        return list2;
    }
}
集合的交集
public static List together(List<String> list1, List<String> list2) {
    List<String> together = new ArrayList<>();
    if (list1.size() > list2.size()) {
        for (int i = 0; i < list1.size(); i++) {
            String x1 = list1.get(i);
            for (int j = 0; j < list2.size(); j++) {
                String x2 = list2.get(j);
                if (x1.equals(x2)) {
                    together.add(x1);
                }
            }
        }
    } else {
        for (int i = 0; i < list2.size(); i++) {
            String x1 = list2.get(i);
            for (int j = 0; j < list1.size(); j++) {
                String x2 = list1.get(j);
                if (x1.equals(x2)) {
                    together.add(x1);
                }
            }
        }
    }
    return together;
}
List去重:利用Set的特性
public static List<Object> removeRepeat(List<Object> list){
    Set<Object> set=new HashSet<Object>(list);
    return new ArrayList<>(set);
}
List对象集合去重:重写equals方法在里面逐个属性去判断,重写hashCode()
/**
 * 重写equlas
 */
@Override
public boolean equals(Object object){
    if(this==object) {
        return true;
    }
    if(object==null || object.getClass()!=getClass()){
        return false;
    }
    People people= (People) object;
    return id==people.id && name.equals(people.name) && age==people.age;
}
/**
 * 重写hashCode方法
 */
@Override
public int hashCode(){
    int hash = hash(id, name, age);
    return hash;
}
  • List过滤
@Test
public void test1(){
    People people1=new People(1,"张三",23);
    People people5=new People(1,"张三",23);
    People people2=new People(2,"张三",23);
    People people3=new People(1,"张三",22);
   
    List<People> peopleList=new ArrayList<People>();
    peopleList.add(people1);
    peopleList.add(people2);
    peopleList.add(people3);
    //实现对象去重操作
    peopleList.stream().distinct().forEach(System.out::println);
}

List自带的

交集
List<String> list=new ArrayList<>();
List<String> list1=new ArrayList<>();
list1.retainAll(list)
差集
 List<String>  list = new ArrayList<>();
 List<String>  list1 = new ArrayList<>();
 list1.removeAll(list);
并集
List<String>  list1 = new ArrayList<>();
List<String>  list2 = new ArrayList<>();
list2.removeAll(list1); 
list2.addAll(listB); 

org.apache.commons下的工具类

Maven依赖
<dependency>
    <groupId>org.apache.commonsgroupId>
    <artifactId>commons-collections4artifactId>
    <version>4.4version>
dependency>
CollectionUtils 并集
CollectionUtils.union(listA, listB)
CollectionUtils 交集
CollectionUtils.intersection(listA, listB)
CollectionUtils 交集的补集
CollectionUtils.disjunction(listA, listB)
CollectionUtils 差集
 CollectionUtils.subtract(listA, listB)

你可能感兴趣的:(工具包)