集合的操作(并集、交集、交集的补集、差价)

import java.util.Arrays;   
import java.util.Collection;   
import java.util.Collections;   
import java.util.List;   
     
import org.apache.commons.collections.CollectionUtils;   
import org.apache.commons.lang.ArrayUtils;   
     
public class CollectionUtilsIntro { 
 @SuppressWarnings("unchecked")   
 public static void main(String[] args) {   
  String[] arrayA = new String[] { "1", "2", "3", "3", "4", "5" };   
  String[] arrayB = new String[] { "3", "4", "4", "5", "6", "7" };   
     
  List a = Arrays.asList(arrayA);   
  List b = Arrays.asList(arrayB);   
  //并集   
  Collection union = CollectionUtils.union(a, b);   
  //交集   
  Collection intersection = CollectionUtils.intersection(a, b);   
  //交集的补集   
  Collection disjunction = CollectionUtils.disjunction(a, b);   
  //集合相减   
  Collection subtract = CollectionUtils.subtract(a, b);   
     
  Collections.sort((List) union);   
  Collections.sort((List) intersection);   
  Collections.sort((List) disjunction);   
  Collections.sort((List) subtract);   
     
  System.out.println("A: " + ArrayUtils.toString(a.toArray()));   
  System.out.println("B: " + ArrayUtils.toString(b.toArray()));   
  System.out.println("--------------------------------------------");   
  System.out.println("Union(A, B): " + ArrayUtils.toString(union.toArray()));   
  System.out.println("Intersection(A, B): " + ArrayUtils.toString(intersection.toArray()));   
  System.out.println("Disjunction(A, B): " + ArrayUtils.toString(disjunction.toArray()));   
  System.out.println("Subtract(A, B): " + ArrayUtils.toString(subtract.toArray()));   
 }   
}

转自:https://blog.csdn.net/xuehuagongzi000/article/details/53448122

你可能感兴趣的:(java)