示例1
//@FunctionalInterface 注解修饰的接口只能有一个抽象方法
@FunctionalInterface
public interface IConvert<F , T> {
//抽象方法 将F转换为T (convert 转换)
T convert(F f);
}
public class Something {
// constructor methods
Something() {
}
Something(String something) {
System.out.println(something);
}
// static methods
static String startsWith(String s) {
return String.valueOf(s.charAt(0));
}
// object methods
String endWith(String s) {
return String.valueOf(s.charAt(s.length() - 1));
}
void endWith() {
}
public static void main(String[] args) {
//访问静态方法
IConvert<String, String> convert = Something::startsWith;
//访问构造函数
IConvert<String,Something> convert1 = Something::new;
//访问对象方法
Something something = new Something();
IConvert<String,String> convert2 = something::endWith;
/**
* 我们可以把类Something中的方法static String startsWith(String s){...}、
* String endWith(String s){...}、Something(String something){...}看作是接口IConvert的实现,
* 因为它们都符合接口定义的那个“模版”,有传参类型F以及返回值类型T。比如构造方法Something(String something),
* 它传参为String类型,返回值类型为Something。注解@FunctionalInterface保证了接口有且仅有一个抽象方法,
* 所以JDK能准确地匹配到相应方法。
**/
}
}
示例2
//anyMatch表示,判断的条件里,任意一个元素成功,返回true
boolean t = transactions.stream().anyMatch(s -> "Alan".equals(s.getTrader().getCity()));
Optional<Integer> sum = transactions.stream()
.filter(s -> "Alan".equals(s.getTrader().getCity()))
.map(Transaction::getValue)
.reduce(Integer::sum);
示例3
range,需要传入开始节点和结束节点两个参数,返回的是一个有序的LongStream。包含开始节点和结束节点两个参数之间所有的参数,间隔为1.
差别就是rangeClosed包含最后的结束节点,range不包含。
parallel() 开启并行流 parallelStream是线程不安全的
getAsLong 返回结果为 long
long result = LongStream.rangeClosed(1, n).parallel().reduce(Long::sum).getAsLong()
函数式的运用
//Objects.requireNonNull 判断对象空值
String username = Objects.requireNonNull(textInputLayoutUsername.getEditText(), "TextInputLayout must have an EditText as child").getText().toString();
public class ParallelStreams {
public static long iterativeSum(long n) {
long result = 0;
for (long i = 0; i <= n; i++) {
result += i;
}
return result;
}
public static long sequentialSum(long n) {
return Stream.iterate(1L, i -> i + 1).limit(n).reduce(Long::sum).get();
}
public static long parallelSum(long n) {
return Stream.iterate(1L, i -> i + 1).limit(n).parallel().reduce(Long::sum).get();
}
public static long rangedSum(long n) {
return LongStream.rangeClosed(1, n).reduce(Long::sum).getAsLong();
}
public static long parallelRangedSum(long n) {
return LongStream.rangeClosed(1, n).parallel().reduce(Long::sum).getAsLong();
}
public static long sideEffectSum(long n) {
Accumulator accumulator = new Accumulator();
LongStream.rangeClosed(1, n).forEach(accumulator::add);
return accumulator.total;
}
public static long sideEffectParallelSum(long n) {
Accumulator accumulator = new Accumulator();
LongStream.rangeClosed(1, n).parallel().forEach(accumulator::add);
return accumulator.total;
}
public static class Accumulator {
private long total = 0;
public void add(long value) {
total += value;
}
}
}
public class ParallelStreamsHarness {
public static void main(String[] args) {
System.out.println("Iterative Sum done in: " + measurePerf(ParallelStreams::iterativeSum, 10_000_000L) + " msecs");
System.out.println("Sequential Sum done in: " + measurePerf(ParallelStreams::sequentialSum, 10_000_000L) + " msecs");
System.out.println("Parallel forkJoinSum done in: " + measurePerf(ParallelStreams::parallelSum, 10_000_000L) + " msecs" );
System.out.println("Range forkJoinSum done in: " + measurePerf(ParallelStreams::rangedSum, 10_000_000L) + " msecs");
System.out.println("Parallel range forkJoinSum done in: " + measurePerf(ParallelStreams::parallelRangedSum, 10_000_000L) + " msecs" );
System.out.println("ForkJoin sum done in: " + measurePerf(ForkJoinSumCalculator::forkJoinSum, 10_000_000L) + " msecs" );
System.out.println("SideEffect sum done in: " + measurePerf(ParallelStreams::sideEffectSum, 10_000_000L) + " msecs" );
System.out.println("SideEffect prallel sum done in: " + measurePerf(ParallelStreams::sideEffectParallelSum, 10_000_000L) + " msecs" );
}
public static <T, R> long measurePerf(Function<T, R> f, T input) {
long fastest = Long.MAX_VALUE;
for (int i = 0; i < 10; i++) {
long start = System.nanoTime();
R result = f.apply(input);
long duration = (System.nanoTime() - start) / 1_000_000;
System.out.println("Result: " + result);
if (duration < fastest) fastest = duration;
}
return fastest;
}
}
参考
来源:CSDN
作者:chrislin9
链接:https://blog.csdn.net/qq_31964019/article/details/103919900