Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一组独特的元素(没有两个相同的元素)。我想从集合中提取 N 个随机和不同的元素。在 Java 中最简单的方法是什么?
Set<MyObject> mySet = getTheSetFromSomeWhere(); List<MyObject> myObjects = new ArrayList<MyObject>(mySet); Collections.shuffle(myObjects); myObjects = myObjects.subList(0, n);