-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathHoppingIterator.java
105 lines (97 loc) · 2.6 KB
/
HoppingIterator.java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/**
* Data-Structures-In-Java
* HoppingIterator.java
*/
package com.deepak.data.structures.Iterators;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* <br> Problem Statement :
*
* Implement an iterator that hops specified number of times and then returns the next
* element after the hop. Note: the iterator always returns the first element as
* it is, and starts hopping only after the first element.
*
* Examples:
*
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
* iterator will return [1, 3, 5] in order when the hop value is 1.
*
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
* iterator will return [1, 4] in order when the hop value is 2.
*
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
* iterator will return [1, 5] in order when the hop value is 3.
*
* </br>
*
* @author Deepak
*/
public class HoppingIterator<T> implements Iterator<T> {
/* Iterator */
private final Iterator<T> iterator;
/* Variable to keep track of next item */
private T nextItem;
/* Number of hops needed */
private final int numOfHops;
/* Flag to check if it is first element */
private boolean first;
/**
* Constructor
*
* @param iterator
* @param numOfHops
*/
public HoppingIterator(Iterator<T> iterator, int numOfHops) {
if (numOfHops < 0) {
throw new IllegalArgumentException("Invalid value for number of hops!!");
}
this.iterator = iterator;
this.numOfHops = numOfHops;
nextItem = null;
first = true;
}
/**
* Method to check of next element exists
*/
@Override
public boolean hasNext() {
/* If we already have next item, return true */
if (nextItem != null) {
return true;
}
/* If it is not first element, move till the next hop */
if (!first) {
for (int hop = 0; hop < numOfHops && iterator.hasNext(); hop++) {
iterator.next();
}
}
/* Now, if next element exits. move there and update first flag */
if (iterator.hasNext()) {
nextItem = iterator.next();
first = false;
}
return nextItem != null;
}
/**
* Method to find next element in collection
*/
@Override
public T next() {
/* If no next element exits, collection has finished */
if (!hasNext()) {
throw new NoSuchElementException("No Element left in collection!!");
}
/* Else, find the next item, return it and set next item to null */
T itemToReturn = nextItem;
nextItem = null;
return itemToReturn;
}
/**
* Method to remove the element
*/
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not supported!!");
}
}