public class EnumDemo {
public static void main(String[] args) {
//用Integer表示星期几业务含义不明确
Employee e = new Employee();
e.setRestDay(WeekDay.SUN);
if (e.getRestDay() == WeekDay.SAT || e.getRestDay() == WeekDay.SUN)
System.out.println("周末休息");
else System.out.println("周一到周五休息");
System.out.println("-------------------------");
System.out.println(WeekDay.SAT.name());
System.out.println(WeekDay.SAT.ordinal());
System.out.println("-------------------------");
WeekDay[] days = WeekDay.values();
for (WeekDay day : days){
System.out.println(day);
}
System.out.println("-------------------------");
WeekDay day = WeekDay.valueOf("SUN");
System.out.println(day);
}
}
在枚举类中,底层操作就是创建一个一个的对象的实例:
public class WeekDay2 {
public static final WeekDay2 MON= new WeekDay2();//Monday
public static final WeekDay2 TUE= new WeekDay2();//Tuesday
public static final WeekDay2 WED= new WeekDay2();//Wednesday
public static final WeekDay2 THU= new WeekDay2();//Thursday
public static final WeekDay2 FRI= new WeekDay2();//Friday
public static final WeekDay2 SAT= new WeekDay2();//Saturday
public static final WeekDay2 SUN= new WeekDay2();//Sunday
//防止创建新的对象
private WeekDay2(){}//实际上枚举类Enum中并没有无参数构造器,
//只有一个带有String和int的构造器,分别表示枚举对象的名称和序数
}
基本语法:参数列表->表达式
public class LambdaTest1 {
class User {
public String name;
public int score;
@Override
public String toString() {
return "User [name=" + name + ", score=" + score + "]";
}
public User(String name, int score) {
this.name = name;
this.score = score;
}
}
@Test
public void testOldUse1() {
User[] us = new User[] { new User("张三", 90), new User("李四", 95),
new User("王五", 98) };
//sort
Arrays.sort(us, new Comparator() {
@Override
public int compare(User o1, User o2) {
return Integer.compare(o1.score, o2.score);
}
});
System.out.println(Arrays.toString(us));
}
@Test
public void testNewUse1() {
User[] us = new User[] { new User("张三", 90), new User("李四", 95),
new User("王五", 98) };
//sort
//Lambda表达式
Arrays.sort(us, (User o1, User o2) -> {
return Integer.compare(o1.score, o2.score);
});
System.out.println(Arrays.toString(us));
}
@Test
public void testNewUse2() {
User[] us = new User[] { new User("张三", 90), new User("李四", 95),
new User("王五", 98) };
//sort
//Lambda表达式
Arrays.sort(us,
(User o1, User o2) -> Integer.compare(o1.score, o2.score));
System.out.println(Arrays.toString(us));
}
@Test
public void testNewUse3() {
User[] us = new User[] { new User("张三", 90), new User("李四", 95),
new User("王五", 98) };
//sort
//Lambda表达式
Arrays.sort(us, (o1, o2) -> Integer.compare(o1.score, o2.score));
System.out.println(Arrays.toString(us));
}
@Test
public void testOldUse2() {
new Thread(new Runnable() {
@Override
public void run() {
System.out.println("Hello Lambda");
}
}).start();
}
@Test
public void testOldUse3() {
new Thread(() -> System.out.println("Hello Lambda")).start();
}
@Test
public void testOldUse4() {
Runnable run = new Runnable() {
@Override
public void run() {
System.out.println("Hello Lambda!");
}
};
new Thread(run).start();
}
@Test
public void testNewUse5() {
Runnable run = () -> System.out.println("Hello Lambda!");
new Thread(run).start();
new Thread(run).start();
}
}
public class LambdaTest2 {
@Test
public void test1() {
new Thread(() -> System.out.println("Hello Lambda")).start();
}
public static void main(String[] args) {
Frame f = new Frame();
f.setSize(100, 100);
Button btn = new Button("lambda");
btn.addActionListener(e -> System.out.println("Lambda"));
f.add(btn);
f.setVisible(true);
}
@Test
public void test2() {
String[] strs = new String[] { "Lambdalalala", "Cherry", "Bubbui",
"BBC" };
Arrays.sort(strs, (final String s1, final String s2) -> {
if (s1 != null && s2 != null)
return Integer.compare(s1.length(), s2.length());
return -1;
});
System.out.println(Arrays.toString(strs));
}
}
结论:lambda表达式中的自由变量会被保存,无论什么时候执行lambda表达式,都可以直接使用。
public class LambdaTest3 {
/**
* lambda表达式中this指向创建lambda表达式的方法中的this
* @param content
* @param times
*/
public void repeatPrint(String content, int times) {
System.out.println(this);//com.Bryan._06_lambda.LambdaTest3@514713
Runnable run = ()->{
System.out.println(this);//com.Bryan._06_lambda.LambdaTest3@514713
};
new Thread(run).start();
}
public void repeatPrint2(String content, int times) {
Runnable run = new Runnable(){
@Override
public void run() {
System.out.println(this);//com.Bryan._06_lambda.LambdaTest3$1@145df5f
}
};
new Thread(run).start();
}
@Test
public void testVar(){
this.repeatPrint("Hello", 5);
}
}
public class LambdaTest4{
public void wrapWork(IMyWork work){
System.out.println("Do some wrap work...");
try {
work.dowork();
} catch (Exception e) {
e.printStackTrace();
}
}
@Test
public void test() throws Exception{
IMyWork work = () -> {
System.out.println("haha");
Thread.sleep(1000);
};
this.wrapWork(work);
}
}
@FunctionalInterface
public interface IMyWork {
void dowork() throws Exception;
boolean equals(Object o);
}
public class LambdaTest5 {
@Test
public void test1() {
Integer[] is = new Integer[]{2,2,8,4,1,4,7,3};
//Arrays.sort(is,(x,y) -> Integer.compare(x, y));
//也是lambda表达式,方法引用(类::静态方法)
Arrays.sort(is,Integer::compare);
System.out.println(Arrays.toString(is));
}
public int compare(int x,int y){
return Integer.compare(x, y);
}
@Test
public void test2(){
LambdaTest5 lt = new LambdaTest5();
Integer[] is = new Integer[]{2,2,8,4,1,4,7,3};
//方法引用(对象::方法)
//Arrays.sort(is,lt::compare);
//Arrays.sort(is,this::compare);
List list =Arrays.asList(2,2,8,4,1,4,7,3);
//list.forEach(x->System.out.println(x));
list.forEach(System.out::println);
//System.out.println(Arrays.toString(is));
}
}
public class LambdaTest5 {
@Test
public void test1() {
Integer[] is = new Integer[] { 2, 2, 8, 4, 1, 4, 7, 3 };
//Arrays.sort(is,(x,y) -> Integer.compare(x, y));
//也是lambda表达式,方法引用(类::静态方法)
Arrays.sort(is, Integer::compare);
System.out.println(Arrays.toString(is));
}
public int compare(int x, int y) {
return Integer.compare(x, y);
}
@Test
public void test2() {
LambdaTest5 lt = new LambdaTest5();
Integer[] is = new Integer[] { 2, 2, 8, 4, 1, 4, 7, 3 };
//方法引用(对象::方法)
//Arrays.sort(is,lt::compare);
//Arrays.sort(is,this::compare);
List list = Arrays.asList(2, 2, 8, 4, 1, 4, 7, 3);
//list.forEach(x->System.out.println(x));
list.forEach(System.out::println);
//System.out.println(Arrays.toString(is));
}
public List asList(IMyCreator> creator, T... a) {
List list = creator.create();
for (T t : a)
list.add(t);
return list;
}
@Test
public void test3() {
//构造方法引用
//类::new
//List list = Arrays.asList(2, 2, 8, 4, 1, 4, 7, 3);
List list = this.asList(() -> {return new ArrayList();} , 2, 2, 8, 4);
List list2 = this.asList(ArrayList::new , 2, 2, 8, 4);
list.forEach(System.out::println);
System.out.println(list.getClass());
}
}