Enerjy |
Discuss. |
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
#1
|
|||
|
|||
|
Hello,
I have another question for the forum. I've been working with Enerjy for several weeks now, and really like it. I've learned a lot, and have much cleaner code thanks to it's incessant nagging about my sloppy java habits. Unfortunately, there are still a few things that I'm struggling with. I have written a whole lot of classes that iterate over (VERY large - several 10's or 100's of Gigs) data files, which have to process these files row by row. As such, each class that interprets these files implements the Iterator class. Unfortunately, by using the iterator, I now trigger JAVA0254 all over the place, which complains that I should be using a for loop construct instead of the iterator interface. In general, that makes sense, but there's no way I'm going to try to pre-load 100's of Gigs of data to make the class Iterable. Am I missing something, or is this really an inappropriate time for the JAVA0254 warning to be triggered? (Not that I expect it to be able to differentiate between the objects I'm iterating over, but I figure this must be a relatively common situation for people dealing with large data files. How do other people deal with this?) Thanks! |
|
#2
|
|||
|
|||
|
Sorry - I just realized I posted this to the wrong folder.
If I don't get a reply here, I'll move it to the correct location later. |
|
#3
|
|||
|
|||
|
Hi,
I'm not sure I follow the bit about having to preload the file to make the class iterable. All you need to be iterable is have some class that implements the Iterable interface and returns one of your iterators. The runtime behavior would then be exactly the same, reading the file line by line. There are many things that baffle me about the JDK. Why the standard BufferedReader (or a Line-based derivative) isn't iterable over the lines is one. Why the enhanced for statement can't loop over Iterators is another. It already has two completely different behaviors to handle Iterables and arrays so would adding Iterators have been so hard? (And don't get me started on the amount of time the world has wasted due to the lack of File.copy) The way we often deal with the iterator problem is that we have a little utility class called IterableIterator that turns an iterator into an iterable that you can use with foreach. The class, shown below, is pretty trivial but it's very useful for exactly this situation. Mark Code:
public class IterableIterator<E> implements Iterable<E> {
private final Iterator<E> iterator;
/**
* Construct an iterable over the given iterator.
*
* @param iterator Iterator to adapt to iterable
*/
@SuppressWarnings("unchecked")
public IterableIterator(Iterator<?> iterator) {
this.iterator = (Iterator<E>)iterator;
}
@SuppressWarnings("unchecked")
public Iterator<E> iterator() {
return this.iterator;
}
}
|
|
#4
|
|||
|
|||
|
Thanks for the reply - I'll play with this a bit.
I was under the impression that to use an iterator in an extended for loop, you needed to be able to generate an array/vector/etc containing all of the values. Obviously this wouldn't work for a file in which you're reading line-by-line. Obviously I need to look into this more closely. Once again, thanks for the fantastic tool. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|