Skip to content

MINOR: Replace Collections and Arrays factory methods with Java 9+ equivalents in tools - #23344

Merged
m1a2st merged 3 commits into
apache:trunkfrom
sunm2n:minor-configcommand-set-of
Sep 4, 2026
Merged

MINOR: Replace Collections and Arrays factory methods with Java 9+ equivalents in tools#23344
m1a2st merged 3 commits into
apache:trunkfrom
sunm2n:minor-configcommand-set-of

Conversation

@sunm2n

@sunm2n sunm2n commented Sep 3, 2026

Copy link
Copy Markdown
Contributor

Replaces legacy collection factory methods in the tools module with
their Set.of() / List.of() / Map.of() equivalents, and removes imports
that become unused

Changed

  • ConfigCommand — Collections.singleton() → Set.of()
  • ConnectInternalTopicsTest — Collections.singleton() → Set.of()
  • DumpLogSegmentsTest — Collections.singletonList() → List.of(),
    Collections.emptyMap() → Map.of(), Arrays.asList() → List.of()
  • StreamsGroupCommandTest — Arrays.asList() → List.of()

Deliberately left unchanged

  • ConfigCommandTest#511 and ShareGroupCommandTest#265 —
    Collections.singletonMap() with a null value; Map.of() rejects nulls
  • Collections.nCopies(), disjoint(), emptyIterator() — no
    List.of()-style equivalent
  • java.util.Arrays imports in the two files above — still needed for
    Arrays.stream()

Verification

  • ./gradlew tools:test --tests ConfigCommandTest --tests
    DumpLogSegmentsTest --tests ConnectInternalTopicsTest --tests
    StreamsGroupCommandTest
  • ./gradlew tools:checkstyleMain tools:checkstyleTest
    tools:spotlessCheck

Reviewers: Ken Huang s7133700@gmail.com

ConfigCommand already uses Set.of() elsewhere in this file, for example
in the listConfigResources calls. This replaces the two remaining
Collections.singleton() usages for consistency, and removes the
java.util.Collections import that is no longer needed.

Set.of() rejects null elements, but both call sites pass a freshly
constructed object, so the replacement is safe here.

verified with `./gradlew tools:test --tests ConfigCommandTest`
@github-actions github-actions Bot added triage PRs from the community tools small Small PRs labels Sep 3, 2026

@m1a2st m1a2st left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch! Could you also clean up the tools test module? There are a few files there with the same pattern.

Replaces Collections.singleton(), singletonList() and emptyMap() with
Set.of(), List.of() and Map.of() in ConnectInternalTopicsTest and
DumpLogSegmentsTest, and removes the now-unused java.util.Collections
imports.

Two singletonMap() call sites are intentionally left as they are, since
they map a key to a null value and Map.of() does not accept null:
ConfigCommandTest#511 and ShareGroupCommandTest#265. Collections.nCopies(),
disjoint() and emptyIterator() are also untouched, as they have no
List.of()-style equivalent.

verified with `./gradlew tools:test --tests DumpLogSegmentsTest --tests ConnectInternalTopicsTest`
@sunm2n

sunm2n commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! I've pushed a second commit that cleans up the tools test module

ConnectInternalTopicsTest and DumpLogSegmentsTest are updated — Collections.singleton(), singletonList() and emptyMap() are replaced with Set.of(), List.of() and Map.of(), and the now-unused java.util.Collections imports are removed

I deliberately left a few call sites alone:

  • ConfigCommandTest#511 and ShareGroupCommandTest#265 use Collections.singletonMap() with a null value, and Map.of() rejects nulls
  • Collections.nCopies(), disjoint() and emptyIterator() have no List.of()-style equivalent

Let me know if you'd prefer a different scope

@github-actions github-actions Bot removed the triage PRs from the community label Sep 4, 2026

@m1a2st m1a2st left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update, we can also replace Arrays.asList() with List.of() in the following files:

  • DumpLogSegmentsTest
  • StreamsGroupCommandTest

And please update the PR title and description.

Replaces Arrays.asList() with List.of() in DumpLogSegmentsTest and
StreamsGroupCommandTest, as suggested in review.

The three call sites in DumpLogSegmentsTest only read through the
returned ListIterator (hasNext/next/previous), so an immutable list is
fine. The one in StreamsGroupCommandTest is immediately copied into an
ArrayList, so mutability of the source does not matter.

The java.util.Arrays imports are kept, since both files still use
Arrays.stream().

verified with `./gradlew tools:test --tests DumpLogSegmentsTest --tests StreamsGroupCommandTest`
@sunm2n sunm2n changed the title MINOR: Replace Collections.singleton() with Set.of() in ConfigCommand MINOR: Replace Collections and Arrays factory methods with Java 9+ equivalents in tools Sep 4, 2026
@sunm2n

sunm2n commented Sep 4, 2026

Copy link
Copy Markdown
Contributor Author

Done — Arrays.asList() is replaced with List.of() in both files, and I've updated the PR title and description to match the wider scope

One note: I kept the java.util.Arrays imports in both files, since they still use Arrays.stream()

@m1a2st m1a2st left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch, LGTM

@m1a2st
m1a2st merged commit 8f24a91 into apache:trunk Sep 4, 2026
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants