Skip to content

MINOR: Cleanup simplify set initialization with Set.of #19925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jun 11, 2025

Conversation

Yunyung
Copy link
Collaborator

@Yunyung Yunyung commented Jun 8, 2025

Simplify Set initialization and reduce the overhead of creating extra
collections.

The changes mostly include:

  • new HashSet<>(List.of(...))
  • new HashSet<>(Arrays.asList(...)) / new HashSet<>(asList(...))
  • new HashSet<>(Collections.singletonList()) / new
    HashSet<>(singletonList())
  • new HashSet<>(Collections.emptyList())
  • new HashSet<>(Set.of())

This change takes the following into account, and we will not change to
Set.of in these scenarios:

  • Require mutability (UnsupportedOperationException).
  • Allow duplicate elements (IllegalArgumentException).
  • Allow null elements (NullPointerException).
  • Depend on Ordering. Set.of does not guarantee order, so it could
    make tests flaky or break public interfaces.

Reviewers: Ken Huang s7133700@gmail.com, PoAn Yang
payang@apache.org, Chia-Ping Tsai chia7712@gmail.com

@github-actions github-actions bot added triage PRs from the community streams core Kafka Broker producer consumer tools connect kraft mirror-maker-2 storage Pull requests that target the storage module KIP-932 Queues for Kafka clients labels Jun 8, 2025
@Yunyung
Copy link
Collaborator Author

Yunyung commented Jun 8, 2025

private static final Set<TopicPartition> INITIAL_ASSIGNMENT =
new HashSet<>(Arrays.asList(TOPIC_PARTITION, TOPIC_PARTITION2));

Keep the above line unchanged to avoid flaky in the verify(...) below:

verify(sinkTask).close(new ArrayList<>(workerCurrentOffsets.keySet()));

@Yunyung Yunyung changed the title MINOR: Cleanup simplify set initialization with Set.of MINOR: Cleanup simplify set initialization with Set.of (WIP) Jun 8, 2025
@@ -893,8 +893,8 @@ public void testAssignEmptyMetadata(final Map<String, Object> parameterizedConfi
// then metadata gets populated
assignments = partitionAssignor.assign(metadata, new GroupSubscription(subscriptions)).groupAssignment();
// check assigned partitions
assertEquals(Set.of(new HashSet<>(List.of(t1p0, t2p0, t1p0, t2p0, t1p1, t2p1, t1p2, t2p2))),
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also remove unnecessary duplicates t1p0, t2p0.

@Yunyung Yunyung changed the title MINOR: Cleanup simplify set initialization with Set.of (WIP) MINOR: Cleanup simplify set initialization with Set.of Jun 8, 2025
Copy link
Member

@FrankYang0529 FrankYang0529 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR.

Could you also change new HashSet<>(Collections.singletonList()) to Set.of()?

@github-actions github-actions bot removed the triage PRs from the community label Jun 9, 2025
@Yunyung Yunyung changed the title MINOR: Cleanup simplify set initialization with Set.of MINOR: Cleanup set initialization with Set.of Jun 9, 2025
Copy link
Collaborator

@m1a2st m1a2st left a comment

Choose a reason for hiding this comment

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

Thank @Yunyung for this patch, left a comment.

  • ClassicGroupTest L423 also can update

@Yunyung Yunyung changed the title MINOR: Cleanup set initialization with Set.of MINOR: Cleanup simplify set initialization with Set.of Jun 9, 2025
@Yunyung
Copy link
Collaborator Author

Yunyung commented Jun 9, 2025

Thanks @FrankYang0529 and @m1a2st for the review. Update according to the comments.

@chia7712 chia7712 merged commit 2e96856 into apache:trunk Jun 11, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants