1 50 package org.apache.avalon.excalibur.collections; 51 52 import java.util.ArrayList ; 53 import java.util.Iterator ; 54 import java.util.List ; 55 56 66 public class ListUtils 67 { 68 public static List intersection( final List list1, final List list2 ) 69 { 70 final ArrayList result = new ArrayList (); 71 final Iterator iterator = list2.iterator(); 72 73 while( iterator.hasNext() ) 74 { 75 final Object o = iterator.next(); 76 77 if( list1.contains( o ) ) 78 { 79 result.add( o ); 80 } 81 } 82 83 return result; 84 } 85 86 public static List subtract( final List list1, final List list2 ) 87 { 88 final ArrayList result = new ArrayList ( list1 ); 89 final Iterator iterator = list2.iterator(); 90 91 while( iterator.hasNext() ) 92 { 93 result.remove( iterator.next() ); 94 } 95 96 return result; 97 } 98 99 public static List sum( final List list1, final List list2 ) 100 { 101 return subtract( union( list1, list2 ), 102 intersection( list1, list2 ) ); 103 } 104 105 public static List union( final List list1, final List list2 ) 106 { 107 final ArrayList result = new ArrayList ( list1 ); 108 109 final Iterator iterator = list2.iterator(); 110 while( iterator.hasNext() ) 111 { 112 final Object o = iterator.next(); 113 if( !result.contains( o ) ) 114 { 115 result.add( o ); 116 } 117 } 118 return result; 119 } 120 } 121 | Popular Tags |