Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limit in For-Each Loop

I'm a beginner in Java and a geomatics student.

I work with an XTF image, its works like TIFF. This image store about 20000 lines called pings with several informations : coordinates, start time, stop time... The treatments I use on Intellij become too heavy but it works well. I want to cut in two the informations stored in my XTF image : 1 image with the 10000 first pings and the other with the 20000 last pings. Later I would gather the two images. My question is simple : how, with a “for each” loop can I order a limit (<=10000) ? I stored the information in a csv file.

for (XtfPing ping : xtf.getPings())
        {
            writer.write( Double.toString( ping.x) );
            writer.write( "," );
            writer.write( Double.toString( ping.y) );
            writer.write( "\n" );
        }
        writer.close();
like image 911
Manimalis Avatar asked Sep 20 '25 10:09

Manimalis


1 Answers

I know this doesn't fit exactly the OP's situation, but maybe this will help someone else looking to implement an iterate limit using Java 8:

List<Object> myList = ...
List<Object> first10000 = myList.stream().limit(10000).collect(Collectors.toList());

for (Object obj : first10000) {
    // do stuff
}

or more simply:

List<Object> myList = ...
myList.stream().limit(1000).forEach(o -> {
    // do stuff
});
like image 156
Matt Avatar answered Sep 22 '25 23:09

Matt