Skip to content

Commit 6b71b22

Browse files
LuciferYangdongjoon-hyun
authored andcommitted
[SPARK-53235][CORE][TESTS] Use Java Set.of instead of ImmutableSet.of
### What changes were proposed in this pull request? This PR aims to use Java 9+ `Set.of` API instead of `ImmutableSet.of` API. ### Why are the changes needed? Java's native API is **shorter and faster**. ```scala scala> spark.version val res0: String = 4.1.0-preview1 scala> spark.time((1 until 100_000_000).foreach(_ => java.util.Set.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))) Time taken: 4287 ms scala> spark.time((1 until 100_000_000).foreach(_ => com.google.common.collect.ImmutableSet.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10))) Time taken: 15024 ms ``` ### Does this PR introduce _any_ user-facing change? No behavior change. ### How was this patch tested? Pass the CIs. ### Was this patch authored or co-authored using generative AI tooling? No. Closes #51961 from LuciferYang/SPARK-53235. Authored-by: yangjie01 <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 5444354 commit 6b71b22

File tree

6 files changed

+24
-16
lines changed

6 files changed

+24
-16
lines changed

common/kvstore/src/test/java/org/apache/spark/util/kvstore/InMemoryStoreSuite.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@
1717

1818
package org.apache.spark.util.kvstore;
1919

20+
import java.util.Set;
2021
import java.util.NoSuchElementException;
2122

22-
import com.google.common.collect.ImmutableSet;
2323
import org.junit.jupiter.api.Test;
2424
import static org.junit.jupiter.api.Assertions.*;
2525

@@ -147,25 +147,25 @@ public void testRemoveAll() throws Exception {
147147
assertFalse(store.removeAllByIndexValues(
148148
ArrayKeyIndexType.class,
149149
KVIndex.NATURAL_INDEX_NAME,
150-
ImmutableSet.of(new int[] {10, 10, 10}, new int[] { 3, 3, 3 })));
150+
Set.of(new int[] {10, 10, 10}, new int[] { 3, 3, 3 })));
151151
assertEquals(9, store.count(ArrayKeyIndexType.class));
152152

153153
assertTrue(store.removeAllByIndexValues(
154154
ArrayKeyIndexType.class,
155155
KVIndex.NATURAL_INDEX_NAME,
156-
ImmutableSet.of(new int[] {0, 0, 0}, new int[] { 2, 2, 2 })));
156+
Set.of(new int[] {0, 0, 0}, new int[] { 2, 2, 2 })));
157157
assertEquals(7, store.count(ArrayKeyIndexType.class));
158158

159159
assertTrue(store.removeAllByIndexValues(
160160
ArrayKeyIndexType.class,
161161
"id",
162-
ImmutableSet.of(new String [] { "things" })));
162+
Set.<String[]>of(new String [] { "things" })));
163163
assertEquals(4, store.count(ArrayKeyIndexType.class));
164164

165165
assertTrue(store.removeAllByIndexValues(
166166
ArrayKeyIndexType.class,
167167
"id",
168-
ImmutableSet.of(new String [] { "more things" })));
168+
Set.<String[]>of(new String [] { "more things" })));
169169
assertEquals(0, store.count(ArrayKeyIndexType.class));
170170
}
171171

common/kvstore/src/test/java/org/apache/spark/util/kvstore/LevelDBSuite.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import java.util.Iterator;
2626
import java.util.List;
2727
import java.util.NoSuchElementException;
28+
import java.util.Set;
2829
import java.util.Spliterators;
2930
import java.util.stream.Collectors;
3031
import java.util.stream.StreamSupport;
3132

32-
import com.google.common.collect.ImmutableSet;
3333
import org.iq80.leveldb.DBIterator;
3434
import org.junit.jupiter.api.AfterEach;
3535
import org.junit.jupiter.api.BeforeEach;
@@ -220,19 +220,19 @@ public void testRemoveAll() throws Exception {
220220
db.removeAllByIndexValues(
221221
ArrayKeyIndexType.class,
222222
KVIndex.NATURAL_INDEX_NAME,
223-
ImmutableSet.of(new int[] {0, 0, 0}, new int[] { 2, 2, 2 }));
223+
Set.of(new int[] {0, 0, 0}, new int[] { 2, 2, 2 }));
224224
assertEquals(7, db.count(ArrayKeyIndexType.class));
225225

226226
db.removeAllByIndexValues(
227227
ArrayKeyIndexType.class,
228228
"id",
229-
ImmutableSet.of(new String[] { "things" }));
229+
Set.<String[]>of(new String[] { "things" }));
230230
assertEquals(4, db.count(ArrayKeyIndexType.class));
231231

