Skip to content

Commit

Permalink
fix issue where StringColumn asSet returns the underlying set from th…
Browse files Browse the repository at this point in the history
…e DictionaryMap (jtablesaw#862)
  • Loading branch information
lwhite1 authored Jan 20, 2021
1 parent a470556 commit 8bd0548
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -177,7 +178,7 @@ public int countOccurrences(String value) {
}

public Set<String> asSet() {
return categories();
return new HashSet<>(categories());
}

public int firstIndexOf(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -176,7 +177,7 @@ public int countOccurrences(String value) {
}

public Set<String> asSet() {
return categories();
return new HashSet<>(categories());
}

public int firstIndexOf(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -186,7 +187,7 @@ public int countOccurrences(String value) {
}

public Set<String> asSet() {
return categories();
return new HashSet<>(categories());
}

public int firstIndexOf(String value) {
Expand Down
16 changes: 15 additions & 1 deletion core/src/test/java/tech/tablesaw/api/StringColumnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,6 +74,15 @@ void testForNulls() {
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");
Expand Down

0 comments on commit 8bd0548

Please sign in to comment.