Directory
- Lambda
-
- lambda expression standard format
- Summarize
- Lambda advanced omission writing method
- practise
- Algorithm questions
- Immortal Rabbit
- Monkey eats peach
- Climb stairs
Lambda
The most intuitive effect is to simplify the writing of anonymous inner classes as above
public static void main(String[] args) {<!-- --> Integer arr[]={<!-- -->2,1,4,6,3,5,8,7,9}; Arrays.sort(arr, (Integer o1, Integer o2)-> {<!-- --> return o1 - o2; } //o1 -o2 is ascending order //o2-o1 is in ascending order ); System.out.println(Arrays.toString(arr)); }
The above is functional programming
Functional programming thinking ignores the complex syntax of object-oriented and emphasizes what to hide rather than who to do it
lambda expression standard format
public class lambdaTest {<!-- --> public static void main(String[] args) {<!-- --> /* method(new Swim() { @Override public void swimming() { System.out.println("I am swimming"); } });*/ method( ()-> {<!-- --> System.out.println("I am swimming"); //You can use lambda because the interface only has one abstract method } ); } private static void method(Swim s) {<!-- --> s.swimming(); } } interface Swim{<!-- --> public abstract void swimming(); }
Summary
lambda makes the code more concise and allows us to pay more attention to the method body, unlike object-oriented where we must first create an object before using the method body
Advanced omission writing method of lambda
Core: can be derived and omitted
public static void main(String[] args) {<!-- --> Integer arr[]={<!-- -->2,1,4,6,3,5,8,7,9}; //Original writing method /* Arrays.sort(arr, (Integer o1, Integer o2)-> { return o1 - o2; } );*/ //Omit writing Arrays.sort(arr, (o1, o2)-> o1 - o2); System.out.println(Arrays.toString(arr)); }
Exercise
public static void main(String[] args) {<!-- --> String arr[]={<!-- -->"aaa","a","aaaa","aa"}; //desired result a aa aaa aaaa /* Arrays.sort(arr, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o1.length()-o2.length(); } });*/ //Simplified writing Arrays.sort(arr,( o1, o2)->o1.length()-o2.length()); //[a, aa, aaa, aaaa] System.out.println(Arrays.toString(arr));//[a, aa, aaa, aaaa] }
Algorithm question
Extended as follows
String s1="a"; String s2="b"; System.out.println(s1.compareTo(s2)); //The print result is -1. Because this comparison is based on alphabetical comparison, a is after b, so -1 is returned. //The reason why the value of -1 is returned is that in the ascii table, a is 97, b is 98, 97-98, so it is -1 String s11="abc"; String s22="abd"; System.out.println(s11.compareTo(s22)); //-1 is the result. This is a comparison one by one. ab is equal to cd and differs by -1.
{<!-- --> /* String s1="a"; String s2="b"; System.out.println(s1.compareTo(s2)); //The print result is -1. Because this comparison is based on alphabetical comparison, a is after b, so -1 is returned. //The reason why the value of -1 is returned is that in the ascii table, a is 97, b is 98, 97-98, so it is -1 String s11="abc"; String s22="abd"; System.out.println(s11.compareTo(s22)); //-1 is the result. This is a comparison one by one. ab is equal to cd and differs by -1. */ //First create three girlfriend objects girlFriend1 gf1=new girlFriend1("nei",18,1.67); girlFriend1 gf2=new girlFriend1("li",19,1.72); girlFriend1 gf3=new girlFriend1("chen",19,1.78); //Save three objects into the array girlFriend1[]girlFriendArr={<!-- -->gf1,gf2,gf3}; //Use Arrays to sort //Anonymous inner class method /* Arrays.sort(girlFriendArr, new Comparator<girlFriend1>() { @Override public int compare(girlFriend1 o1, girlFriend1 o2) { //Sort by age. If the age is the same, sort by the height. If the height is the same, sort by the alphabet of the name. double temp=o2.getAge()-o1.getAge(); //Ternary operator temp=temp==0?o1.getHeight()-o2.getHeight():temp; //The meaning of this ternary operator is that if temp is 0, then compare the age.: Otherwise, it is temp; temp=temp==0?o1.getName().compareTo(o2.getName()):temp; if(temp>0){ return 1; }else if(temp<0){ return -1; }else{ return 0; } //Or write it as follows, but it needs to be forced to convert //return temp; //Print } });*/ //The following shows how to write lambda expressions Arrays.sort(girlFriendArr,( o1, o2)-> {<!-- --> //Sort by age. If the age is the same, sort by the height. If the height is the same, sort by the alphabet of the name. double temp=o2.getAge()-o1.getAge(); //Ternary operator temp=temp==0?o1.getHeight()-o2.getHeight():temp; //The meaning of this ternary operator is that if temp is 0, then compare the age.: Otherwise, it is temp; temp=temp==0?o1.getName().compareTo(o2.getName()):temp; if(temp>0){<!-- --> return 1; }else if(temp<0){<!-- --> return -1; }else{<!-- --> return 0; } //Or write it as follows, but it needs to be forced to convert //return temp; //Print } ); System.out.println(Arrays.toString(girlFriendArr)); }
The Immortal Rabbit
Find the pattern and the sum of the quantities in the first two months is equal to the third month
public static void main(String[] args) {<!-- --> //Look at the number of rabbits in month 12 (Fibonacci) /* int arr[]=new int[12]; //The first two are fixed so assign them directly arr[0]=1; arr[1]=1; //Loop to get the following for (int i = 2; i < arr.length; i + + ) { arr[i]=arr[i-1] + arr[i-2]; //Because the latter one is equal to the sum of the first two days } System.out.println(arr[11]);*/ //Solution 2 recursion //Find patterns //Month fn(12)=fn(11) + fn(10); //fn represents the method name. The number of 12-month rabbits is equal to the number of 11-month rabbits plus the number of 10-month rabbits. //Month fn(11)=fn(10) + fn(9); //Month fn(10)=fn(9) + fn(8); //Month fn(9)=fn(8) + fn(7); //... //fn(2)=1; //fn(1)=1; System.out.println(getSum(12)); } private static int getSum(int month) {<!-- --> if(month==1|| month==2){<!-- --> return 1; } return getSum(month-1) + getSum(month-2); }
Monkey eats peach
public static void main(String[] args) {<!-- --> //day10 days, one of this is known //The rule is that the number of peaches every day is the number of peaches on the next day plus 1 and then ×2 //day10=1 //day9(day10 + 1)×2=4 //day10 is the recursive exit System.out.println(getCount(1)); //The number of peaches on the first day of 1534 } private static int getCount(int day) {<!-- --> if(day<=0||day>10){<!-- --> System.out.println("Number of days that does not exist"); return -1; } if(day==10){<!-- --> return 1; } //The rule is that the number of peaches every day is the number of peaches on the next day plus 1 and then ×2 return (getCount(day + 1) + 1)*2; }
Climb the stairs
public static void main(String[] args) {<!-- --> //One kind of steps and one way to climb //Two kinds of steps, two kinds //21 ways to climb 7 floors of stairs //How many ways to climb the 100th floor? //The recursive exit is step 1 and step 2 System.out.println(getCount(20)); //10946 types } private static int getCount(int n) {<!-- --> if(n==1){<!-- --> return 1; } if(n==2){<!-- --> return 2; } return getCount(n-1) + getCount(n-2); //Because the climbing method of the 20th floor is equal to the climbing method of the 19th floor plus the climbing method of the 18th floor }
public static void main(String[] args) {<!-- --> //expand //Sometimes we climb one at a time, sometimes we climb two at a time, sometimes we climb three at a time. System.out.println(getCount(20)); //121415 } private static int getCount(int n) {<!-- --> if(n==1){<!-- --> return 1; } if(n==2){<!-- --> return 2; } if(n==3){<!-- --> return 4;//The reason for 4 here is because there are four ways to climb on the fourth floor } //The above is the recursive exit return getCount(n-1) + getCount(n-2) + getCount(n-3); } //expand