java-faker icon indicating copy to clipboard operation
java-faker copied to clipboard

Idea: Random subset

Open wcarmon opened this issue 4 years ago • 4 comments

Is your feature request related to a problem? Please describe. Currently it's hard to get a Set, which is a random subset of a collection.

kotlin example of use case

val options = setOf(
  "cheese",
  "olives",
  "onion",
  "pepperoni",
  "peppers",
)

val selectedIngredients =
  options
    .shuffled()
    .take(ThreadLocalRandom.current().nextInt(options.size))
    .toSet()

java example

    List<String> options = Arrays.asList(
        "cheese",
        "olives",
        "onion",
        "pepperoni",
        "peppers");

    Collections.shuffle(options, ThreadLocalRandom.current());

    options
        .stream()
        .limit(ThreadLocalRandom.current().nextInt(options.size()))
        .collect(Collectors.toSet());

Describe the solution you'd like

faker.options.randomSubset("cheese", "olives", "bacon", "ham")

Describe alternatives you've considered see the alternative code samples above

Additional context Add any other context or screenshots about the feature request here.

wcarmon avatar Oct 08 '21 23:10 wcarmon

Hi, we will volunteer to work on this issue in October/November. For the project maintainer, please let us know if this is okay and if you want it fixed.

komminen avatar Oct 16 '21 18:10 komminen

Hey @komminen. Sure, I'm happy to contribute code.
I wasn't sure if you wanted the change to your API or not.

wcarmon avatar Oct 17 '21 00:10 wcarmon

I think the project is dead somehow?

joaquinjsb avatar Oct 29 '21 10:10 joaquinjsb

@wcarmon, @komminen , @joaquinjsb

may be a bit too late but anyway there is a port of java-faker to jdk8 with lots of improvements including subsets generation https://github.com/datafaker-net/datafaker/pull/129

snuyanzin avatar May 01 '22 06:05 snuyanzin