System.out :: println
method reference as is, you can also pass it as a lambda expression.import java.util.stream.Stream;
public class StreamForEach {
public static void main(String[] args) {
Stream.of("Munchkin", "Siamese", "Persian", "Tama")
.forEach(v -> System.out.println(v));
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
Stream.of("Munchkin", "Siamese", "Persian", "Tama")
.parallel()
.forEach(v -> System.out.println(v));
//Persian Tama Siamese Munchkin
/*If you want to keep the order
.forEachOrdered(v -> System.out.println(v));
Munchkin Siamese Persian Tama
*/
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
var str = Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.filter(s -> s.startsWith("S"))
.findFirst();
//If the orElse method is null"-"Replace with
System.out.println(str.orElse("-")); //Siamese
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
var str = Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.parallel()
.filter(s -> s.startsWith("S"))
.findAny();
System.out.println(str.orElse("-")); //Scottish Fold
}
}
//Check if all the values in the list are 0 or more
import java.util.stream.IntStream;
public class StreamMatch {
public static void main(String[] args) {
System.out.println(
IntStream.of(1, 10, 5, -5, 12)
.allMatch(v -> v >= 0)
); //false
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
var list = Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.filter(s -> s.startsWith("S"))
.toArray();
System.out.println(list[0]); //Siamese
}
}
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamTrans2 {
public static void main(String[] args) {
var list = Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.filter(s -> s.startsWith("S"))
.collect(Collectors.toList());
System.out.println(list);
}
}
//Name the Person object array:Convert to map format of email address
public class StreamCollectMap {
public static void main(String[] args) {
System.out.println(
Stream.of(
new Person("Yamada Taro", "[email protected]"),
new Person("Hanako Suzuki", "[email protected]"),
new Person("Saburo Inoue", "[email protected]"),
new Person("Kumi Sato", "[email protected]"),
new Person("Yamada Taro", "[email protected]")
).collect(Collectors.toMap(
//The argument key represents the key of the map
Person::getName,
//The argument value represents the key of the map
Person::getEmail,
//When there is a possibility of duplication
(s, a) -> s + "/" + a
//Overwrite value when duplicated
// (s, a) -> a
))
);
}
}
public class Person {
private String name;
private String email;
public Person(String name, String email) {
this.name = name;
this.email = email;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
}
import java.util.Comparator;
import java.util.stream.Stream;
public class StreamMin {
public static void main(String[] args) {
var str = Stream.of("Rockfish", "pacific saury", "Flounder", "Sardines", "Atka mackerel")
.min(Comparator.naturalOrder());
System.out.println(str.orElse("")); //Sardines
}
}
//Find the number of strings larger than the string length 3.
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.filter(s -> s.length() > 7)
.count() //2
);
}
}
//Int array sum, average
import java.util.stream.IntStream;
public class StreamSum {
public static void main(String[] args) {
var list = new int[] { 5, 1, 10, -3 };
System.out.println(IntStream.of(list).sum()); //13
System.out.println(IntStream.of(list).average().orElse(0)); //3.25
}
}
//Combine string streams separated by commas
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.reduce((result, str) -> {
return result + "," + str;
})
.orElse("") //Munchkin,Persian,Scottish Fold,Siamese,Tama
);
}
}
import java.util.stream.Stream;
public class StreamReduce2 {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
// .parallel()
.reduce("Himalayan", (result, str) -> {
return result + "," + str;
})
); //Himalayan,Munchkin,Persian,Scottish Fold,Siamese,Tama
}
}
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("153", "211", "112", "350", "418", "208")
.parallel()
.reduce(0,
//Get individual elements
//The operation result is stored in result, and value receives each element.
(result, value) -> {
return result + Integer.parseInt(value); //Convert string value to number
},
//Summarize the distributed results(Parallel stream only)
(result1, result2) -> {
return result1 + result2; //1452
}
)
);
}
}
//Sort a given string stream and convert it to a list
import java.util.ArrayList;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama"
.sorted()
.collect(
//Create a container that stores the value in the first value of reduction by the number of expressions
ArrayList<String>::new,
//Arguments are containers, individual elements
(list, str) -> list.add(str),
//For parallel streams
(list1, list2) -> list1.addAll(list2)
)
);
}
}
import java.util.ArrayList;
//import java.util.Arrays;
import java.util.stream.Collector;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.collect(
Collector.of(
ArrayList::new,
ArrayList::add,
(list1, list2) -> {
list1.addAll(list2);
return list1;
},
Collector.Characteristics.IDENTITY_FINISH
)
)
);
}
}
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.collect(Collectors.joining(",", "<", ">")) //<Munchkin,Persian,Scottish Fold,Siamese,Tama>
);
}
}
//Categorize strings by length
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.collect(
//Receive element type with argument str and generate grouping key
//Group by string
Collectors.groupingBy(str -> str.length()
)) //{4=[Tama], 7=[Persian, Siamese], 8=[Munchkin], 13=[Scottish Fold]}
);
}
}
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.collect(
Collectors.groupingBy(
str -> str.length(),
Collectors.joining("/") //{4=Tama, 7=Persian/Siamese, 8=Munchkin, 13=Scottish Fold}
))
);
}
}
//Divide whether the character string is 7 characters or less or long
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.collect(
Collectors.partitioningBy(
str -> str.length() > 7
) //{false=[Persian, Siamese, Tama], true=[Munchkin, Scottish Fold]}
)
);
}
}
//List streams with toList method
//Collections::Convert to read-only with unmodifiableList
import java.util.Collections;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
System.out.println(
Stream.of("Munchkin", "Siamese", "Persian", "Scottish Fold", "Tama")
.sorted()
.collect(
Collectors.collectingAndThen(
Collectors.toList(),
Collections::unmodifiableList
)
)
);
}
}
import java.util.IntSummaryStatistics;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
var summary = IntStream.of(5, 13, 7, 2, 30)
.collect(
//Pass a reference to the parseInt sod in the constructor, accept, combineDB
IntSummaryStatistics::new,
IntSummaryStatistics::accept,
IntSummaryStatistics::combine
);
System.out.println(summary.getMin()); //2
System.out.println(summary.getSum()); //57
System.out.println(summary.getAverage()); //11.4
}
}
Recommended Posts