簡體   English   中英

如何從 stream 中獲取隨機對象

[英]How to get random objects from a stream

假設我有一個單詞列表,我想創建一個方法,它將新列表的大小作為參數並返回新列表。 我如何從我的原始 sourceList 中獲取隨機單詞?

public List<String> createList(int listSize) {
   Random rand = new Random();
   List<String> wordList = sourceWords.
      stream().
      limit(listSize).
      collect(Collectors.toList()); 

   return wordList;
}

那么如何以及在哪里可以使用我的 Random?

我找到了一個合適的解決方案。 Random 提供了一些方法來返回流。 例如 ints(size) 創建一個隨機整數流。

public List<String> createList(int listSize)
{
   Random rand = new Random();
   List<String> wordList = rand.
      ints(listSize, 0, sourceWords.size()).
      mapToObj(i -> sourceWords.get(i)).
      collect(Collectors.toList());

   return wordList;
}

我認為最優雅的方式是擁有一個特殊的收藏家。

我很確定你能保證每件物品被挑選的機會均等的唯一方法是收集、洗牌和重新播放。 這可以使用內置的 Collectors.collectingAndThen(...) 助手輕松完成。

通過隨機比較器或使用隨機減速器進行排序,就像在其他一些答案中建議的那樣,將導致非常有偏見的隨機性。

List<String> wordList = sourceWords.stream()
  .collect(Collectors.collectingAndThen(Collectors.toList(), collected -> {
      Collections.shuffle(collected);
      return collected.stream();
  }))
  .limit(listSize)
  .collect(Collectors.toList());

您可以將該改組收集器移動到輔助函數:

public class CollectorUtils {

    public static <T> Collector<T, ?, Stream<T>> toShuffledStream() {
        return Collectors.collectingAndThen(Collectors.toList(), collected -> {
            Collections.shuffle(collected);
            return collected.stream();
        });
    }

}

我假設您正在尋找一種與其他流處理功能很好地集成的方法。 因此,以下簡單的解決方案不是您要尋找的:)

Collections.shuffle(wordList)
return wordList.subList(0, limitSize)

這是我想出的一個解決方案,它似乎與所有其他解決方案不同,所以我想為什么不把它添加到一堆。

基本上,它的工作原理是在每次請求下一個元素時使用與Collections.shuffle一次迭代相同的技巧 - 選擇一個隨機元素,將該元素與列表中的第一個元素交換,向前移動指針。 也可以用指針從末尾開始倒數。

需要注意的是,它確實會改變您傳入的列表,但我想如果您不喜歡那樣,您可以將副本作為第一件事。 我們更感興趣的是減少冗余副本。

private static <T> Stream<T> randomStream(List<T> list)
{
    int characteristics = Spliterator.SIZED;
    // If you know your list is also unique / immutable / non-null
    //int characteristics = Spliterator.DISTINCT | Spliterator.IMMUTABLE | Spliterator.NONNULL | Spliterator.SIZED;
    Spliterator<T> spliterator = new Spliterators.AbstractSpliterator<T>(list.size(), characteristics)
    {
        private final Random random = new SecureRandom();
        private final int size = list.size();
        private int frontPointer = 0;

        @Override
        public boolean tryAdvance(Consumer<? super T> action)
        {
            if (frontPointer == size)
            {
                return false;
            }

            // Same logic as one iteration of Collections.shuffle, so people talking about it not being
            // fair randomness can take that up with the JDK project.
            int nextIndex = random.nextInt(size - frontPointer) + frontPointer;
            T nextItem = list.get(nextIndex);
            // Technically the value we end up putting into frontPointer
            // is never used again, but using swap anyway, for clarity.
            Collections.swap(list, nextIndex, frontPointer);

            frontPointer++;
            // All items from frontPointer onwards have not yet been chosen.

            action.accept(nextItem);
            return true;
        }
    };

    return StreamSupport.stream(spliterator, false);
}

這是我的單行解決方案:

 List<String> st = Arrays.asList("aaaa","bbbb","cccc");
 st.stream().sorted((o1, o2) -> RandomUtils.nextInt(0, 2)-1).findFirst().get();

