Skip to content

Commit 95567ef

Browse files
committed
containers
1 parent e19530c commit 95567ef

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

Containers/Trial.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import java.util.*;
2+
3+
interface Generator<T> {
4+
T next();
5+
}
6+
7+
class CollectionData<T> extends ArrayList<T> {
8+
public CollectionData(Generator<T> gen, int quantity) {
9+
for (int i = 0; i < quantity; i++)
10+
add(gen.next());
11+
}
12+
13+
// A generic convenience method:
14+
public static <T> CollectionData<T> list(Generator<T> gen, int quantity) {
15+
return new CollectionData<T>(gen, quantity);
16+
}
17+
}
18+
19+
class Government implements Generator<String> {
20+
String[] foundation = ("strange women lying in ponds " +
21+
"distributing swords is no basis for a system of " +
22+
"government").split(" ");
23+
private int index;
24+
25+
public String next() {
26+
return foundation[index++];
27+
}
28+
}
29+
30+
class CollectionDataTest {
31+
public static void main(String[] args) {
32+
Set<String> set = new LinkedHashSet<String>(
33+
new CollectionData<String>(new Government(), 15));
34+
// Using the convenience method:
35+
set.addAll(CollectionData.list(new Government(), 15));
36+
System.out.println(set);
37+
}
38+
}

0 commit comments

Comments
 (0)