[Mulgara-svn] r1893 - trunk/src/jar/util/java/org/mulgara/util/functional

pag at mulgara.org pag at mulgara.org
Wed Jan 27 19:20:21 UTC 2010


Author: pag
Date: 2010-01-27 11:20:20 -0800 (Wed, 27 Jan 2010)
New Revision: 1893

Modified:
   trunk/src/jar/util/java/org/mulgara/util/functional/C.java
Log:
Added a new method to find the intersection between a Set and the contents of an array, and between a List and the contents of an array

Modified: trunk/src/jar/util/java/org/mulgara/util/functional/C.java
===================================================================
--- trunk/src/jar/util/java/org/mulgara/util/functional/C.java	2010-01-27 19:18:47 UTC (rev 1892)
+++ trunk/src/jar/util/java/org/mulgara/util/functional/C.java	2010-01-27 19:20:20 UTC (rev 1893)
@@ -18,10 +18,12 @@
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.ListIterator;
 import java.util.NoSuchElementException;
+import java.util.Set;
 
 /**
  * Generic Collections utility class.
@@ -265,6 +267,33 @@
   }
 
   /**
+   * Method to create an intersection of a list and an array. The order of the result will
+   * be the same as the order of the original list.
+   * @param list The list to intersect. This will be an ArrayList, no matter the source type.
+   * @param array The array to intersect against the list.
+   * @return A list containing elements that are in both list and array.
+   */
+  public static final <T> List<T> intersect(List<T> list, T[] array) {
+    HashSet<T> lookup = new HashSet<T>();
+    for (T e: array) lookup.add(e);
+    List<T> result = new ArrayList<T>();
+    for (T e: list) if (lookup.contains(e)) result.add(e);
+    return result;
+  }
+
+  /**
+   * Method to create an intersection of a set and an array.
+   * @param set The set to intersect. This will be a HashSet, no matter the source type.
+   * @param array The array to intersect against the set.
+   * @return A set containing elements that are in both set and array.
+   */
+  public static final <T> Set<T> intersect(Set<T> set, T[] array) {
+    Set<T> result = new HashSet<T>();
+    for (T e: array) if (set.contains(e)) result.add(e);
+    return result;
+  }
+
+  /**
    * Inserts an element into an ordered list in a given order.
    * @param <T> The type of the element to be inserted. Must be comparable on itself.
    * @param list The list to insert into. This must already be ordered in the same order as this insert.




More information about the Mulgara-svn mailing list