4

Are there interfaces in Java library, which also enumerates some series, but follows slightly another logic than Iterator and Enumeration? Namely they should return boolean on next(), saying whether next element was reached, and should have getCurrent() method, which returns current element?

UPDATE

Iterator.next() is not equivalent of IEnumerator.Current since former will advance iterator on each call while latter won't.

UPDATE 2

I am designing my own class with my own functionality. My question was in order to find a "competent" analogy. The sample from C# was just a sample, I am not translating something from C# to Java.

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • You could subclass the `Iterator` and rename `hasNext()` to `next()` and `next()` to `getCurrent()` –  Mar 21 '13 at 22:06
  • @a_horse_with_no_name: getCurrent() could, I guess, return the same object several time, without going forward like next() does. – JB Nizet Mar 21 '13 at 22:08
  • @a_horse_with_no_name, do you know `IEnumerator` interface contract from `C#`? – Suzan Cioc Mar 21 '13 at 22:09
  • @Suzan. Why would you write `C#` in `Java`? – Alexander Pogrebnyak Mar 21 '13 at 22:11
  • 3
    This sounds sort of like Guava's [`PeekingIterator`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/PeekingIterator.html). – Louis Wasserman Mar 21 '13 at 22:11
  • @LouisWasserman the only difference would be the `Reset` method that moves the iterator before to the first position of the collection, but maybe OP doesn't need it. – Luiggi Mendoza Mar 21 '13 at 22:17
  • @LouisWasserman pls post an answer; if no other matches I will chose yours – Suzan Cioc Mar 21 '13 at 22:36
  • If you expect to have the classes in each language all behave *exactly* the same, down the the smallest level of behavior, you're going to be in for one rough conversion. You need to be able to understand that the two languages each have a class clearly capable of serving the same purpose, but that the API is a little bit different. Just learn to get used to it. – Servy Mar 21 '13 at 23:31
  • Suzan, did you ever make this Enumerator/Enumerable interface for Java? I need the exact same thing, a complete Enumerator/Enumerable with combinators etc. – experquisite Nov 06 '14 at 23:33

3 Answers3

3

This sounds like Guava's PeekingIterator; you can decorate a plain Iterator with Iterators.peekingIterator.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
1

You have to use a different approach in Java. e.g., instead of this C# code:

Dictionary<int?, int?> m = new Dictionary<int?, int?>();
for (IEnumerator<KeyValuePair<int?, int?>> it = m.GetEnumerator(); it.MoveNext();)
{
    Console.Write(it.Current.Key);
    Console.Write(it.Current.Value);
}

You will need to use:

java.util.HashMap<Integer, Integer> m = new java.util.HashMap<Integer, Integer>();
for (java.util.Iterator<java.util.Map.Entry<Integer, Integer>> it = m.entrySet().iterator(); it.hasNext();)
{
    java.util.Map.Entry<Integer, Integer> current = it.next();
    System.out.print(current.getKey());
    System.out.print(current.getValue());
}

There should not be a high demand for this particular conversion since you would normally use a 'foreach' loop in C#, which would convert more cleanly to Java.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
-1

If you use a standard concrete Collection class, such as HashSet and ArrayList to name but two, you will have access to an iterator.

Calling the method: collection.hasNext() will return a boolean, but not advance the pointer. This will allow you to determine whether you should attempt to read collection.next().

Example:

Set<String> numbers = new HashSet<>();

// fill in set...

while (numbers.hasNext()) {
    System.out.println(numbers.next());
}

Of course, you can also iterate through a collection using the for-each syntax:

for (String s : numbers) {
    System.out.println(s)
}
Redandwhite
  • 2,529
  • 4
  • 25
  • 43