package com.hrf.newFeature;
public interface Dao {
void show();
}
package com.hrf.newFeature;
public interface Dao2 {
void show2(int i);
}
package com.hrf.newFeature;
public interface Dao3 {
void show(int x, int y);
}
package com.hrf.newFeature;
public interface Dao4 {
int sum(int x, int y);
}
package com.hrf.newFeature;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
public class Lambda01 {
public static void main(String[] args) {
Dao dao =() -> System.out.println("输出的结果!");
Dao dao2 =() ->{
System.out.println("输出的结果!1");
System.out.println("输出的结果!2");
System.out.println("输出的结果!3");
};
dao.show();
dao2.show();
Dao2 dao3 =(x) -> System.out.println(x);
dao3.show2(10);
Dao3 dao4 =(x,y) -> System.out.println(x + y);
dao4.show(10, 20);
Dao4 dao5 =(x,y) -> (x / y);
int s = dao5.sum(100, 20);
System.out.println(s);
Dao4 dao6 =(x,y) -> {
if(x > y) {
return x + y;
}else {
return y - x;
}
};
int s1 = dao5.sum(100, 20);
System.out.println(s1);
Comparator<Integer> comparator = (x,y) -> y-x;
Set<Integer> set = new TreeSet<>(comparator);
set.add(9);
set.add(4);
set.add(5);
set.add(3);
System.out.println(set);
Set<String> set1 = new TreeSet<>((x,y)->x.length() - y.length());
set1.add("hello");
set1.add("worl");
set1.add("he");
System.out.println(set1);
Person p1 = new Person("张三", 23);
Person p2 = new Person("张三图", 21);
Person p3 = new Person("张三啊", 20);
List<Lambda01.Person> list = new ArrayList<Lambda01.Person>();
list.add(p1);
list.add(p2);
list.add(p3);
list.stream().filter(el -> el.getAge() > 18).forEach(System.out::println);
Collections.sort(list, (a, b) ->{
if(a.getAge() - b.getAge() == 0) {
return a.getName().compareTo(b.getName());
}else {
return Integer.compare(a.getAge(), b.getAge());
}
});
list.forEach(el -> System.out.println(el.getName() + "\t" + el.getAge()));
}
public static class Person {
String name;
Integer age;
public Person() {
}
public Person(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
}