Java Magazine, Sept/Oct 2018
ORACLE COM JAVAMAGAZINE SEPTEMBER OCTOBER 2018 90 fix this buffer size Of course the java util List returned from the readAllLines method implements Iterable therefore you can call forEach on it and print all lines Notice this forEach is a totally diferent method from the one on Stream but its external behavior is identical Given this you can determine that option C compiles successfully and is correct In option D the initial subexpression Files readAllLines p is the same as in option C Therefore this is an expression of List String type Because you can extract a Stream from a List option D is also correct Notice that the code in option D is definitely not optimal First it draws the entire file into memory which might be a problem with a large file Then it wraps a Stream around that List This approach takes extra effort uses the maximum memory possible for this file and then makes a Stream Going directly to the Stream would reduce memory use create simpler code and still support all the operations you might want to perform using the Stream API The Files list method shown in option E does not supply the contents of a file Instead it returns a listing of the contents of a directory The contents are provided in the form of a stream specifically Stream Path which makes this a very useful method too and the code will compile successfully although it throws an exception at runtime because the Path that is provided refers to a file not to directory Because of this option E is incorrect article Simon Roberts joined Sun Microsystems in time to teach Suns first Java classes in the UK He created the Sun Certified Java Programmer and Sun Certified Java Developer exams He also wrote several Java certification guides and is currently a freelance educator who publishes recorded and live video training through Pearson InformIT available direct and through the OReilly Safari Books Online service He remains involved with Oracles Java certification projects Mikalai Zaikin is a lead Java developer at IBA IT Park in Minsk Belarus During his career he has helped Oracle with development of Java certification exams and he has been a technical reviewer of several Java certification books including three editions of the famous Sun Certified Programmer for Java study guides by Kathy Sierra and Bert Bates
You must have JavaScript enabled to view digital editions.