juc
LongAdder VS AtomicLong
推荐使用LongAdder(采用分段锁机制提高并发)
time
Period.between获取的是区分年月日内容,并非实际间隔
LocalDate from = LocalDate.of(2000,1,1);Period period = Period.between(from,LocalDate.now());System.out.println("2020-01-01到当前日期" + LocalDate.now());System.out.println("距离当前多少年:" + period.getYears());System.out.println("距离当前多少月:" + period.getMonths());System.out.println("距离当前多少日:" + period.getDays());//如果需要计算实际天数间隔,可使用以下方法LocalDate to = LocalDate.of(2000, 1, 1);long day = LocalDate.now().toEpochDay() - to.toEpochDay();ORlong day = ChronoUnit.DAYS.between(LocalDate.now(), to);//from 到to的间隔,小于0表示from>toSystem.out.println("距离当前多少日:" + day);
function
| 方法 | ||
|---|---|---|
| Supplier | 无参数,只有返回值 | - get |
| Consumer | 无返回值 | - accept:该函数式接口的唯一的抽象方法,接收一个参数,没有返回值. - andThen:在执行完调用者方法后再执行传入参数的方法 |
| Predicate | 输出boolean | - test:函数式接口断言,给一个参数T,返回boolean类型的结果。 - and:接收一个Predicate类型,装传入的条件和当前条件以并且的关系过滤数据。 - or:or方法接收一个Predicate类型,将传入的条件和当前的条件以或者的关系过滤数据。 - negate:将当前条件取反。 - isEquals:通过传入对象的equals方法进行判断。 - not:将传入条件取反。 |
| UnaryOperator | 输入T,输出T | |
| Function | 输入T,输出R | - apply:函数式接口方法,输入T,输出R。 - compose:compose接收一个Function参数,先执行传入的逻辑,再执行当前的逻辑。 - andThen:andThen接收一个Function参数,先执行当前的逻辑,再执行传入的逻辑。 - identity:方便方法的连缀,返回当前对象。 |
Supplier:T get()
Consumer:void do()
Predicate: boolean test(T)
Function:R apply(T)
自定义:在interface上增加注解@FunctionalInterface
Function<T, R> => R apply(T t); 接受一个输入参数,返回一个结果BiFunction<T, U, R> => R apply(T t, U u); 接受两个输入参数,返回一个结果Predicate<T> => boolean test(T t); 接受一个输入参数,返回布尔值BiPredicate<T, U> => boolean test(T t, U u); 接受两个输入参数,返回布尔值Supplier<T> => T get(); 无输入参数,返回一个结果BooleanSupplier => boolean getAsBoolean();Consumer<T> => void accept(T t); 接受一个输入参数,无返回值BiConsumer<T, U> => void accept(T t, U u); 接受两个输入参数,无返回值BinaryOperatorstatic <T> T operatorBy(T num1, T num2, BinaryOperator<T> result) {// 返回两个参数的计算结果return result.apply(num1, num2);}static <T> T minBy(T str1, T str2, Comparator<T> comparator) {// 返回两者里面较小的一个return BinaryOperator.minBy(comparator).apply(str1, str2);}static <T> T maxBy(T str1, T str2, Comparator<T> comparator) {// 返回两者里面较大的一个return BinaryOperator.maxBy(comparator).apply(str1, str2);}operatorBy(4,2,(num1,num2)->num1 - num2)minBy("hello","wonders", Comparator.comparingInt(String::length))//-- PredicatePredicate<String> predicate = (s) -> s.length() > 0;predicate.test("foo"); // truepredicate.negate().test("foo"); // falsePredicate<Boolean> nonNull = Objects::nonNull;Predicate<Boolean> isNull = Objects::isNull;Predicate<String> isEmpty = String::isEmpty;Predicate<String> isNotEmpty = isEmpty.negate();// --FunctionsFunction<String, Integer> toInteger = Integer::valueOf;Function<String, String> backToString = toInteger.andThen(String::valueOf);backToString.apply("123"); // "123"//-- SuppliersSupplier<Person> personSupplier = Person::new;personSupplier.get(); // new Person//-- ConsumersConsumer<Person> greeter = (p) -> System.out.println("Hello, " + p.firstName);greeter.accept(new Person("Luke", "Skywalker"));//-- ComparatorsComparator<Person> comparator = (p1, p2) -> p1.firstName.compareTo(p2.firstName);Person p1 = new Person("John", "Doe");Person p2 = new Person("Alice", "Wonderland");comparator.compare(p1, p2); // > 0comparator.reversed().compare(p1, p2); // < 0
stream
# 以集合对象中某个字段构造Map集合.collect(Collectors.toMap(ClassName::getId, Function.identity())) # Map<String,Class>.collect(Collectors.groupingBy(ClassName::getParent)) # Map<String,List<Class>># 获取集合对象中某个字段的集合.stream().map(ClassName::properties).collect(Collectors.toList())# 累加Stream::mapToIntStream::mapToDoubleStream::mapToLongint sum = list.stream().mapToInt(p -> p.getNum()).sum();BigDecimal sum = list.stream().map(Person::getAmount).reduce(BigDecimal::add).get();BigDecimal sum = list.stream().map(item -> item.getAmount()).reduce(BigDecimal.ZERO, BigDecimal::add);
