-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathMaxIterator.java
84 lines (76 loc) · 1.79 KB
/
MaxIterator.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
/**
* Data-Structures-In-Java
* MaxIterator.java
*/
package com.deepak.data.structures.Iterators;
import java.util.Comparator;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* <br> Problem Statement :
*
* Implement a max iterator i.e. it always returns the next item in the list
* bigger than the last item returned.
*
* </br>
*
* @author Deepak
*/
public class MaxIterator<T> implements Iterator<T> {
/* Iterator and Comparator */
private final Iterator<T> iterator;
private final Comparator<T> comparator;
/* Variables to store last item and next item */
private T lastItem;
private T nextItem;
/**
* Constructor
*
* @param iterator
* @param comparator
*/
public MaxIterator(Iterator<T> iterator, Comparator<T> comparator) {
this.iterator = iterator;
this.comparator = comparator;
}
/**
* Method to check if next element exists
*/
@Override
public boolean hasNext() {
/* If next item is not null, return true */
if (nextItem != null) {
return true;
}
/* Else go through the collection and find the next bigger element */
while (nextItem == null && iterator.hasNext()) {
T item = iterator.next();
if (lastItem == null || comparator.compare(item, lastItem) > 0) {
nextItem = item;
}
}
/* Return true if exists */
return nextItem != null;
}
/**
* Method to find next element
*/
@Override
public T next() {
/* If next element doesn't exists, throw exception */
if (!hasNext()) {
throw new NoSuchElementException("No element found in collection!!");
}
/* Else update the last item found */
lastItem = nextItem;
nextItem = null;
return lastItem;
}
/**
* Method to remove the element
*/
@Override
public void remove() {
throw new UnsupportedOperationException("Remove not supported!!");
}
}