Repository: jtablesaw/tablesaw
Pull request #1228 (2023-08-18 09:57:25)

Fix isIn(Collection) (issue #1227)
Thanks for contributing.

- [X] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

Fixed issue #1227 where `isIn(Collection)` would not match column values unless they were given as Doubles.

## Testing

`NumberColumnTest.testIsInIntegerColumn()`
`NumberColumnTest.testIsInDoubleColumn()`



Patch:
diff --git a/core/src/main/java/tech/tablesaw/api/NumericColumn.java b/core/src/main/java/tech/tablesaw/api/NumericColumn.java
index fd483422..d0fa2870 100644
--- a/core/src/main/java/tech/tablesaw/api/NumericColumn.java
+++ b/core/src/main/java/tech/tablesaw/api/NumericColumn.java
@@ -5,6 +5,7 @@ import static tech.tablesaw.columns.numbers.NumberPredicates.isMissing;
 import static tech.tablesaw.columns.numbers.NumberPredicates.isNotMissing;
 
 import it.unimi.dsi.fastutil.doubles.DoubleComparator;
+import it.unimi.dsi.fastutil.doubles.DoubleOpenHashSet;
 import java.text.NumberFormat;
 import java.util.Collection;
 import java.util.Optional;
@@ -77,9 +78,11 @@ public interface NumericColumn<T extends Number>
   /** {@inheritDoc} */
   @Override
   default Selection isIn(Collection<Number> numbers) {
+    DoubleOpenHashSet doubleNumbers =
+        new DoubleOpenHashSet(numbers.stream().mapToDouble(Number::doubleValue).toArray());
     final Selection results = new BitmapBackedSelection();
     for (int i = 0; i < size(); i++) {
-      if (numbers.contains(getDouble(i))) {
+      if (doubleNumbers.contains(getDouble(i))) {
         results.add(i);
       }
     }


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java b/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java
index 4a580838..3b7d4fd0 100644
--- a/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/api/NumberColumnTest.java
@@ -33,6 +33,7 @@ import java.math.BigDecimal;
 import java.text.NumberFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Locale;
 import java.util.concurrent.TimeUnit;
@@ -203,21 +204,28 @@ public class NumberColumnTest {
   }
 
   @Test
-  public void testIsIn() {
-    Number[] originalValues = {32, 42, 40, 57, 52, -2};
-    Number[] resultValues = {10.0, -2.0, 57.0, -5.0};
-    List<Number> inValues = Arrays.asList(resultValues);
+  public void testIsInIntegerColumn() {
+    List<Number> inValues = Arrays.asList(new Number[] {10d, (short) -2, 57, 42L, 40f, 52d, -5d});
 
-    DoubleColumn initial = DoubleColumn.create("Test");
-    Table t = Table.create("t", initial);
+    Table t = Table.create("t", IntColumn.create("Test", 32, 42, 40, 57, 52, -2, 11, 25));
 
-    for (Number value : originalValues) {
-      initial.append(value);
-    }
+    Selection filter = t.numberColumn("Test").isIn(inValues);
+    Table result = t.where(filter);
+    assertEquals(
+        new HashSet<>(Arrays.asList(-2, 57, 42, 40, 52)), result.numberColumn("Test").asSet());
+  }
+
+  @Test
+  public void testIsInDoubleColumn() {
+    List<Number> inValues = Arrays.asList(new Number[] {10d, (short) -2, 57, 42L, 40f, 52d, -5d});
+
+    Table t =
+        Table.create("t", DoubleColumn.create("Test", 32d, 42d, 40d, 57d, 52d, -2d, 11d, 25d));
 
     Selection filter = t.numberColumn("Test").isIn(inValues);
     Table result = t.where(filter);
-    assertEquals(2, result.rowCount());
+    assertEquals(
+        new HashSet<>(Arrays.asList(-2d, 40d, 42d, 52d, 57d)), result.numberColumn("Test").asSet());
   }
 
   @Test