RandomUtils 來自 commons lang 3

嘗試這樣的事情:

List<String> getSomeRandom(int size, List<String> sourceList) {
    List<String> copy = new ArrayList<String>(sourceList);
    Collections.shuffle(copy);
    List<String> result = new ArrayList<String>();
    for (int i = 0; i < size; i++) {
        result.add(copy.get(i));
    }

    return result;
}

如果您想要結果列表中的非重復項並且您的初始列表是不可變的:

  • 沒有直接的方法可以從當前的 Streams API 中獲取它。
  • 不可能使用隨機比較器,因為它會破壞比較契約。

您可以嘗試以下操作:

public List<String> getStringList(final List<String> strings, final int size) {
    if (size < 1 || size > strings.size()) {
        throw new IllegalArgumentException("Out of range size.");
    }

    final List<String> stringList = new ArrayList<>(size);

    for (int i = 0; i < size; i++) {
        getRandomString(strings, stringList)
                .ifPresent(stringList::add);
    }

    return stringList;
}

private Optional<String> getRandomString(final List<String> stringList, final List<String> excludeStringList) {
    final List<String> filteredStringList = stringList.stream()
            .filter(c -> !excludeStringList.contains(c))
            .collect(toList());

    if (filteredStringList.isEmpty()) {
        return Optional.empty();
    }

    final int randomIndex = new Random().nextInt(filteredStringList.size());
    return Optional.of(filteredStringList.get(randomIndex));
}

@kozla13 改進版:

List<String> st = Arrays.asList("aaaa","bbbb","cccc");
st.stream().min((o1, o2) -> o1 == o2 ? 0 : (ThreadLocalRandom.current().nextBoolean() ? -1 : 1)).orElseThrow();
  1. 使用java內置類ThreadLocalRandom
  2. nextInt 從序列 [-1, 0, 1] 生成一個,但在 compare func 中返回 0 意味着等於元素和 this 的副作用 - 在這種情況下將始終采用第一個元素 (o1)。
  3. 正確處理對象等於大小寫

如果源列表通常比新列表大得多,您可能會通過使用BitSet獲取隨機索引來提高效率:

List<String> createList3(int listSize, List<String> sourceList) {
  if (listSize > sourceList.size()) {
    throw new IllegalArgumentException("Not enough words in the source list.");
  }

  List<String> newWords = randomWords(listSize, sourceList);
  Collections.shuffle(newWords); // optional, for random order
  return newWords;
}

private List<String> randomWords(int listSize, List<String> sourceList) {
  int endExclusive = sourceList.size();
  BitSet indices = new BitSet(endExclusive);
  Random rand = new Random();
  while (indices.cardinality() < listSize) {
    indices.set(rand.nextInt(endExclusive));
  }
  
  return indices.stream().mapToObj(i -> sourceList.get(i))
    .collect(Collectors.toList());
}

stream 可能有點矯枉過正。 復制源列表,這樣你就不會產生副作用,然后返回一個隨機副本的子列表。

public static List<String> createList(int listSize, List<String> sourceList) {
  if (listSize > sourceList.size()) {
    throw IllegalArgumentException("Not enough words for new list.");
  }
  List<String> copy = new ArrayList<>(sourceList);
  Collections.shuffle(copy);
  return copy.subList(0, listSize);
}

答案很簡單(使用流):

List<String> a = src.stream().sorted((o1, o2) -> {
        if (o1.equals(o2)) return 0;
        return (r.nextBoolean()) ? 1 : -1;
    }).limit(10).collect(Collectors.toList());

你可以測試一下:

List<String> src = new ArrayList<String>();
for (int i = 0; i < 20; i++) {
    src.add(String.valueOf(i*10));
}
Random r = new Random();
List<String> a = src.stream().sorted((o1, o2) -> {
        if (o1.equals(o2)) return 0;
        return (r.nextBoolean()) ? 1 : -1;
    }).limit(10).collect(Collectors.toList());
System.out.println(a);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM