public int add(int x, int y) {
return x + y;
}
(int x, int y) -> x + y;
(x, y) -> x + y; //返回两数之和
(x, y) -> { return x + y; } //显式指明返回值
() -> { System.out.println("Hello Lambda!"); }
c -> { return c.size(); }
@FunctionalInterface
public interface Runnable { void run(); }
public interface Callable<V> { V call() throws Exception; }
public interface ActionListener { void actionPerformed(ActionEvent e); }
public interface Comparator<T> { int compare(T o1, T o2); boolean equals(Object obj); }
Runnable r1 = () -> {System.out.println("Hello Lambda!");};
Object obj = r1;
Object obj = () -> {System.out.println("Hello Lambda!");}; // ERROR! Object is not a functional interface!
Object o = (Runnable) () -> { System.out.println("hi"); }; // correct
System.out.println( () -> {} ); //错误! 目标类型不明
System.out.println( (Runnable)() -> {} ); // 正确
@FunctionalInterface
public interface MyRunnable {
public void run();
}
Runnable r1 = () -> {System.out.println("Hello Lambda!");};
MyRunnable2 r2 = () -> {System.out.println("Hello Lambda!");};
@FunctionalInterface
public interface Function<T, R> {
R apply(T t);
}
@FunctionalInterface
public interface Consumer<T> {
void accept(T t);
}
@FunctionalInterface
public interface Predicate<T> {
boolean test(T t);
}
Thread oldSchool = new Thread( new Runnable () {
@Override
public void run() {
System.out.println("This is from an anonymous class.");
}
} );
Thread gaoDuanDaQiShangDangCi = new Thread( () -> {
System.out.println("This is from an anonymous method (lambda exp).");
} );
for(Object o: list) { // 外部迭代
System.out.println(o);
}
list.forEach(o -> {System.out.println(o);}); //forEach函数实现内部迭代
List<Shape> shapes = ... shapes.stream() .filter(s -> s.getColor() == BLUE) .forEach(s -> s.setColor(RED));
shapes.parallelStream(); // 或shapes.stream().parallel()
//给出一个String类型的数组,找出其中所有不重复的素数
public void distinctPrimary(String... numbers) {
List<String> l = Arrays.asList(numbers);
List<Integer> r = l.stream()
.map(e -> new Integer(e))
.filter(e -> Primes.isPrime(e))
.distinct()
.collect(Collectors.toList());
System.out.println("distinctPrimary result is: " + r);
}
//给出一个String类型的数组,找出其中各个素数,并统计其出现次数
public void primaryOccurrence(String... numbers) {
List<String> l = Arrays.asList(numbers);
Map<Integer, Integer> r = l.stream()
.map(e -> new Integer(e))
.filter(e -> Primes.isPrime(e))
.collect( Collectors.groupingBy(p->p, Collectors.summingInt(p->1)) );
System.out.println("primaryOccurrence result is: " + r);
}
Collectors.groupingBy(p->p, Collectors.summingInt(p->1))
//给出一个String类型的数组,求其中所有不重复素数的和
public void distinctPrimarySum(String... numbers) {
List<String> l = Arrays.asList(numbers);
int sum = l.stream()
.map(e -> new Integer(e))
.filter(e -> Primes.isPrime(e))
.distinct()
.reduce(0, (x,y) -> x+y); // equivalent to .sum()
System.out.println("distinctPrimarySum result is: " + sum);
}
// 统计年龄在25-35岁的男女人数、比例
public void boysAndGirls(List<Person> persons) {
Map<Integer, Integer> result = persons.parallelStream().filter(p -> p.getAge()>=25 && p.getAge()<=35).
collect(
Collectors.groupingBy(p->p.getSex(), Collectors.summingInt(p->1))
);
System.out.print("boysAndGirls result is " + result);
System.out.println(", ratio (male : female) is " + (float)result.get(Person.MALE)/result.get(Person.FEMALE));
}
// 嵌套的λ表达式
Callable<Runnable> c1 = () -> () -> { System.out.println("Nested lambda"); };
c1.call().run();
// 用在条件表达式中
Callable<Integer> c2 = true ? (() -> 42) : (() -> 24);
System.out.println(c2.call());
// 定义一个递归函数,注意须用this限定
protected UnaryOperator<Integer> factorial = i -> i == 0 ? 1 : i * this.factorial.apply( i - 1 );
...
System.out.println(factorial.apply(3));
int five = ( (x, y) -> x + y ) (2, 3); // ERROR! try to call a lambda in-place
...
int tmp1 = 1; //包围类的成员变量
static int tmp2 = 2; //包围类的静态成员变量
public void testCapture() {
int tmp3 = 3; //没有声明为final,但是effectively final的本地变量
final int tmp4 = 4; //声明为final的本地变量
int tmp5 = 5; //普通本地变量
Function<Integer, Integer> f1 = i -> i + tmp1;
Function<Integer, Integer> f2 = i -> i + tmp2;
Function<Integer, Integer> f3 = i -> i + tmp3;
Function<Integer, Integer> f4 = i -> i + tmp4;
Function<Integer, Integer> f5 = i -> {
tmp5 += i; // 编译错!对tmp5赋值导致它不是effectively final的
return tmp5;
};
...
tmp5 = 9; // 编译错!对tmp5赋值导致它不是effectively final的
}
...
Integer::parseInt //静态方法引用 System.out::print //实例方法引用 Person::new //构造器引用
//c1 与 c2 是一样的(静态方法引用) Comparator<Integer> c2 = (x, y) -> Integer.compare(x, y); Comparator<Integer> c1 = Integer::compare; //下面两句是一样的(实例方法引用1) persons.forEach(e -> System.out.println(e)); persons.forEach(System.out::println); //下面两句是一样的(实例方法引用2) persons.forEach(person -> person.eat()); persons.forEach(Person::eat); //下面两句是一样的(构造器引用) strList.stream().map(s -> new Integer(s)); strList.stream().map(Integer::new);
public void distinctPrimarySum(String... numbers) {
List<String> l = Arrays.asList(numbers);
int sum = l.stream().map(Integer::new).filter(Primes::isPrime).distinct().sum();
System.out.println("distinctPrimarySum result is: " + sum);
}
super::toString //引用某个对象的父类方法 String[]::new //引用一个数组的构造器
public interface MyInterf {
String m1();
default String m2() {
return "Hello default method!";
}
}
public class Sub implements Base1, Base2 {
public void hello() {
Base1.super.hello(); //使用Base1的实现
}
}
public interface MyInterf {
String m1();
default String m2() {
return "Hello default method!";
}
static String m3() {
return "Hello static method in Interface!";
}
}
Stream.generate(Math::random).limit(5).forEach(System.out::println);
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有