232232
db.removeAllByIndexValues(
233233
ArrayKeyIndexType.class,
234234
"id",
235-
ImmutableSet.of(new String[] { "more things" }));
235+
Set.<String[]>of(new String[] { "more things" }));
236236
assertEquals(0, db.count(ArrayKeyIndexType.class));
237237
}
238238

common/kvstore/src/test/java/org/apache/spark/util/kvstore/RocksDBSuite.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
import java.util.Iterator;
2626
import java.util.List;
2727
import java.util.NoSuchElementException;
28+
import java.util.Set;
2829
import java.util.Spliterators;
2930
import java.util.stream.Collectors;
3031
import java.util.stream.StreamSupport;
3132

32-
import com.google.common.collect.ImmutableSet;
3333
import org.junit.jupiter.api.AfterEach;
3434
import org.junit.jupiter.api.BeforeEach;
3535
import org.junit.jupiter.api.Test;
@@ -217,19 +217,19 @@ public void testRemoveAll() throws Exception {
217217
db.removeAllByIndexValues(
218218
ArrayKeyIndexType.class,
219219
KVIndex.NATURAL_INDEX_NAME,
220-
ImmutableSet.of(new int[] {0, 0, 0}, new int[] { 2, 2, 2 }));
220+
Set.of(new int[] {0, 0, 0}, new int[] { 2, 2, 2 }));
221221
assertEquals(7, db.count(ArrayKeyIndexType.class));
222222

223223
db.removeAllByIndexValues(
224224
ArrayKeyIndexType.class,
225225
"id",
226-
ImmutableSet.of(new String[] { "things" }));
226+
Set.<String[]>of(new String[] { "things" }));
227227
assertEquals(4, db.count(ArrayKeyIndexType.class));
228228

229229
db.removeAllByIndexValues(
230230
ArrayKeyIndexType.class,
231231
"id",
232-
ImmutableSet.of(new String[] { "more things" }));
232+
Set.<String[]>of(new String[] { "more things" }));
233233
assertEquals(0, db.count(ArrayKeyIndexType.class));
234234
}
235235

common/network-shuffle/src/test/java/org/apache/spark/network/shuffle/CleanupNonShuffleServiceServedFilesSuite.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import java.util.concurrent.atomic.AtomicBoolean;
2626
import java.util.stream.Collectors;
2727

28-
import com.google.common.collect.ImmutableSet;
2928
import org.junit.jupiter.api.Test;
3029

3130
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -43,10 +42,10 @@ public class CleanupNonShuffleServiceServedFilesSuite {
4342
private static final String SORT_MANAGER = "org.apache.spark.shuffle.sort.SortShuffleManager";
4443

4544
private static Set<String> expectedShuffleFilesToKeep =
46-
ImmutableSet.of("shuffle_782_450_0.index", "shuffle_782_450_0.data");
45+
Set.of("shuffle_782_450_0.index", "shuffle_782_450_0.data");
4746

4847
private static Set<String> expectedShuffleAndRddFilesToKeep =
49-
ImmutableSet.of("shuffle_782_450_0.index", "shuffle_782_450_0.data", "rdd_12_34");
48+
Set.of("shuffle_782_450_0.index", "shuffle_782_450_0.data", "rdd_12_34");
5049

5150
private TransportConf getConf(boolean isFetchRddEnabled) {
5251
return new TransportConf(

dev/checkstyle.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,10 @@
264264
<property name="format" value="ImmutableMap\.of"/>
265265
<property name="message" value="Use Map.of instead." />
266266
</module>
267+
<module name="RegexpSinglelineJava">
268+
<property name="format" value="ImmutableSet\.of"/>
269+
<property name="message" value="Use Set.of instead." />
270+
</module>
267271
<!-- support structured logging -->
268272
<module name="RegexpSinglelineJava">
269273
<property name="format" value="org\.slf4j\.(Logger|LoggerFactory)" />

scalastyle-config.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,11 @@ This file is divided into 3 sections:
792792
<customMessage>Use Map.copyOf instead.</customMessage>
793793
</check>
794794

795+
<check customId="ImmutableSetof" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
796+
<parameters><parameter name="regex">\bImmutableSet\.of\b</parameter></parameters>
797+
<customMessage>Use java.util.Set.of instead.</customMessage>
798+
</check>
799+
795800
<check customId="maputils" level="error" class="org.scalastyle.file.RegexChecker" enabled="true">
796801
<parameters><parameter name="regex">org\.apache\.commons\.collections4\.MapUtils\b</parameter></parameters>
797802
<customMessage>Use org.apache.spark.util.collection.Utils instead.</customMessage>

0 commit comments

Comments
 (0)