Add a Collection option#1880
Conversation
|
Hi there, first time contributor here! Please feel free ask any question. |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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();There was a problem hiding this comment.
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();There was a problem hiding this comment.
How about:
return collection.stream() .skip(faker.random().nextInt(collection.size())) .findFirst();
Would it randomize the output in case of an ordered collection ?
There was a problem hiding this comment.
Yes, similar to the other approaches it would return a random n th element.
@asolntsev any feedback, stream vs iterator?
There was a problem hiding this comment.
Oh yes it's not a filter but a skip. My bad!
There was a problem hiding this comment.
@kingthorin Sure, your solution with stream().skip() is more concise.
|
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
asolntsev
left a comment
There was a problem hiding this comment.
Approved - with one suggestion to improve performance.
There was a problem hiding this comment.
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();5e758be to
3549dd1
Compare
3549dd1 to
21b5596
Compare
|
That's on me, I did run spotless:apply but I may have still broken something after. |
|
You are too fast for me! If you prepare a v3, wouldn't it be the perfect time to harmonize the API by renaming |
|
We could. I'd suggest opening a new issue in which to discuss. I don't think anyone has brought it up before. |


Add a Collection option to address #1879