package com.cy.test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
/**
* 新机测试
*/
public class Test {
public static void main(String[] args) {
System.out.println("hello world");
starThread(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"线程启动");
}
});
// 方法的参数是一个函数式接口 接口中只有一个方法
starThread(()-> System.out.println(Thread.currentThread().getName()+"线程启动"));
System.out.println("=====================================");
ArrayList<String> list = new ArrayList<>();
list.add("sss");
list.add("a");
list.add("dd");
list.add("cccc");
list.add("zzzzzz");
System.out.println(list);
Collections.sort(list);
System.out.println(list);
Collections.sort(list,getComparator());
System.out.println(list);
}
//runable是函数式接口
public static void starThread(Runnable run){
// Thread thread = new Thread(run);
// thread.start();
new Thread(run).start();
}
public static Comparator<String> getComparator(){
// Comparator<String> comparator=new Comparator<String>() {
// @Override
// public int compare(String s1, String s2) {
// return s1.length()-s2.length();
// }
// };
// return comparator;
// return (String s1,String s2)->{
// return s1.length()-s2.length();
// };
return (s1,s2)->{return s1.length()-s2.length();};
}
}
常用的函数式接口
supplier接口
package com.cy.test;
import java.util.function.Supplier;
/**
* supplier接口测试
*/
public class SupplierTest {
public static void main(String[] args) {
String string = getString(new Supplier<String>() {
@Override
public String get() {
return "hello world";
}
});
System.out.println(string);
//lambda
String string1 = getString(() -> {
return "hello world java";
});
System.out.println(string1);
String string2 = getString(() -> "hello world java test");
System.out.println(string2);
Integer i = getInteger(new Supplier<Integer>() {
@Override
public Integer get() {
return 9527;
}
});
System.out.println(i);
Integer i2 = getInteger(() -> 9527);
System.out.println(i2);
int[] arr={1,651,65,418,44,51,12,33,2};
int max = getMax(new Supplier<Integer>() {
@Override
public Integer get() {
int max = arr[0];
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
return max;
}
});
System.out.println(max);
int max1 = getMax(() -> {
int max2 = arr[0];
for (int j = 0; j < arr.length; j++) {
if (arr[j] > max2) {
max2 = arr[j];
}
}
return max2;
});
System.out.println(max1);
}
public static String getString(Supplier<String> s){
return s.get();
}
public static Integer getInteger(Supplier<Integer> i){
return i.get();
}
public static int getMax(Supplier<Integer> i){
return i.get();
}
}
package com.cy.test;
import java.util.function.Predicate;
public class PredicateTest {
public static void main(String[] args) {
boolean b = checkString("打啊三大发生大", (String s) -> {
return s.length() > 9;
});
System.out.println(b);
boolean b1 = checkString("sdfsdfsdfds", s -> s.length() > 4);
System.out.println(b1);
boolean b2 = check("sdfsdfsdfds", s -> s.length() > 4,s -> s.length() <16);
System.out.println(b2);
}
public static boolean checkString(String s, Predicate<String> pre){
// return pre.test(s);
// return !pre.test(s);
return pre.negate().test(s);
}public static boolean check(String s, Predicate<String> pre,Predicate<String> pre2){
// boolean b = pre.test(s);
// boolean b2 = pre.test(s);
// return b && b2;
//短路与
boolean b = pre.and(pre2).test(s);
//短路或
boolean b2 = pre.or(pre2).test(s);
return b;
}
}
package com.cy.test;
import java.util.function.Function;
public class FunctionTest {
public static void main(String[] args) {
convert("666",s -> Integer.parseInt(s));
convert("666",Integer::parseInt);
convert2(100,i -> String.valueOf(i+555));
}
public static void convert(String s, Function<String,Integer>fun){
Integer i = fun.apply(s);
System.out.println(i);
}
public static void convert2(int i, Function<Integer,String>fun){
String s = fun.apply(i);
System.out.println(s);
}
}