Common methods in Stream _count (termination method)

Counting the number: count:
common methods in the Stream stream _count: used to count the number of elements in the Stream stream
Long cont(); The
count method is a termination method, and the return value is an integer of type Long,
so it cannot be called anymore Other methods in
the Stream stream. This method returns a long value representing the number of elements (no longer an int value like the old collection).

Example:

public class Demo05Stream {
    
    
    public static void main(String[] args) {
    
    
        //获取一个Stream流
        Stream<String> stream = Stream.of("1", "2", "3", "4");
        long count = stream.count();
        System.out.println(count);

    }
}

Program demonstration:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44664432/article/details/109155018