int size = myHashSet.size();
int item = new Random().nextInt(size); // In real life, the Random object should be rather more shared than this
int i = 0;
for(Object obj : myhashSet)
{
if (i == item)
return obj;
i++;
}
More Related Contents:
- Obtaining a powerset of a set in Java
- Take n random elements from a List?
- Iteratively compute the Cartesian product of an arbitrary number of sets
- How to generate a random permutation in Java?
- Randomly Generate Letters According to their Frequency of Use?
- best way to pick a random subset from a collection?
- Generate Random Numbers in Array [duplicate]
- Random Number Algorithm – Implementation in Java
- Graph theory puzzle about connectivity?
- How to check if an integer is in a set of given ranges? [closed]
- Algorithm to divide a given number in two proportions by one of the component of that number
- What’s the best way to build a string of delimited items in Java?
- Efficient swapping of elements of an array in Java
- Java: random long number in 0
- What is the best way get the symmetric difference between two sets in java?
- Why use inheritance at all? [closed]
- java indexof(String str) method complexity [duplicate]
- A better algorithm to find the next palindrome of a number string
- Removing items from a collection in java while iterating over it
- Anagram algorithm in java
- Any implementation of Ordered Set in Java?
- JDK9 randomization on immutable sets and maps
- How do I get the intersection between two arrays as a new array?
- ‘MergeSort Algorithm’ – What’s the better implementation in JAVA? [closed]
- Generating shuffled range using a PRNG rather than shuffling
- Should I use composite primary keys or not?
- Finding the shortest path nodes with breadth first search
- Mergesort in java
- Random over ThreadLocalRandom
- Time complexity of System.arraycopy(…)?