菜鸡学习day23
package day23;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.function.Function;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
public class D2301{
public static void main(String[] args) {
show(new shiXian());
show(new Function111() {
@Override
public String method01() {
System.out.println("匿名内部类。");
return null;
}
});
show(()->{
System.out.println("lambda表达式重写方法。");
return null;
});
String message1 = "AAA";
String message2 = "BBB";
String message3 = "CCC";
showLog(2,()->{
return message1 + message2 + message3;
});
thread(()->{
System.out.println(Thread.currentThread().getName());
});
String[] s = {
"aaa","bbbb","cccccc","d"};
Arrays.sort(s,comparator());
System.out.println(Arrays.toString(s));
getString(()->{
return "AAAAAAA";
});
int[] array = {
1,3,2,4,5,7,6};
int max = getMax(()->{
int i = 1;
int num = array[0];
while(i < array.length){
if(num <= array[i])
num = array[i];
i++;
}
return num;
});
System.out.println(max);
consumer01("AAA",(String str)->{
System.out.println("消费");
});
consumer02("DDFGasddw",
(con1)->{
System.out.println(con1.toUpperCase());
},(con2)->{
System.out.println(con2.toLowerCase());
});
String[] array1 = {
"小明,男","小红,女","小周,女"};
daYin(array1,(name)->{
String s1 = name.split(",")[0];
System.out.print("姓名:" + s1); },
(xingBie)->{
String s2 = xingBie.split(",")[1];
System.out.println(";性别:" + s2);
});
boolean b1 = predicate("ABCDE",(str)->{
return str.equals("zzz");
});
System.out.println(b1);
boolean b2 = and1("ABCDEa",(str1)->{
return str1.length() > 5;
},(str2)->{
return str2.contains("a");
});
System.out.println(b2);
boolean b3 = or1("ABCDEFG",(str1)->{
return str1.length() > 5;
},(str2)->{
return str2.contains("a");
});
System.out.println(b3);
boolean b4 = no1("ABCDE",(str)->{
return str.length() < 3;
});
System.out.println(b4);
String[] array4 = {
"小明,32","小小红,32","小小小周,17"};
ArrayList<String> list = lianxi(array4,(str)->{
return str.split(",")[0].length() == 3;
},(str)->{
return str.split(",")[1].equals("32");
});
System.out.println(list);
apply1("AAAAA",(str)->{
return str.length();
});
zhuanHuan("123",(str)->{
return Integer.parseInt(str);
},(i)->{
return String.valueOf(i);
});
lianXi("小周,17",(str)->{
return str.split(",")[1];
},(str)->{
return Integer.parseInt(str);
},(i)->{
return i + 100;
});
}
public static void show(Function111 fun){
fun.method01();
}
public static void showLog(int level,Function111 fun){
if(level == 1){
System.out.println(fun.method01());
}
}
public static void thread(Runnable run){
new Thread(run).start();
}
public static Comparator<String> comparator(){
return (String str1,String str2)->{
System.out.println("重写。");
return str2.length() - str1.length();
};
}
public static String getString(Supplier<String> sup){
return sup.get();
}
public static int getMax(Supplier<Integer> sup){
return sup.get();
}
public static void consumer01(String str,Consumer<String> con){
con.accept(str);
}
public static void consumer02(String str, Consumer<String> con1,Consumer<String>con2){
con1.andThen(con2).accept(str);
}
public static void daYin(String[] array,Consumer<String> name,Consumer<String> xingBie){
for(String str : array){
name.andThen(xingBie).accept(str);
}
}
public static boolean predicate(String str,Predicate pre){
return pre.test(str);
}
public static boolean and1(String str,Predicate<String> pre1,Predicate<String> pre2){
return pre1.and(pre2).test(str);
}
public static boolean or1(String str,Predicate<String> pre1,Predicate<String> pre2){
return pre1.or(pre2).test(str);
}
public static boolean no1(String str,Predicate<String> pre1){
return pre1.negate().test(str);
}
public static ArrayList<String> lianxi(String[] array,Predicate<String> pre1,Predicate<String> pre2){
ArrayList<String> list = new ArrayList<>();
for(String str : array){
if(pre1.and(pre2).test(str)){
list.add(str);
}
}
return list;
}
public static void apply1(String str,Function<String, Integer> fun){
Integer i = fun.apply(str);
System.out.println(i);
}
public static void zhuanHuan(String str,Function<String,Integer> fun1,Function<Integer,String> fun2){
Integer i = fun1.apply(str) + 10;
System.out.println(i);
String str2 = fun2.apply(i);
System.out.println(str2.toString());
}
public static void lianXi(String str,Function<String,String> fun1,Function<String,Integer> fun2,Function<Integer,Integer> fun3){
Integer i =fun1.andThen(fun2).andThen(fun3).apply(str);
System.out.println(i);
}
}
package day23;
@FunctionalInterface
public interface Function111 {
public abstract String method01();
}
package day23;
public class shiXian implements Function111 {
@Override
public String method01(){
System.out.println("实现类重写方法。");
return null;
}
}