Retrieving a random item from ArrayList
anyItem
is a method and the System.out.println
call is after your return statement so that won't compile anyway since it is unreachable.
Might want to re-write it like:
import java.util.ArrayList;
import java.util.Random;
public class Catalogue
{
private Random randomGenerator;
private ArrayList<Item> catalogue;
public Catalogue()
{
catalogue = new ArrayList<Item>();
randomGenerator = new Random();
}
public Item anyItem()
{
int index = randomGenerator.nextInt(catalogue.size());
Item item = catalogue.get(index);
System.out.println("Managers choice this week" + item + "our recommendation to you");
return item;
}
}
How to pick a random item from a arraylist in java?
Simple, put your code in a method like this
Random rand; // Global variable
public static int randomItem(Arraylist<Integer> mylist) {
rand = new Random();
Integer randomInt = lista.get(rand.nextInt(lista.size()));
return randomInt;
}
and call it like this in your main method;
int selected = randomItem(mylist);
System.out.println(selected);
Select Random element from ArrayList print then remove It
Fast and simple, make use of Collections.shuffle
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
list.add("d");
list.add("e");
while (!list.isEmpty()) {
// You don't need to do this on each iteration
// but this is going to give you a really
// random result
Collections.shuffle(list);
String value = list.remove(0);
System.out.println(value);
}
}
}
First run...
e
a
c
d
b
Second run...
a
e
d
b
c
Obviously, each run will give different results
Take n random elements from a ListE?
Two main ways.
Use
Random#nextInt(int)
:List<Foo> list = createItSomehow();
Random random = new Random();
Foo foo = list.get(random.nextInt(list.size()));It's however not guaranteed that successive
n
calls returns unique elements.Use
Collections#shuffle()
:List<Foo> list = createItSomehow();
Collections.shuffle(list);
Foo foo = list.get(0);It enables you to get
n
unique elements by an incremented index (assuming that the list itself contains unique elements).
In case you're wondering if there's a Java 8 Stream approach; no, there isn't a built-in one. There's no such thing as Comparator#randomOrder()
in standard API (yet?). You could try something like below while still satisfying the strict Comparator
contract (although the distribution is pretty terrible):
List<Foo> list = createItSomehow();
int random = new Random().nextInt();
Foo foo = list.stream().sorted(Comparator.comparingInt(o -> System.identityHashCode(o) ^ random)).findFirst().get();
Better use Collections#shuffle()
instead.
Related Topics
Java Date - Insert into Database
How to Get a Defaulttablemodel Object's Data into a Subclass of Defaulttablemodel
How to Create a Sub Array from Another Array in Java
Create a Autocompleting Textbox in Java with a Dropdown List
Sending Mail Attachment Using Java
Getting Max Value from an Arraylist of Objects
A Java API to Generate Java Source Files
How to Install the Jdk on Ubuntu Linux
What Does It Mean: the Serializable Class Does Not Declare a Static Final Serialversionuid Field
Make Arraylist.Toarray() Return More Specific Types
Why Can't a Generic Type Parameter Have a Lower Bound in Java