javaでiteratorイテレータを使って、 Listに格納されたBeanを抽出するサンプル

処理コード:

private Map<Class<?>, List<Bean>> registerDataSource = new HashMap<Class<?>, List<Bean>>();
public Iterator<Bean> getRegisterIterator() {
// 同等コード:
// List<List<Bean>> dataList =..
// for (List<Bean> subList : dataList){
// for (List<Bean> item : subList){
// ……
// }
// }

//
final Collection<List<Bean>> adapterList = this.registerDataSource.values();
final Iterator<List<Bean>> entIterator = adapterList.iterator();
return new Iterator<Bean>() {
private Iterator<Bean> regIterator = new ArrayList<Bean>(0).iterator();
public Bean next() {
while (true) {
if (this.regIterator.hasNext() == false) {
if (entIterator.hasNext() == false)
break;
this.regIterator = entIterator.next().iterator();
}
if (this.regIterator.hasNext())
break;
}
//
if (this.regIterator.hasNext() == false)
throw new NoSuchElementException();
return regIterator.next();
}
public boolean hasNext() {
if (entIterator.hasNext() == false && regIterator.hasNext() == false)
return false;
return true;
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}

Development

Posted by arkgame