java.util.stream.Stream interface common method

 

Operation flow model is very rich, here are some common API. These methods may be divided into two:

  1. Delay method

    The return value type is still the Stream interfaces own type of method, therefore supports chained calls. (In addition to the end of the method, the other methods are delaying.)

  2. Termination method

    The return value type is no longer Stream Interface own type of method is no longer supported similar StringBuilder kind of chained calls. Termination method presented here include count and forEach method.

 

Processed one by one: forEach

Although the method called forEach, but the for loop "for-each" different nickname.

void forEach(Consumer<? super T> action);

The method receives a Consumer Interface function, each stream will function element to the process.

java.util.function.Consumer <T> interface is a consumer interface. 
Consumer interfaces include an abstract method void accept (T t), are intended consumption data is designated as a generic.

Basic use:

import java.util.stream.Stream;

public class Demo06ForEach {

    public static void main(String[] args) {
        Stream<String> stream = Stream.of("Java", "C", "Python", "Hadoop", "Spark");
        stream.forEach(name-> System.out.println(name));
    }

}

Here the call of () method, the method signature:

@SafeVarargs
public static<T> Stream<T> of(T... values) {
    return Arrays.stream(values);
}

 

Filter: filter

By filter method to a subset of the stream to another stream. Method signature:

Stream<T> filter(Predicate<? super T> predicate);

The interface receives a Predicate functional interface parameters (or may be a method reference Lambda) as the filter conditions.

 

java.util.stream.Predicate the interface is a function interface, which is only abstract methods:

boolean test(T t);

The method will generate a boolean result, on behalf of the specified condition is satisfied. If the result is true, then the Stream flow filter method will retain elements; if the result is false, then the filter element method will be discarded.

 

Basic use

Stream stream of code filter using a method substantially as:

import java.util.stream.Stream;

public class Demo07StreamFilter {
    public static void main(String[] args) {
        Stream<String> original = Stream.of("Java", "C", "Python", "Hadoop", "Spark");
        Stream<String> result = original.filter(s -> s.length() >= 5);
        result.forEach(System.out::println);
    }
}

Run the program, the console output:

Python
Hadoop
Spark

In this example, the Lambda expressions to specify the filtering conditions: length of the string is greater than or equal to 5.

 

Map: map

If desired the flow stream mapped to another element may be used map method. Method signature:

<R> Stream<R> map(Function<? super T, ? extends R> mapper);

The interface requires a Function interface function parameters, type T can convert the current data stream to another stream R type.

 

java.util.stream.Function interface is a function interface, which is only abstract methods:

R apply(T t);

This may be converted into a type of a T-R types, and this conversion operation, called "mapping."

 

Basic use

Stream stream of code map using the method substantially as:

import java.util.stream.Stream;

public class Demo08StreamMap {
    public static void main(String[] args) {
        Stream<String> original = Stream.of("100", "200", "300");
        Stream<Integer> result = original.map(str -> Integer.parseInt(str));
        result.forEach(System.out::println);
    }
}

Run the program, the console output:

100
200
300

In this code, the parameter map method by reference to a method to convert a string becomes an int type (and automatic packing of the Integer class of object). The method then calls the Stream forEach interfaces, output one by one.

 

Statistics Number: count

As the old collection Collection among size method as stream provides count number of elements of a method to count the number of them:

long count();

This method returns a long value representing the number of elements (that is not set as the old int value).

 

Basic use

import java.util.stream.Stream;

public class Demo09StreamCount {
    public static void main(String[] args) {
        Stream<String> original = Stream.of("Java", "C", "Python", "Hadoop", "Spark");
        Stream<String> result = original.filter(s -> s.length() >= 5);
        System.out.println(result.count());
    }
}

Run the program, the console output:

3

 

The first few access: limit

The method can be taken convection limit, access only the first n. Method signature:

Stream<T> limit(long maxSize);

Is a long type parameter, if the length is greater than the current set of parameters it is truncated; otherwise, no operation is performed.

 

Basic use

import java.util.stream.Stream;

public class Demo10StreamLimit {
    public static void main(String[] args) {
        Stream<String> original = Stream.of("Java", "C", "Python", "Hadoop", "Spark");
        Stream<String> result = original.limit(3);
        result.forEach(System.out::println);
    }
}

Run the program, the console output:

Java 
C 
Python

 

Skip the first few: skip

If you want to skip the first few elements, you can skip a new method to obtain a stream taken after:

Stream<T> skip(long n);

If the flow is greater than the current length of n, n a skip before; otherwise 0 will be a length of blank flow.

 

Basic use

import java.util.stream.Stream;

public class Demo11StreamSkip {
    public static void main(String[] args) {
        Stream<String> original = Stream.of("Java", "C", "Python", "Hadoop", "Spark");
        Stream<String> result = original.skip(3);
        result.forEach(System.out::println);
    }
}

Run the program, the console output:

Hadoop
Spark

 

Combination: concat

If there are two streams, want to merge into a stream, you can use Stream interface static method concat:

static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b)
Note: This is a static method, and the method concat java.lang.String which is different.

 

Basic use

import java.util.stream.Stream;

public class Demo12StreamConcat {
    public static void main(String[] args) {
        Stream<String> original1 = Stream.of("Java", "C", "Python");
        Stream<String> original2 = Stream.of("Hadoop", "Spark");
        Stream<String> result = Stream.concat(original1, original2);
        result.forEach(System.out::println);
    }
}

Run the program, the console output:

Java
C
Python
Hadoop
Spark

 

Stream interface commonly used method described here, in order to deepen understanding, can look simple way to complete the exercise Stream .

 

          

Guess you like

Origin www.cnblogs.com/liyihua/p/12289676.html