默认方法与静态方法
@FunctionalInterface
public interface IUserCredential {
String verIifUser(String username);
static void login() {
System.out.println("Login Success");
}
default void signOut() {
System.out.println("Log out");
}
}
import java.util.UUID;
import java.util.function.*;
class RunnerMain implements IUserCredential {
@Override
public String verIifUser(String username) {
return "萨瓦迪卡";
}
public static void main(String[] args) {
Predicate<String> pred = "admin"::equals;
System.out.println(pred.test("周杰伦"));
Consumer<String> con = (message) -> {
System.out.println(message + "sent");
};
con.accept("message");
Function<String, Integer> function = (gender) -> "male".equals(gender) ? 1 : 0;
System.out.println(function.apply("male"));
System.out.println(function.apply("male1"));
Supplier<String> supplier = () -> UUID.randomUUID().toString();
System.out.println(supplier.get());
UnaryOperator<String> un = (img) -> {
img += "200*120";
return img;
};
System.out.println(un.apply("原图--"));
BinaryOperator<Integer> bin = (i1, i2) -> i1 > i2 ? i1 : i2;
System.out.println(bin.apply(10, 20));
EtPerson etPerson = Person::getNumber;
System.out.println(etPerson.howMany());
Person per = new Person();
EtPerson et = per::numeral;
System.out.println(et.howMany());
BindPerson person = Person::new;
System.out.println(person.singer());
}
}
class Person {
public static int getNumber() {
return 1;
}
public int numeral() {
return 1;
}
}
interface EtPerson {
int howMany();
}
interface BindPerson {
Person singer();
}
常用集合转换为stream
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class SteamTest {
public static void main(String[] args) {
Stream<String> stream = Stream.of("admin", "tom", "damu");
String[] strArrays = new String[]{"xueqi", "biyao"};
Stream<String> stream2 = Arrays.stream(strArrays);
List<String> list = new ArrayList<>();
list.add("少林");
list.add("武当");
list.add("青城");
list.add("崆峒");
list.add("峨眉");
Stream<String> stream3 = list.stream();
Set<String> set = new HashSet<>();
set.add("少林罗汉拳");
set.add("武当长拳");
set.add("青城剑法");
Stream<String> stream4 = set.stream();
Map<String, Integer> map = new HashMap<>();
map.put("tom", 1000);
map.put("jerry", 1200);
map.put("shuke", 1000);
Stream<Map.Entry<String, Integer>> stream5 = map.entrySet().stream();
String[] array = stream.toArray(String[]::new);
String str = stream.collect(Collectors.joining());
List<String> stringList = stream.collect(Collectors.toList());
Set<String> stringSet = stream.collect(Collectors.toSet());
Map<String, String> stringMap = stream.collect(Collectors.toMap(x -> x, y -> y));
List<String> accountList = new ArrayList<>();
accountList.add("人工");
accountList.add("智能");
accountList.add("深度");
accountList.add("学习");
accountList.add("数学");
accountList.add("线性");
accountList.add("代数");
accountList = accountList.stream().map(x -> "python " + x).collect(Collectors.toList());
accountList = accountList.stream().filter(x -> x.length() > 1).collect(Collectors.toList());
accountList.stream().peek(x -> System.out.println("peek 1:" + x))
.peek(x -> System.out.println("peek 2:" + x))
.forEach(System.out::println);
List<Integer> intList = new ArrayList<>();
intList.add(20);
intList.add(19);
intList.add(7);
intList.add(8);
intList.add(86);
intList.add(11);
intList.add(3);
intList.add(20);
intList.stream().skip(2).collect(Collectors.toList()).forEach(System.out::println);
intList.stream().limit(3).collect(Collectors.toList()).forEach(System.out::println);
intList.stream().distinct().forEach(System.out::println);
Optional<Integer> optionalMax = intList.stream().max(Comparator.comparingInt(x -> x));
optionalMax.ifPresent(System.out::println);
Optional<Integer> optionalMin = intList.stream().min(Comparator.comparingInt(x -> x));
optionalMin.ifPresent(System.out::println);
intList.stream().sorted().collect(Collectors.toList()).forEach(System.out::println);
intList.stream().reduce(Integer::sum).ifPresent(System.out::println);
}
}