1.stream.forEach() 与 collection.forEach()
Although there are iterative methods, but the results are completely different.
Such as:
List<String> strl=Arrays.asList("aaa","bb","c","wwww","hh");Stream.of(strl).forEach(System.out::println);------------------------------------------------------>①
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
strl.stream().forEach(System.out::println);-------------------------------------------------------->②
System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^");
strl.forEach(System.out::println);----------------------------------------------------------------->③Output:[aaa, bb, c, hh, wwww]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aaa
bb
c
hh
wwww
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
aaa
bb
c
hh
wwww
Although the idea of suggesting ② line can be abbreviated form of the third row, but
①② in forEach () points to Stream interface and ③ in forEach () points to the Iterable interface.
View seen:
① of () method returns with one element of the Stream Stream Interface
Returns a sequential {@code Stream} containing a single element.
② In the .Stream () method returns an iterator used after divided to division Stream
/**
* Returns a sequential {@code Stream} with this collection as its source.
*
* <p>This method should be overridden when the {@link #spliterator()}
* method cannot return a spliterator that is {@code IMMUTABLE},
* {@code CONCURRENT}, or <em>late-binding</em>. (See {@link #spliterator()}
* for details.)
*
* @implSpec
* The default implementation creates a sequential {@code Stream} from the
* collection's {@code Spliterator}.
*
* @return a sequential {@code Stream} over the elements in this collection
* @since 1.8
*/
Therefore, the results obtained ① and ② internal iteration is not the same.