Skip to content

Commit 2dc18fa

Browse files
author
Deepak Malik
committed
Max Iterator
1 parent fb30f25 commit 2dc18fa

File tree

4 files changed

+128
-9
lines changed

4 files changed

+128
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Implementation of Algorithms in Java, Most of these names are picked up from Gee
3434
- [X] [Hopping Iterator](../master/src/com/deepak/algorithms/Iterators/HoppingIterator.java)
3535
- [ ] InOrder Iterator
3636
- [X] [LevelOrder Iterator](../master/src/com/deepak/algorithms/Iterators/LevelOrderIterator.java)
37-
- [ ] MaxIterator Iterator
37+
- [X] [MaxIterator Iterator](../master/src/com/deepak/algorithms/Iterators/MaxIterator.java)
3838
- [ ] MultiIterator Iterator
3939
- [ ] Peek Iterator
4040
- [ ] PreOrder Iterator

src/com/deepak/algorithms/Iterators/HoppingIterator.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,6 @@
2525
* If the original iterator returns: [1, 2, 3, 4, 5] in order, then the hopping
2626
* iterator will return [1, 5] in order when the hop value is 3.
2727
*
28-
* Methods expected to be implemented:
29-
*
30-
* public class HoppingIterator<T> implements Iterator<T> {
31-
* public HoppingIterator(Iterator<T> iterator, int numHops) {...}
32-
* public boolean hasNext() {...}
33-
* public T next() {...}
34-
* }
35-
*
3628
* </br>
3729
*
3830
* @author Deepak
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* Algorithms-In-Java
3+
* MaxIterator.java
4+
*/
5+
package com.deepak.algorithms.Iterators;
6+
7+
import java.util.Comparator;
8+
import java.util.Iterator;
9+
import java.util.NoSuchElementException;
10+
11+
/**
12+
* <br> Problem Statement :
13+
*
14+
* Implement a max iterator i.e. it always returns the next item in the list
15+
* bigger than the last item returned.
16+
*
17+
* </br>
18+
*
19+
* @author Deepak
20+
*/
21+
public class MaxIterator<T> implements Iterator<T> {
22+
23+
/* Iterator and Comparator */
24+
private final Iterator<T> iterator;
25+
private final Comparator<T> comparator;
26+
/* Variables to store last item and next item */
27+
private T lastItem;
28+
private T nextItem;
29+
30+
/**
31+
* Constructor
32+
*
33+
* @param iterator
34+
* @param comparator
35+
*/
36+
public MaxIterator(Iterator<T> iterator, Comparator<T> comparator) {
37+
this.iterator = iterator;
38+
this.comparator = comparator;
39+
}
40+
41+
/**
42+
* Method to check if next element exists
43+
*/
44+
@Override
45+
public boolean hasNext() {
46+
/* If next item is not null, return true */
47+
if (nextItem != null) {
48+
return true;
49+
}
50+
/* Else go through the collection and find the next bigger element */
51+
while (nextItem == null && iterator.hasNext()) {
52+
T item = iterator.next();
53+
if (lastItem == null || comparator.compare(item, lastItem) > 0) {
54+
nextItem = item;
55+
}
56+
}
57+
/* Return true if exists */
58+
return nextItem != null;
59+
}
60+
61+
/**
62+
* Method to find next element
63+
*/
64+
@Override
65+
public T next() {
66+
/* If next element doesn't exists, throw exception */
67+
if (!hasNext()) {
68+
throw new NoSuchElementException("No element found in collection!!");
69+
}
70+
/* Else update the last item found */
71+
lastItem = nextItem;
72+
nextItem = null;
73+
return lastItem;
74+
}
75+
76+
/**
77+
* Method to remove the element
78+
*/
79+
@Override
80+
public void remove() {
81+
throw new UnsupportedOperationException("Remove not supported!!");
82+
}
83+
84+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Algorithms-in-Java
3+
* MaxIterator_Test.java
4+
*/
5+
package com.deepak.algorithms.Iterators;
6+
7+
import java.util.Arrays;
8+
import java.util.Comparator;
9+
import java.util.List;
10+
import java.util.NoSuchElementException;
11+
12+
import org.junit.Assert;
13+
import org.junit.Test;
14+
15+
/**
16+
* Test cases for max iterator
17+
*
18+
* @author Deepak
19+
*/
20+
public class MaxIterator_Test {
21+
22+
/**
23+
* Test case to validate max iterator
24+
*/
25+
@Test(expected=NoSuchElementException.class)
26+
public void testMaxIterator() {
27+
List<Integer> integerList = Arrays.asList(2, 1, 4, 9, 5);
28+
MaxIterator<Integer> maxIterator = new MaxIterator<>(integerList.iterator(),
29+
new Comparator<Integer>() {
30+
@Override
31+
public int compare(Integer a, Integer b) {
32+
return a - b;
33+
}
34+
});
35+
Assert.assertTrue(maxIterator.hasNext());
36+
Assert.assertTrue(maxIterator.next() == 2);
37+
Assert.assertTrue(maxIterator.next() == 4);
38+
Assert.assertTrue(maxIterator.next() == 9);
39+
Assert.assertFalse(maxIterator.hasNext());
40+
Assert.assertTrue(maxIterator.next() == 9);
41+
}
42+
43+
}

0 commit comments

Comments
 (0)