Iterate twice on values (MapReduce)

前端 未结 11 1112
轮回少年
轮回少年 2020-11-29 07:22

I receive an iterator as argument and I would like to iterate on values twice.

public void reduce(Pair key, Iterator          


        
11条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 08:22

    Reusing the given iterator, no.

    But you can save the values in an ArrayList when iterating through them in the first place and then iterating upon the constructed ArrayList, of course (or you can build it directly in the first place by using some fancy Collection methods and then iterating directly on the ArrayList twice. It's a matter of tastes).

    Anyway, are you sure passing an Iterator is a good thing in the first place? Iterators are used to do just a linear scan through the collection, this is why they don't expose a "rewind" method.

    You should pass something different, like a Collection or an Iterable, as already suggested in a different answer.

提交回复
热议问题