# 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.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>
CollectionUtils 并集
CollectionUtils.union(listA, listB)
CollectionUtils 交集
CollectionUtils.intersection(listA, listB)
CollectionUtils 交集的补集
CollectionUtils.disjunction(listA, listB)
CollectionUtils 差集
 CollectionUtils.subtract(listA, listB)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全栈程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值