Stream <T>
.
operatorimport java.util.ArrayList;
import java.util.Arrays;
public class StreamBasic {
//Data source preparation
public static void main(String[] args) {
var list = new ArrayList<String>(Arrays.asList("ant", "elephant", "hamster"));
/*Processing by stream
Stream generation from a pre-prepared list
ArrayList<String>Because it is based on
stream method is also Stream<String>Returns an object
*/
list.
stream().
/*Intermediate processing
Extract 3 or more characters with the filter method and convert to uppercase with map
The return value is Stream<T>
*/
filter(s -> s.length() > 3).
map(String::toUpperCase).
/*Termination
System the value given by the forEach method.out
*/
forEach(System.out::println);//ELEPHANT HAMSTER
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class StreamBasic2 {
//list
public static void main(String[] args) {
var list = new ArrayList<String>(Arrays.asList("ant", "elephant", "hamster"));
list.stream().forEach(System.out::println);
// list.parallelStream().forEach(System.out::println);
System.out.println("---------------");
//Array
var data = new String[] { "rose", "Morning glory", "Tulips" };
Arrays.stream(data).forEach(System.out::println);
System.out.println("---------------");
//map
var map = new HashMap<String, String>(
Map.of("orange", "Mandarin orange", "apple", "Apple", "strawberry", "Strawberry"));
map.entrySet().stream().forEach(System.out::println);
}
}
list.parallelStream().forEach(System.out::println)
import java.util.stream.Stream;
public class StreamOf {
public static void main(String[] args) {
var stream = Stream.of("first", "second", "third");
stream.forEach(System.out::println); //first, second, third
}
}
Random.ints method
when generating a random number stream in the appimport java.util.Random;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws Exception {
var stream = Stream.generate(() -> {
var r = new Random();
return r.nextInt(100);
});
stream.limit(10).forEach(System.out::println);
//46 24 42 80 61 67 69 22 85 1
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
var stream = Stream.iterate(1, (num) -> {
return num * 2;
});
stream.limit(10).forEach(System.out::println);
//4 8 16 32 64 128 256 512
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
var builder = Stream.builder()
.add("Munchkin")
.add("Siamese")
.add("Persian");
builder.build().forEach(System.out::println);
//Munchkin
//Siamese
//Persian
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
var stream1 = Stream.of("Munchkin", "Siamese", "Persian");
var stream2 = Stream.of("Corgi", "Poodle", "Shiba");
Stream.concat(stream1, stream2).forEach(System.out::println);
// Munchkin Siamese Persian
// Corgi Poodle Shiba
}
}
//Upper limit A h Not included in the stream
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.range(10, 20)
.forEach(System.out::println); //10 11 12 13 14 15 16 17 18 19
}
}
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
IntStream.rangeClosed(10, 20)
.forEach(System.out::println); //10 11 12 13 14 15 16 17 18 19 20
}
}
Recommended Posts