jdk1.8新特性
获取时间纳秒差
java.time.Instant.now()当前时间相减获取到毫秒
java.time.Period
java.time.Instant
java.time.Duration
1、接口中的默认方法和静态方法
2、新的日期API LocalDate | LocalTime | LocalDateTime
3、Lambda表达式
函数式接口可通过lambda表达式访问
该注解只能标记在"有且仅有一个抽象方法"的接口上。
4、方法引用和构造器,实例对象的方法,数组引用,通过new的方式来调用
5、Stream API
6、新时间日期API
7、Optional容器
8、JDK8接口中的静态方法和默认方法,都不算是抽象方法。
9、接口默认继承java.lang.Object,所以如果接口显示声明覆盖了Object中方法,那么也不算抽象方法。
10、该注解不是必须的,如果一个接口符合"函数式接口"定义,那么加不加该注解都没有影响。加上该注解能够更好地让编译器进行检查。如果编写的不是函数式接口,但是加上了@FunctionInterface,那么编译器会报错
函数式编程jdk1.8有个java.util.function 包
11、重复注解
import java.lang.annotation.*;
public class RepeatingAnnotations {
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface Filters{
Filter[] value();
}
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Repeatable(Filters.class)
public @interface Filter{
String value();
}
@Filter("filter1")
@Filter("filter2")
public interface Filterable{}
public static void main(String[] args) {
for(Filter filter: Filterable.class.getAnnotationsByType(Filter.class)){
System.out.println(filter.value());
}
}
}
12、扩展了注解的上下文。现在几乎可以为任何东西添加注解:局部变量、泛型类、父类与接口的实现,就连方法的异常也能添加注解。
list.stream().mapToInt(num->Integer.parseInt(num)).filter(e->e%3==0).forEach(System.out::print);
Map<String,Integer> o = (Map<String,Integer>)list.stream().collect(Collectors.toMap(String::trim, String::length,(k,l)->k));
List分区
com.google.common.collect.Lists.partition(list, 1000)
easyexcel导入处理
EasyExcelFactory.read(
file.getInputStream(), TableKerberosExcel.class, new TableKbExcelListener())
.sheet()
.doRead();
@Slf4j
public class TableKbExcelListener extends AnalysisEventListener<TableExcel> {
private static final int BATCH_COUNT = 20000;
List<TableExcel> list = new ArrayList<>();
@Override
public void invoke(TableExcel data, AnalysisContext context) {
list.add(data);
if (list.size() >= BATCH_COUNT) {
saveData();
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext context) {
saveData();
}
方法和消费的用法
public static <R> R remoteCall(
Supplier<R> supplier, String errInterfaceDesc, String errParamDesc) {
try {
return supplier.get();
} catch (Exception e) {
}
};
public static <T, R> R remoteCall(
T param, Function<T, R> function, String errInterfaceDesc, String errParamDesc) {
try {
return function.apply(param);
} catch (Exception e) {
}
};
public static <T, U, R> R remoteCall(
T param1,
U param2,
BiFunction<T, U, R> function,
String errInterfaceDesc,
String errParamDesc) {
try {
return function.apply(param1, param2);
} catch (Exception e) {
}
};
public static <T, R> R remoteCall(T param, Function<T, R> function, String errMsg) {
try {
return function.apply(param);
} catch (Exception e) {
}
};
public static <T, R> void remoteCall(
T param1, R param2, BiConsumer<T, R> function, String errMsg) {
try {
function.accept(param1, param2);
} catch (Exception e) {
}
};
public static <T, R> void remoteCall(
T param1, R param2, BiConsumer<T, R> consumer, String errInterfaceDesc, String errParamDesc) {
try {
consumer.accept(param1, param2);
} catch (Exception e) {
}
}
Collections.toMap解析
// Map<Long, VipEntity> vipMap = vipList.stream().collect(
//Collectors.toMap(VipEntity::getUserId, v -> v, (v1, v2) -> v1));
// 说明:
// .collect(Collectors.toMap(VipEntity::getUserId, v -> v, (v1, v2) -> v1))
// 第一个参数 VipEntity::getUserId 表示选择 VipEntity 的 getUserId 作为map的key值;
// 第二个参数 v -> v 表示选择将原来的对象作为map的value值;
// 第三个参数 (v1, v2) -> v1 中,如果v1与v2的key值相同,选择v1作为那个key所对应的value值。
classifier可以传XXXDTO::getName,分组根据name字段分组
Collectors.groupingBy(classifier)
l
collection转map keyMapper和valueMapper是function,是key和value取值的方法,mapSupplier是要返回的Map类型
Collectors.toMap(keyMapper, valueMapper, (key1, key2) -> key2,mapSupplier);
二进制转化
private static final char[] DIGITS =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e',
'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
private static String toUnsignedString(final byte b, final int shift) { int i = b & 0xFF; final char[] buf = new char[8]; int charPos = 8; final int radix = 1 << shift; final int mask = radix - 1; do { buf[--charPos] = DIGITS[i & mask]; i >>>= shift; } while (i != 0); while ((8 - charPos) < (8 / shift)) { buf[--charPos] = DIGITS[0]; } return new String(buf, charPos, (8 - charPos)); }
数字转换
public static final char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' }; /** * 数字转换成字符串,并加入千分,支持正负 * 将要转换的数0 */ public static String formatThousandString(final long n) { final String s = Long.toString(n); final int nSize = s.length(); int i; if (n < 0) { i = 1 + (nSize - 1) % 3; if (i == 1) { i = 4; } } else { i = nSize % 3; if (i == 0) { i = 3; } } final StringBuilder sb = new StringBuilder(nSize + nSize / 3); sb.append(s.substring(0, i)); for (; i + 3 <= nSize; i += 3) { sb.append(','); sb.append(s.substring(i, i + 3)); } return sb.toString(); } public static String getDecimalString(final double value, final int decimal) { if (0.0 == value) { return "0"; } final long x = (long) (value * 100); final long y = x % 100; return Long.toString(x / 100) + (y > 0 ? ("." + (y)) : ""); } public static String getPercentString(final long value, final long total) { return getPercentString(value, total, 2); } /** * 百分比字符串 * @param value * @param total * @param precision * @return */ public static String getPercentString(final long value, final long total, final int precision) { if (0 == total) { return "0"; } final long x = value * 100; final long y = x * 100 / total % 100; return Long.toString(x / total) + (y > 0 ? ("." + (y)) : ""); } /** * 判断是否是数字字符串 */ public static boolean isNumeric(final String value) { if (Check.isEmpty(value)) { return false; } // 去掉负号 String sNum = value; if (sNum.startsWith("-")) { sNum = sNum.substring(1); } final int n = sNum.length(); for (int i = 0; i < n; i++) { if (!Character.isDigit(sNum.charAt(i)) && sNum.charAt(i) != '.') { return false; } } return true; } /** * Convert the integer to an unsigned number. */ public static String toBinaryString(final int value) { int i = value; final char[] buf = new char[32]; int charPos = 32; final int radix = 1 << 1; final int mask = radix - 1; do { buf[--charPos] = DIGITS[(i & mask)]; i >>>= 1; } while (i != 0); while (charPos > 0) { buf[--charPos] = '0'; } return new String(buf, charPos, (32 - charPos)); } /** * Convert the integer to an unsigned number. */ public static String toBinaryString(final long value) { long i = value; final char[] buf = new char[64]; int charPos = 64; final int radix = 1 << 1; final long mask = radix - 1; do { buf[--charPos] = DIGITS[(int) (i & mask)]; i >>>= 1; } while (i != 0); while (charPos > 0) { buf[--charPos] = '0'; } return new String(buf, charPos, (64 - charPos)); } public static String toHexString(final byte[] bytes) { if (null == bytes) { return ""; } final StringBuilder sb = new StringBuilder(bytes.length); for (final byte b : bytes) { sb.append(ByteUtil.toHexString(b)); } return sb.toString(); } /** * 将数字转换成十六进制字符串并在前面补 * @param value * @return */ public static String toHexString(final int value) { final String hex = "0000000" + Integer.toHexString(value); return hex.substring(hex.length() - 8); } /** * 将数字转换成十六进制字符串并在前面补 * * @param value * @return */ public static String toHexString(final long value) { final String hex = "000000000000000" + Long.toHexString(value); return hex.substring(hex.length() - 16); }
范围处理
IntStream.range(0,list.size()).forEach
list定义排序
list.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(n -> n.getCode()))), ArrayList::new))
浙公网安备 33010602011771号