Repository: jtablesaw/tablesaw
Pull request #862 (2021-01-20 14:43:36)

fix issue #856 where StringColumn asSet returns the underlying set from th…
…e DictionaryMap

Thanks for contributing.

- [x] Tick to sign-off your agreement to the [Developer Certificate of Origin (DCO) 1.1](https://developercertificate.org)

## Description

What was changed

## Testing

Did you add a unit test?
yes



Patch:
diff --git a/core/src/main/java/tech/tablesaw/columns/strings/ByteDictionaryMap.java b/core/src/main/java/tech/tablesaw/columns/strings/ByteDictionaryMap.java
index 32d490f4..12cab27a 100644
--- a/core/src/main/java/tech/tablesaw/columns/strings/ByteDictionaryMap.java
+++ b/core/src/main/java/tech/tablesaw/columns/strings/ByteDictionaryMap.java
@@ -17,6 +17,7 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -177,7 +178,7 @@ public class ByteDictionaryMap implements DictionaryMap {
   }
 
   public Set<String> asSet() {
-    return categories();
+    return new HashSet<>(categories());
   }
 
   public int firstIndexOf(String value) {
diff --git a/core/src/main/java/tech/tablesaw/columns/strings/IntDictionaryMap.java b/core/src/main/java/tech/tablesaw/columns/strings/IntDictionaryMap.java
index c507f701..90c167a4 100644
--- a/core/src/main/java/tech/tablesaw/columns/strings/IntDictionaryMap.java
+++ b/core/src/main/java/tech/tablesaw/columns/strings/IntDictionaryMap.java
@@ -17,6 +17,7 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -176,7 +177,7 @@ public class IntDictionaryMap implements DictionaryMap {
   }
 
   public Set<String> asSet() {
-    return categories();
+    return new HashSet<>(categories());
   }
 
   public int firstIndexOf(String value) {
diff --git a/core/src/main/java/tech/tablesaw/columns/strings/ShortDictionaryMap.java b/core/src/main/java/tech/tablesaw/columns/strings/ShortDictionaryMap.java
index 43991245..6a29e785 100644
--- a/core/src/main/java/tech/tablesaw/columns/strings/ShortDictionaryMap.java
+++ b/core/src/main/java/tech/tablesaw/columns/strings/ShortDictionaryMap.java
@@ -17,6 +17,7 @@ import java.nio.ByteBuffer;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Comparator;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -186,7 +187,7 @@ public class ShortDictionaryMap implements DictionaryMap {
   }
 
   public Set<String> asSet() {
-    return categories();
+    return new HashSet<>(categories());
   }
 
   public int firstIndexOf(String value) {


Test patch:
diff --git a/core/src/test/java/tech/tablesaw/api/StringColumnTest.java b/core/src/test/java/tech/tablesaw/api/StringColumnTest.java
index 3a237154..7f1e22d5 100644
--- a/core/src/test/java/tech/tablesaw/api/StringColumnTest.java
+++ b/core/src/test/java/tech/tablesaw/api/StringColumnTest.java
@@ -14,11 +14,16 @@
 
 package tech.tablesaw.api;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 import static tech.tablesaw.columns.strings.StringPredicates.isEqualToIgnoringCase;
 
 import java.util.Arrays;
 import java.util.List;
+import java.util.Set;
 import java.util.function.Function;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -69,6 +74,15 @@ class StringColumnTest {
     assertEquals("", table2.stringColumn("id").get(3));
   }
 
+  /** Test that asSet returns a different set each time. */
+  @Test
+  void asSet() {
+    Set<String> set1 = column.asSet();
+    Set<String> set2 = column.asSet();
+    set1.remove("Value 2");
+    assertNotEquals(set1.size(), set2.size());
+  }
+
   @Test
   void testAppendObj() {
     StringColumn column = StringColumn.create("testing");