OnJava8-Examples/collectionsindepth/CollectionDataTest.java

31 lines
996 B
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collectionsindepth/CollectionDataTest.java
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2015-06-15 17:47:35 -07:00
import java.util.*;
2015-11-03 12:00:44 -08:00
import java.util.function.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
2015-11-03 12:00:44 -08:00
class Government implements Supplier<String> {
2015-06-15 17:47:35 -07:00
String[] foundation = ("strange women lying in ponds " +
"distributing swords is no basis for a system of " +
"government").split(" ");
private int index;
@Override
2015-11-03 12:00:44 -08:00
public String get() { return foundation[index++]; }
2015-06-15 17:47:35 -07:00
}
public class CollectionDataTest {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<>(
new CollectionData<>(new Government(), 15));
// Using the convenience method:
set.addAll(CollectionData.list(new Government(), 15));
System.out.println(set);
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
[strange, women, lying, in, ponds, distributing, swords,
is, no, basis, for, a, system, of, government]
2015-09-07 11:44:36 -06:00
*/