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;
}
}