Ned help converting nested for(each) loop to Iterator "pattern"

Status
Not open for further replies.
I need help with an algorithm and my brain is fried.

I'm trying to create an iterator to replace a nested for(each) loop that looks like:

Code:
List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();

for (Assembly a : dao.getAssemblies()) {

  List<Part> parts = a.getPartsList();

  if (parts != null) {

    for (Part p : parts) {

      data.add(toMap(p));

    }

  }

}

I'm running into memory/performance issues so I can't have the original source of data side-by-side with a full-copy of my 'translated'(?) data structure. Also, the logic of the overall application will make more sense if I can pick off one item at a time.

I'm trying to implement an iterator which emits a single Map<String, Object> 'row' for each Part of each Assembly. What I currently have is way way WAY over programmed/engineered to even bother with posting here (and it may taint any help you want to give me.) Most importantly it doesn't work -- either I get NullPointerExceptions or I go into an infinite loop. I think I've been thinking about it too hard and/or looking at this too long to unlock my brain.

I think my primary logic problem that I have is that a given assembly may not yet have a parts list built for it either (a) because it is old and is no longer used or (b) it is new and is not yet complete.

Iterator interface is:

Code:
interface Iterator<E> {

  /** 
   * hasNext() returns true if there are items left in the iterator, false otherwise
   */
  boolean hasNext();

  /**
   * get the next element from the collection
   * throws NoSuchElementException if you've gone past the end of the iterator
  E next(); 

  /** 
   *  delete the 'next' item from the collection;
   *  throws NoSuchElementException if you've gone past the end of the iterator
   *  throws UnsupportedOperationException if the iterator/collection doesn't support the remove operation
   */
  void remove();

}

Any help would be appreciated!
 
LordHunter317":16hol2ed said:
You haven't given us enough context to solve the problem, because I don't see how or why your issue isn't solved by just replacing:
Code:
data.add(toMap(p))
with your actual processing logic. More importantly, I don't see what a custom iterator could possibly get you except maybe letting you hide one level of looping.

The 'toMap(Object)' method simply takes an object that meets the JavaBeans specification and creates a Property->Value Map from it.

data.add() adds this map as 'row' or 'record' in the list named data.

In my case, I'm trying not to create a secondary full List<Map<String, Object>> data structure parallel to the one the DAO is already providing. The processing I'm doing is per 'row'/'record' and doesn't require me to have the full dataset in memory (let alone, two.)

In my original code I did build the entire dataset out as in the original code only because it was easier to convert the whole thing at once with the above loop. But due to memory problems which cause performance problems (garbage collection), it would be better to 'visit' each 'record' once and only once, do whatever processing needs to be done on that record, and press on ... again, no reason to have two full datasets in memory.
 
Status
Not open for further replies.