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:
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:
Any help would be appreciated!
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!