Mag*_*nus 0 java collections syntax java-stream
我是大学新手Java程序员.我今天发现了一些关于Java语法如何工作的概念.
public class testClass {
ArrayList <String> persons = new ArrayList <String> ();
public void run(){
Stream <String> personstream = persons.stream();
}}
Run Code Online (Sandbox Code Playgroud)
stream()
在ArrayList
类中找不到该方法,但它可能看起来好像在那里.当我将鼠标移到stream()
Eclipse中的-method上时,它表示它是Collections的一部分,但我stream()
在其在线文档中找不到任何方法.
stream()
如果它不是我从中调用它的类的一部分,为什么调用该方法呢?
你检查了正确的类和Java版本吗?Java 8 Collection
(非Collections
)有一个stream()
默认方法,它由ArrayList
以下方法继承:
/**
* 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
*/
default Stream<E> stream() {
return StreamSupport.stream(spliterator(), false);
}
Run Code Online (Sandbox Code Playgroud)