java 实现将传入的两个集合进行并,交,联集,差操作

 

       集合的操作是程序中很少用到的操作.也是常用到的数学方法.所以有必要进行一下记录,以备不时之需.也希望对查看的同行有所帮忙.请大家多多回复,我也好知道自己的不足.共同进步!

/**
 *
 * @description:实现将传入的两个集合进行并,交,联集,差操作
 * 
 * 
@author jackWu
 * Feb 5, 2007
 
*/


package  com.gzsdx.itsm.common.util;

import  java.util.Arrays;
import  java.util.Collection;
import  java.util.List;

import  org.apache.commons.collections.CollectionUtils;

import  com.gzsdx.itsm.common.listener.Manager;
import  com.gzsdx.itsm.common.log.LogWriter;

public   class  CollectionPower  {
    
/**
     * @class description: Feb 5, 2007
     
*/

    
private static final LogWriter log = Manager.log;
    
/**
     * 
@author jackWu
     * @description:实现并操作
     * Feb 5, 2007
     * 
@param arrayA
     * 
@param arrayB
     * 
@return
     
*/

    
public String collectionUnion(String[] arrayA, String[] arrayB) {
        String arrayC 
= "";
        
try {
            List a 
= Arrays.asList(arrayA);
            List b 
= Arrays.asList(arrayB);
            Collection union 
= CollectionUtils.union(a, b);
            arrayC
=union.toArray().toString();
        }
 catch (Exception ex) {
            log.info(
"集合并操作失败!info===============", ex.getMessage());
        }

        
return arrayC;
    }

    
/**
     * 
@author jackWu
     * @description:实现交操作
     * Feb 5, 2007
     * 
@param arrayA
     * 
@param arrayB
     * 
@return
     
*/

    
public String collectionIntersection(String[] arrayA, String[] arrayB) {
        String arrayC 
= "";
        
try {
            List a 
= Arrays.asList(arrayA);
            List b 
= Arrays.asList(arrayB);
            Collection intersection 
= CollectionUtils.intersection(a, b);
            arrayC
=intersection.toArray().toString();
        }
 catch (Exception ex) {
            log.info(
"集合并操作失败!info===============", ex.getMessage());
        }

        
return arrayC;
    }

    
/**
     * 
@author jackWu
     * @description:实现联集操作
     * Feb 5, 2007
     * 
@param arrayA
     * 
@param arrayB
     * 
@return
     
*/

    
public String collectionDisjunction(String[] arrayA, String[] arrayB) {
        String arrayC 
= "";
        
try {
            List a 
= Arrays.asList(arrayA);
            List b 
= Arrays.asList(arrayB);
            Collection disjunction 
= CollectionUtils.disjunction(a, b);
            arrayC
=disjunction.toArray().toString();
        }
 catch (Exception ex) {
            log.info(
"集合并操作失败!info===============", ex.getMessage());
        }

        
return arrayC;
    }

    
/**
     * 
@author jackWu
     * @description:实现差操作
     * Feb 5, 2007
     * 
@param arrayA
     * 
@param arrayB
     * 
@return
     
*/

    
public String collectionSubtract(String[] arrayA, String[] arrayB) {
        String arrayC 
= "";
        
try {
            List a 
= Arrays.asList(arrayA);
            List b 
= Arrays.asList(arrayB);
            Collection subtract 
= CollectionUtils.subtract(a, b);
            arrayC
=subtract.toArray().toString();
        }
 catch (Exception ex) {
            log.info(
"集合并操作失败!info===============", ex.getMessage());
        }

        
return arrayC;
    }

}

 
你可以使用`Collections.sort`方法来对List集合中的对象进行排序,同时通过自定义Comparator来指定排序规则。下面是一个示例代码: ```java import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; public class Main { public static void main(String[] args) { // 创建一个List集合并添加对象 List<Person> personList = new ArrayList<>(); personList.add(new Person("Alice", 25)); personList.add(new Person("Bob", 30)); personList.add(new Person("Charlie", 20)); // 使用Collections.sort方法进行排序,传入自定义的Comparator Collections.sort(personList, new Comparator<Person>() { @Override public int compare(Person p1, Person p2) { // 根据年龄进行升序排序 int ageCompare = Integer.compare(p1.getAge(), p2.getAge()); if (ageCompare != 0) { return ageCompare; } // 年龄相同则根据姓名进行升序排序 return p1.getName().compareTo(p2.getName()); } }); // 打印排序后的结果 for (Person person : personList) { System.out.println(person.getName() + " - " + person.getAge()); } } static class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } } ``` 在上面的代码中,我们创建了一个名为Person的简单类来表示集合中的对象。我们通过实现Comparator接口来定义排序规则,首先按照年龄升序排序,如果年龄相同再按照姓名升序排序。然后使用Collections.sort方法对List集合进行排序,并输出排序后的结果。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值