-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMultiIterableClass.java
More file actions
69 lines (57 loc) · 1.91 KB
/
MultiIterableClass.java
File metadata and controls
69 lines (57 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package holding;
/**
* RUN:
* javac holding/MultiIterableClass.java && java holding.MultiIterableClass
* OUTPUT:
* banana-shaped be to Earth the know we how is that And
* is banana-shaped Earth that how the be And we know to
* And that is how we know the Earth to be banana-shaped
*/
import java.util.*;
public class MultiIterableClass extends IterableClass {
public Iterable<String> reversed() {
return new Iterable<String>() {
public Iterator<String> iterator() {
return new Iterator<String>() {
int current = words.length - 1;
public boolean hasNext() {
return current > -1;
}
public String next() {
return words[current--];
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
};
}
public Iterable<String> randomized() {
return new Iterable<String>() {
public Iterator<String> iterator() {
List<String> shuffled = new ArrayList<String>(Arrays.asList(words));
Collections.shuffle(shuffled, new Random(47));
return shuffled.iterator();
}
};
}
public static void main(String[] args) {
MultiIterableClass mic = new MultiIterableClass();
// reversed iterator
for (String s : mic.reversed()) {
System.out.print(s + " ");
}
System.out.println();
// randomized iterator
for (String s : mic.randomized()) {
System.out.print(s + " ");
}
System.out.println();
// default iterator
for (String s : mic) {
System.out.print(s + " ");
}
System.out.println();
}
}