Skip to content

Add a Collection option#1880

Merged
kingthorin merged 1 commit into
datafaker-net:mainfrom
deluxe:feature/collection-option
Jul 16, 2026
Merged

Add a Collection option#1880
kingthorin merged 1 commit into
datafaker-net:mainfrom
deluxe:feature/collection-option

Conversation

@deluxe

@deluxe deluxe commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Add a Collection option to address #1879

@deluxe

deluxe commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

Hi there, first time contributor here! Please feel free ask any question.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I named this method nextElement to overload nextElement(List<E>) but both methods could be named option like others in this class (with nextElement(List<E>) kept for backward compatibility).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Generally it's totally fine.

But in case of huge collections, this is not the most effective solution. List.copyOf(collection) will create a copy of the whole collection every time.

A more effective solution would be using Iterator, something like this:

int index = faker.random().nextInt(collection.size());
Iterator<T> it = collection.iterator();
for (int i = 0; i < index; i++) {
  iterator.next();
}
return iterator.next();

@kingthorin kingthorin Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

How about:

// Use streams, skip to random location in collection, return the first item there
return collection.stream()
            .skip(faker.random().nextInt(collection.size()))
            .findFirst();

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

How about:

return collection.stream()
            .skip(faker.random().nextInt(collection.size()))
            .findFirst();

Would it randomize the output in case of an ordered collection ?

@kingthorin kingthorin Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yes, similar to the other approaches it would return a random n th element.

@asolntsev any feedback, stream vs iterator?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Oh yes it's not a filter but a skip. My bad!

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@kingthorin Sure, your solution with stream().skip() is more concise.

@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.61%. Comparing base (9da71f3) to head (6534166).
⚠️ Report is 12 commits behind head on main.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1880      +/-   ##
============================================
+ Coverage     92.48%   92.61%   +0.13%     
- Complexity     3562     3564       +2     
============================================
  Files           346      346              
  Lines          7048     7051       +3     
  Branches        685      678       -7     
============================================
+ Hits           6518     6530      +12     
+ Misses          366      362       -4     
+ Partials        164      159       -5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asolntsev asolntsev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Approved - with one suggestion to improve performance.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Generally it's totally fine.

But in case of huge collections, this is not the most effective solution. List.copyOf(collection) will create a copy of the whole collection every time.

A more effective solution would be using Iterator, something like this:

int index = faker.random().nextInt(collection.size());
Iterator<T> it = collection.iterator();
for (int i = 0; i < index; i++) {
  iterator.next();
}
return iterator.next();

@asolntsev asolntsev added this to the 3.0.0 milestone Jul 16, 2026
@asolntsev asolntsev linked an issue Jul 16, 2026 that may be closed by this pull request
@kingthorin
kingthorin force-pushed the feature/collection-option branch 2 times, most recently from 5e758be to 3549dd1 Compare July 16, 2026 15:19

@kingthorin kingthorin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks!

@kingthorin
kingthorin force-pushed the feature/collection-option branch from 3549dd1 to 21b5596 Compare July 16, 2026 15:19
@kingthorin
kingthorin merged commit b979949 into datafaker-net:main Jul 16, 2026

@asolntsev asolntsev left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@deluxe Something is wrong with formatting in this PR.

Look at indentation here:

Image

This is how usually formatting looks like in Java code:

Image

@kingthorin

Copy link
Copy Markdown
Collaborator

That's on me, I did run spotless:apply but I may have still broken something after.
Will check/fix.

@deluxe

deluxe commented Jul 16, 2026

Copy link
Copy Markdown
Contributor Author

You are too fast for me! If you prepare a v3, wouldn't it be the perfect time to harmonize the API by renaming Options.nextElement(...) to Options.option(...) ?

@kingthorin

Copy link
Copy Markdown
Collaborator

We could. I'd suggest opening a new issue in which to discuss. I don't think anyone has brought it up before.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add a Collection option

4 participants