OnJava8-Examples/generics/RandomList.java
2016-01-25 18:05:55 -08:00

26 lines
791 B
Java

// generics/RandomList.java
// (c)2016 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
import java.util.*;
import java.util.stream.*;
public class RandomList<T> extends ArrayList<T> {
private Random rand = new Random(47);
public T select() {
return get(rand.nextInt(size()));
}
public static void main(String[] args) {
RandomList<String> rs = new RandomList<>();
Arrays.stream(
("The quick brown fox jumped over " +
"the lazy brown dog").split(" "))
.forEach(rs::add);
IntStream.range(0, 11).forEach(i ->
System.out.print(rs.select() + " "));
}
}
/* Output:
brown over fox quick quick dog brown The brown lazy brown
*/