1 package org.columba.core.base; 17 18 import java.util.Collections ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.ListIterator ; 22 23 30 public class ListTools { 31 39 public static void intersect(List a, List b) { 40 ListIterator aIt; 41 ListIterator bIt; 42 43 if (a.size() == 0) { 44 return; 45 } 46 47 if (b.size() == 0) { 48 a.clear(); 49 50 return; 51 } 52 53 Collections.sort(a); 54 Collections.sort(b); 55 56 aIt = a.listIterator(); 57 bIt = b.listIterator(); 58 59 Comparable aVal; 60 Comparable bVal; 61 62 aVal = (Comparable ) aIt.next(); 63 bVal = (Comparable ) bIt.next(); 64 65 boolean loop = true; 66 int compareResult; 67 68 while (loop) { 69 compareResult = aVal.compareTo(bVal); 70 71 if (compareResult < 0) { aIt.remove(); 73 74 if (aIt.hasNext()) { 75 aVal = (Comparable ) aIt.next(); 76 } else { 77 return; 78 } 79 } else if (compareResult == 0) { 81 if (aIt.hasNext()) { 82 aVal = (Comparable ) aIt.next(); 83 } else { 84 loop = false; 85 86 return; 87 } 88 89 if (bIt.hasNext()) { 90 bVal = (Comparable ) bIt.next(); 91 } else { 92 loop = false; 93 aIt.remove(); 94 } 95 } else { 97 if (bIt.hasNext()) { 98 bVal = (Comparable ) bIt.next(); 99 } else { 100 loop = false; 101 aIt.remove(); 102 } 103 } 104 } 105 106 while (aIt.hasNext()) { 107 aIt.next(); 108 aIt.remove(); 109 } 110 } 111 112 120 public static void substract(List a, List b) { 121 ListIterator aIt; 122 ListIterator bIt; 123 124 if ((a.size() == 0) || (b.size() == 0)) { 125 return; 126 } 127 128 Collections.sort(a); 129 Collections.sort(b); 130 131 aIt = a.listIterator(); 132 bIt = b.listIterator(); 133 134 Comparable aVal; 135 Comparable bVal; 136 137 aVal = (Comparable ) aIt.next(); 138 bVal = (Comparable ) bIt.next(); 139 140 boolean loop = true; 141 int compareResult; 142 143 while (loop) { 144 compareResult = aVal.compareTo(bVal); 145 146 if (compareResult < 0) { 148 if (aIt.hasNext()) { 149 aVal = (Comparable ) aIt.next(); 150 } else { 151 return; 152 } 153 } else if (compareResult == 0) { aIt.remove(); 155 156 if (aIt.hasNext()) { 157 aVal = (Comparable ) aIt.next(); 158 } else { 159 return; 160 } 161 162 if (bIt.hasNext()) { 163 bVal = (Comparable ) bIt.next(); 164 } else { 165 return; 166 } 167 } else { 169 if (bIt.hasNext()) { 170 bVal = (Comparable ) bIt.next(); 171 } else { 172 return; 173 } 174 } 175 } 176 } 177 178 186 public static void intersect_astable(List a, List b) { 187 if (a.size() == 0) { 188 return; 189 } 190 191 if (b.size() == 0) { 192 a.clear(); 193 194 return; 195 } 196 197 Iterator ita = a.iterator(); 198 Iterator itb; 199 Object acta; 200 201 boolean found; 202 203 while (ita.hasNext()) { 204 acta = ita.next(); 205 itb = b.iterator(); 206 found = false; 207 208 while (itb.hasNext() && !found) { 209 found = acta.equals(itb.next()); 210 } 211 212 if (!found) { 213 ita.remove(); 214 } else { 215 itb.remove(); 216 } 217 } 218 } 219 } 220 | Popular Tags |