Free Image Hosting:
http://image-host.developers-heaven.net
Share your images free!!!
/**
* Reports the number of seconds the computer spent
* reading 300,000 lines, via a Java 8 type Stream object,
* and using its forEach method,
* to then parsing out the 10 integers on each line,
* adding each integer, as parsed out, to a growing List < Integer >
*
* @version 2017-12-06
* @author dwz
*/
import java.nio.file.Paths;
import java.nio.file.Files;
;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
public class TimedReadStream {
static String FNAME = "MyRandomInts.txt";
static List < Integer > nums = new ArrayList <> ();
public static void main( String[] args ) {
long startTime = System.currentTimeMillis();
testItOut();
long endTime = System.currentTimeMillis();
System.out.println();
System.out.println( "Run time in seconds was: " + (endTime - startTime) / 1000.0 );
System.out.println( "There are " + nums.size() + " numbers in the List of nums." );
}
static void testItOut() {
try( Stream< String > myLines = Files.lines( Paths.get( FNAME ) ) ) {
myLines.forEach( line -> processLine( line) );
} catch( IOException ex ) {
System.out.println( ex );
}
}
static void processLine( String line ) {
String[] ary = line.split( " " );
try {
for( String val : ary )
nums.add( Integer.parseInt(val) );
} catch( Exception ex ) {
System.out.println( ex );
}
}
}
/**
* Reports the number of seconds the computer spent
* reading 300,000 lines, line by line,
* and then parsing out the 10 integers on each line
* adding each integer, as parsed out, to a growing List < Integer >
*
* @version 2017-12-06
* @author dwz
*/
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.charset.Charset;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class TimedReadLine {
static String FNAME = "MyRandomInts.txt";
static List < Integer > nums = new ArrayList <> ();
public static void main( String[] args ) {
long startTime = System.currentTimeMillis();
testItOut();
long endTime = System.currentTimeMillis();
System.out.println();
System.out.println( "Run time in seconds was: " + (endTime - startTime) / 1000.0 );
System.out.println( "There are " + nums.size() + " numbers in the List of nums." );
}
static void testItOut() {
try( BufferedReader fin = Files.newBufferedReader( Paths.get( FNAME ),
Charset.defaultCharset() ) ) {
String line = null;
while( ( line = fin.readLine() ) != null ) {
processLine( line );
}
} catch( IOException ex ) {
System.out.println( ex );
}
}
static void processLine( String line ) {
String[] ary = line.split( " " );
try {
for( String val : ary )
nums.add( Integer.parseInt(val) );
} catch( Exception ex ) {
System.out.println( ex );
}
}
}