重置可迭代对象 [英] Resetting Iterable Object

查看:108
本文介绍了重置可迭代对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写MR作业,在减速器方面,我需要做任何事情之前检查Iterable的大小.很久以前有人问过同样的问题(如何来找到Iterable对象的大小?),但是给出的解决方案不起作用.由于Iterable没有size()方法,请建议我怎么做. 我尝试了以下选项.

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. 试图从iterable获取一个迭代器对象,但是得到了以下类型转换 错误.与ResettableIterator相同的错误. java.lang.ClassCastException: org.apache.hadoop.mapreduce.ReduceContext$ValueIterator cannot be cast to java.util.ListIterator

  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

如果我迭代到Iterable对象的末尾以获取大小,则 如何返回索引的开头.

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.

if(values instanceof Collection){ return ((Collection)values).size(); } 不会被击中,因为值的类型不同.

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. Iterator iterator = Use values.iterator();获取迭代器.
  2. 将值存储在List或其他数据结构中,然后对其进行迭代.
  1. Iterator iterator = Use values.iterator(); to get iterator.
  2. Store values in List or some other data structure and then iterate over it.

这篇关于重置可迭代对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