作者:陈佩儒61473 | 来源:互联网 | 2023-09-25 09:15
本文由编程笔记#小编为大家整理,主要介绍了java 使用java 8进行字符串处理的有趣示例相关的知识,希望对你有一定的参考价值。
package Java_FP.collection;
import java.util.Arrays;
import java.util.Optional;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;
/**
* Created by liuyufei on 19/10/16.
*/
public class CollectionExample_8 {
final static String[] food = new String[]{
"Crunchy carrots",
"Golden-hued bananas",
"",
"Bright orange pumpkins",
"Little trees of broccoli",
"meat"
};
public static final Predicate NON_EMPTY = s -> !s.isEmpty();
//just return the last element, ignore the previous ones
private static BinaryOperator chooseLast = (allSoFar,nextElement)-> nextElement;
private static Function lastWord =
phrase -> Arrays.asList(phrase.split(" ")).stream().reduce(chooseLast).orElse("");
private static BinaryOperator joinOn(String connector) {
return (allSoFar, nextElement) -> allSoFar + connector + nextElement;
}
private static String summarize(final String[] description) {
return Arrays.asList(description).stream()
.filter(NON_EMPTY)
.map(lastWord)
.reduce(joinOn(" & "))
.orElse("");
//avoid null pointer exception, if allTogetherNow is empty, "" will be returned
}
public static void main(String[] args) {
final String summary = summarize(food);
final String desiredSummary =
"carrots & bananas & pumpkins & broccoli & meat";
System.out.println(summary);
if (summary.equals(desiredSummary)) System.out.println("yay!");
}
}