Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting Iterator types in Java

I have an Iterator<TypeA> that I want to convert to Iterator<TypeB>. TypeA and TypeB cannot be directly cast to each other but I can write a rule how to cast them. How can I accomplish this? Should I extend and override Iterator<TypeA>'s next, hasNext and remove methods?

like image 423
parsa Avatar asked Oct 08 '10 07:10

parsa


People also ask

What are the various iterator types in Java?

There are three types of iterators. Enumeration − Enumeration is initial iterators introduced in jdk 1.0 and is only for older collections like vector or hashTables. Enumeration can be used for forward navigation only. Element can not be removed using Enumeration.

How do I turn an iterable into a list?

To convert iterable to Collection, the iterable is first converted into spliterator. Then with the help of StreamSupport. stream(), the spliterator can be traversed and then collected with the help collect() into collection.


4 Answers

You don't need to write this yourself. Guava (formerly Google-collections) has you covered in Iterators.transform(...)

You provide your iterator and a function that converts TypeA to TypeB and you're done.

Iterator<TypeA> iterA = ....;
Iterator<TypeB> iterB = Iterators.transform(iterA, new Function<TypeA, TypeB>() {
   @Override
   public TypeB apply(TypeA input) {
     TypeB output = ...;// rules to create TypeB from TypeA
     return output;
   }
});
like image 112
GaryF Avatar answered Sep 22 '22 10:09

GaryF


Write an adapter. Here's an example in Java. OReilly's book 'Head First: Design Patterns' gives the best explanation on the topic.

like image 39
Boris Pavlović Avatar answered Sep 21 '22 10:09

Boris Pavlović


I guess you should write your own iterator(it can implements java.util.iterator) with your ((converting)) rule in a method and use it.

like image 39
Aboelnour Avatar answered Sep 23 '22 10:09

Aboelnour


You can use the following snippet :

public static void main(String... args){
    Iterator<TypeA> iteratorTypeA = methodToGetIteratorTypeA();
    Iterator<TypeB> iteratorTypeB = new IteratorConveter<TypeA, TypeB>(iteratorTypeA,
        new Converter<TypeA, TypeB>(){
            public TypeB convert(TypeA typeA){
                return null; //Something to convert typeA to type B
            }
        });
}

public class IteratorConveter<F, T> implements Iterator<T> {
    private final Converter<? super F, ? extends T> converter;
    private final Iterator<F> iterator;

    public IteratorConveter(Iterator<F> iterator, Converter<? super F, ? extends T> converter) {
        this.converter = converter;
        this.iterator = iterator;
    }

    public boolean hasNext() {
        return iterator.hasNext();
    }

    public T next() {
        return converter.convert(iterator.next());
    }

    public void remove() {
        iterator.remove();
    }
}


interface Converter<F, T> {
    T convert(F object);
}
like image 20
Colin Hebert Avatar answered Sep 20 '22 10:09

Colin Hebert