2016-12-30 22:22:39 -08:00
|
|
|
// collectiontopics/SuppliersCollectionTest.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2016-07-05 14:46:09 -06:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2016-07-05 14:46:09 -06:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.function.*;
|
|
|
|
import java.util.stream.*;
|
|
|
|
import onjava.*;
|
|
|
|
|
|
|
|
class Government implements Supplier<String> {
|
|
|
|
static String[] foundation = (
|
2017-01-22 16:48:11 -08:00
|
|
|
"strange women lying in ponds " +
|
|
|
|
"distributing swords is no basis " +
|
|
|
|
"for a system of government").split(" ");
|
2016-07-05 14:46:09 -06:00
|
|
|
private int index;
|
2021-01-31 15:42:31 -07:00
|
|
|
@Override public String get() {
|
2017-01-22 16:48:11 -08:00
|
|
|
return foundation[index++];
|
|
|
|
}
|
2016-07-05 14:46:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
public class SuppliersCollectionTest {
|
|
|
|
public static void main(String[] args) {
|
2017-01-22 16:48:11 -08:00
|
|
|
// Suppliers class from the Generics chapter:
|
2016-07-05 14:46:09 -06:00
|
|
|
Set<String> set = Suppliers.create(
|
|
|
|
LinkedHashSet::new, new Government(), 15);
|
|
|
|
System.out.println(set);
|
|
|
|
List<String> list = Suppliers.create(
|
|
|
|
LinkedList::new, new Government(), 15);
|
|
|
|
System.out.println(list);
|
|
|
|
list = new ArrayList<>();
|
|
|
|
Suppliers.fill(list, new Government(), 15);
|
|
|
|
System.out.println(list);
|
|
|
|
|
2016-10-22 13:35:19 -07:00
|
|
|
// Or we can use Streams:
|
2017-01-22 16:48:11 -08:00
|
|
|
set = Arrays.stream(Government.foundation)
|
|
|
|
.collect(Collectors.toSet());
|
2016-07-05 14:46:09 -06:00
|
|
|
System.out.println(set);
|
2017-01-22 16:48:11 -08:00
|
|
|
list = Arrays.stream(Government.foundation)
|
|
|
|
.collect(Collectors.toList());
|
2016-07-05 14:46:09 -06:00
|
|
|
System.out.println(list);
|
2017-01-22 16:48:11 -08:00
|
|
|
list = Arrays.stream(Government.foundation)
|
|
|
|
.collect(Collectors
|
|
|
|
.toCollection(LinkedList::new));
|
2016-07-05 14:46:09 -06:00
|
|
|
System.out.println(list);
|
2017-01-22 16:48:11 -08:00
|
|
|
set = Arrays.stream(Government.foundation)
|
|
|
|
.collect(Collectors
|
|
|
|
.toCollection(LinkedHashSet::new));
|
2016-07-05 14:46:09 -06:00
|
|
|
System.out.println(set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
[strange, women, lying, in, ponds, distributing,
|
|
|
|
swords, is, no, basis, for, a, system, of, government]
|
|
|
|
[strange, women, lying, in, ponds, distributing,
|
|
|
|
swords, is, no, basis, for, a, system, of, government]
|
|
|
|
[strange, women, lying, in, ponds, distributing,
|
|
|
|
swords, is, no, basis, for, a, system, of, government]
|
|
|
|
[ponds, no, a, in, swords, for, is, basis, strange,
|
|
|
|
system, government, distributing, of, women, lying]
|
|
|
|
[strange, women, lying, in, ponds, distributing,
|
|
|
|
swords, is, no, basis, for, a, system, of, government]
|
|
|
|
[strange, women, lying, in, ponds, distributing,
|
|
|
|
swords, is, no, basis, for, a, system, of, government]
|
|
|
|
[strange, women, lying, in, ponds, distributing,
|
|
|
|
swords, is, no, basis, for, a, system, of, government]
|
2016-07-05 14:46:09 -06:00
|
|
|
*/
|