文章目录
Lambda表达式
Lambda表达式是JDK8开始的一种新语法形式。
基本作用:简化函数式接口的匿名内部类的写法。
注意:
- Lambda表达式可以用来简化匿名内部类的书写
- Lambda表达式只能简化函数式接口的匿名内部类的写法
函数式接口:
有且仅有一个抽象方法的接口叫做函数式接口,接口上方可以加@FunctionalInterface注解
java
import java.util.Arrays;
import java.util.Comparator;
public class test41 {
public static void main(String[] args) {
Integer[] integers = {5, 4, 8, 2, 4, 7, 6, 3, 0, 1, 9, 10};
Arrays.sort(integers, (Integer o1, Integer o2) -> {
return o1 - o2;
});
System.out.println(Arrays.toString(integers)); //[0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10]
}
}
Lambda表达式的省略写法
省略核心:可推导,可省略。
省略规则:
1.参数类型可以省略不写。
java
import java.util.Arrays;
import java.util.Comparator;
public class test41 {
public static void main(String[] args) {
Integer[] integers = {5, 4, 8, 2, 4, 7, 6, 3, 0, 1, 9, 10};
Arrays.sort(integers, (o1, o2) -> {
return o1 - o2;
});
System.out.println(Arrays.toString(integers)); //[0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10]
}
}
2.如果只有一个参数,参数类型可以省略,同时()也可以省略。
3.如果Lambda表达式的方法体只有一行,大括号,分号,return可以省略不写,注意三者需要同时省略。
java
import java.util.Arrays;
import java.util.Comparator;
public class test41 {
public static void main(String[] args) {
Integer[] integers = {5, 4, 8, 2, 4, 7, 6, 3, 0, 1, 9, 10};
Arrays.sort(integers, (o1, o2) -> o1-o2);
System.out.println(Arrays.toString(integers)); //[0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9, 10]
}
}
Lambda练习
练习1
定义数组并存储一些字符串,按照字符串的长度进行排序,
短的在前,长的在后
java
import java.util.Arrays;
import java.util.Comparator;
public class test42 {
public static void main(String[] args) {
String[] strings = {"cjm", "cjm_big_pig", "cjm_pig"};
Arrays.sort(strings, (o1, o2) -> o1.length()-o2.length());
System.out.println(Arrays.toString(strings)); //[cjm, cjm_pig, cjm_big_pig]
}
}
练习2
定义数组并存储一些对象,
要求1:属性有姓名,年龄,身高
要求2:按照年龄的大小进行排序,年龄一样,按照身高排序,身高一样
按照姓名的字母进行排序(姓名中不要有中文或特殊字符)
java
public class pig {
private String name;
private int age;
private int height;
public pig() {
}
public pig(String name, int age, int height) {
this.name = name;
this.age = age;
this.height = height;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
public int getHeight() {
return height;
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setHeight(int height) {
this.height = height;
}
}
java
import java.util.Arrays;
import java.util.Comparator;
public class test43 {
public static void main(String[] args) {
pig pig1 = new pig("cjm", 29, 160);
pig pig2 = new pig("djm", 25, 165);
pig pig3 = new pig("djm", 25, 175);
pig pig4 = new pig("cjm", 25, 175);
pig[] pigs = {pig1, pig2, pig3, pig4};
Arrays.sort(pigs, new Comparator<pig>() {
@Override
public int compare(pig o1, pig o2) {
if (o1.getAge() != o2.getAge()) {
return o1.getAge() - o2.getAge();
} else {
if (o1.getHeight() != o2.getHeight()) {
return o1.getHeight() - o2.getHeight();
} else {
return o1.getName().compareTo(String.valueOf(o2));
}
}
}
});
for (pig pig : pigs) {
System.out.println(pig.getName() + " " + pig.getAge() + " " + pig.getHeight());
}
}
}
运行结果:
djm 25 165
cjm 25 175
djm 25 175
cjm 29 160
算法题
算法题1 斐波那契数列
java
import java.util.ArrayList;
public class test44 {
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList<>();
arrayList.add(1);
arrayList.add(1);
for (int i = 2; i < 20; i++) {
arrayList.add(arrayList.get(i - 1) + arrayList.get(i - 2));
}
System.out.println(arrayList.get(11)); //144
}
}
算法题2 猴子吃桃子
java
public class test45 {
public static void main(String[] args) {
int[] ints = new int[10];
ints[9] = 1;
for (int i = 8; i >= 0; i--) {
ints[i] = (ints[i + 1] + 1) * 2;
}
System.out.println(ints[0]); //1534
}
}
java
public class test45 {
public static void main(String[] args) {
System.out.println(getCount(1));
}
public static int getCount(int day){
if(day<=0||day>=11){
System.out.println("时间错误!");
return -1;
}
if(day==10){
return 1;
}
return (getCount(day+1)+1)*2;
}
}
算法题3 爬楼梯
java
public class test46 {
public static void main(String[] args) {
System.out.println(Fn(20)); //10946
}
public static int Fn(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
return Fn(n - 1) + Fn(n - 2);
}
}
一次可以爬1个或2个或3个台阶:
java
public class test46 {
public static void main(String[] args) {
System.out.println(Fn(20)); //121415
}
public static int Fn(int n) {
if (n == 1) {
return 1;
}
if (n == 2) {
return 2;
}
if (n == 3) {
return 4;
}
return Fn(n - 1) + Fn(n - 2) + Fn(n - 3);
}
}