Univocity: When using CsvRoutines to iterate beans, why can't I use iterator remove functionality?

时间:2016-07-11 19:07:25

标签: java iterator javabeans univocity

Given I have setup the following iterator (with domain being some class and input being some file):

BeanListProcessor<?> beanProcessor = new BeanListProcessor<Class<?>>((Class<Class<?>>) domain);
CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setRowProcessor(beanProcessor);
parserSettings.setHeaderExtractionEnabled(true);
parserSettings.setDelimiterDetectionEnabled(true);
CsvRoutines routines = new CsvRoutines(parserSettings);
Iterator<?> it = routines.iterate(domain, input).iterator();

Why can't I use...

while (it.hasNext()) {
    Object record = it.next();
    it.remove();                
} 

...to remove a bean?

In other words what is the reason for this implementation in the class com.univocity.parsers.common.routine.AbstractRoutines?

@Override
public void remove() {
    throw new UnsupportedOperationException("Can't remove beans");
}

I need to remove the "top" bean before iterating to the next bean.

1 个答案:

答案 0 :(得分:2)

我是图书馆的作者以及您无法将其删除的原因,因为这些bean不会保存在您可以随意操作的内存列表中。

这些bean在读取时从输入中解析,解析器无法控制输入。处理不需要的bean的唯一理智选择是忽略它:只需在你的循环上调用continue以继续下一个bean。

希望这有帮助