package function;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Predicate;
public class Function001 {
@Test
public void test001() {
Comparator<Integer> comparable = (o1, o2) -> Integer.compare( o1, o2 );
System.out.println( comparable.compare( 10, 12 ) );
Comparator<Integer> com = Integer::compare;
System.out.println( com.compare( 15, 13 ) );
}
@Test
public void test002() {
Consumer<String> consumer = (a) -> System.out.println( a + " hello world" );
consumer.accept( "boy" );
}
@Test
public void test003() {
MethodTest( 200, (money) -> System.out.println( "价格是:" + money ) );
}
public static void MethodTest(int money, Consumer<Integer> con) {
con.accept( money );
}
@Test
public void test004() {
List<String> list = Arrays.asList( "北冰洋","北极","太平洋","印度洋","大西洋" );
// List<String> filterList =filterList( list,s->s.contains( "洋" ) );
List<String> filterList = filterList( list, Function001::test );
System.out.println( filterList );
}
public static List<String> filterList(List<String> list, Predicate<String> predicate) {
List<String> filterList = new ArrayList<>();
for (String s : list) {
if (predicate.test( s )) {
filterList.add( s );
}
}
return filterList;
}
public static boolean test(String s) {
return s.contains( "洋" );
}
}