Resetting Iterable Object

谁说我不能喝 提交于 2020-01-06 08:16:10

问题


I am writing a MR job where in the reducer side, I need to check the size of Iterable before doing anything. Someone did asked the same question long back(How to find the size of an Iterable Object?) but the solutions given are not working.Since the Iterable doesnt have a size() method, please suggesst me how to do it. I tried the following options.

  1. Tried to get a iterator object from iterable, but got following typecast error. Same error with ResettableIterator. java.lang.ClassCastException: org.apache.hadoop.mapreduce.ReduceContext$ValueIterator cannot be cast to java.util.ListIterator

  2. If i iterate to the end of the Iterable object to get size, then How can I go back to the start of the index.

  3. if(values instanceof Collection){ return ((Collection)values).size(); } this doesnt get hit as the values is of different type.

Please help me with some sample code. This issue has really taken a lot of time. Thanks a lot.!!


回答1:


  1. Iterator iterator = Use values.iterator(); to get iterator.
  2. Store values in List or some other data structure and then iterate over it.


来源:https://stackoverflow.com/questions/17650119/resetting-iterable-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!