java常用api

===============================================LocalDateTime===============================================
ZoneId: 时区ID,用来确定Instant和LocalDateTime互相转换的规则
Instant: 用来表示时间线上的一个点(瞬时)
LocalDate: 表示没有时区的日期, LocalDate是不可变并且线程安全的
LocalTime: 表示没有时区的时间, LocalTime是不可变并且线程安全的
LocalDateTime: 表示没有时区的日期时间, LocalDateTime是不可变并且线程安全的
Clock: 用于访问当前时刻、日期、时间,用到时区
Duration: 用秒和纳秒表示时间的数量(长短),用于计算两个日期的“时间”间隔
Period: 用于计算两个“日期”间隔
DateTimeFormatter 格式化

//获取默认时区
ZoneId systemDefault = ZoneId.systemDefault();
//Date转Instant
Instant instant = new Date().toInstant();
//Instant转Date
Date date= Date.from(instant);

//计算日期间隔,获取时间差时,不会计算前一位时间的
Period period = Period.between(LocalDate.now(), LocalDate.now().plusDays(1));
//计算时间间隔,获取时间差时,会计算前一位时间的

Duration duration = Duration.between(LocalDateTime.now(), LocalDateTime.now().plusDays(1));

DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy:MM:dd HH:mm:ss");
//时间转字符串
String format = formatter.format(LocalDateTime.now());
//字符串转时间
LocalDateTime localDateTime = LocalDateTime.parse("2019:11:04 17:16:55", formatter);



Date date = Date.from(LocalDateTime.now().atZone( ZoneId.systemDefault()).toInstant());

Date date= Date.from(LocalDate.now().atStartOfDay(ZoneId.systemDefault()).toInstant());

LocalDateTime localDateTime = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault());
LocalDate localDate = LocalDateTime.ofInstant(new Date().toInstant(), ZoneId.systemDefault()).toLocalDate();
 
===============================================双括号匿名内部类初始化===============================================
JSON.parseObject(value, new TypeReference<HashSet<BookListCollectCacheDTO>>(){});

Java中双括号初始化是个什么操作_听到微笑的博客-CSDN博客

================================================

public JedisPool jedisPool() {
final JedisPool jedisPool;
try {
jedisPool = new JedisPool(DEFAULT_CONFIG, host, port, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, password, 0, null);
     //增加回调函数
Runtime.getRuntime().addShutdownHook(new Thread(jedisPool::close));
} catch (Exception e) {
throw new RuntimeException("init rdb 3.0 failed", e);
}
return jedisPool;
}

================================================

这种方式也挺好,多学习

public class JedisPoolManager {


public <T> T execute(JedisPoolManager.Executor<T> executor) {
Jedis jedis = this.getJedisPool().getResource();
Throwable var3 = null;


Object var4;
try {
var4 = executor.executeCommand(jedis);
} catch (Throwable var13) {
var3 = var13;
throw var13;
} finally {
if (jedis != null) {
if (var3 != null) {
try {
jedis.close();
} catch (Throwable var12) {
var3.addSuppressed(var12);
}
} else {
jedis.close();
}
}


}


return var4;
}


public interface Executor<T> {
T executeCommand(Jedis var1);
}
}



================
public static List<Long> RECOMMEND_BOOKLIST_IDS =new ArrayList<Long>(){{
add(1)
}};
=====

//根据时间排序
orderTaskDOs = orderTaskDOs.stream()
.sorted(Comparator.comparing(InviteUserTaskDO::getDoneTime).reversed()).collect(Collectors.toList());

==============
Map<String, Boolean> map = inviteBenefitInstanceDTO.stream()
.collect(Collectors.toMap(InviteBenefitInstanceDTO::getOuterInstanceId,
e -> InstanceStatus.VALID.toString().equals(e.getUseStatus()),(existing, replacement) -> existing));


对象去重:
===========================

在这个例子中,Person类重写了equalshashCode方法,这样它们只根据id字段判断两个Person对象是否相等。然后通过stream().distinct().collect(Collectors.toList())链式调用来得到去重后的列表。

然而,如果你不想改变Person类的equalshashCode方法的行为(可能因为它们在别的地方被用于不同的目的),你可以使用Collectors.toMapMap.values来实现去重,这不需要改变Person类的定义:

 
1List<Person> distinctList = new ArrayList<>(list.stream() 2 .collect(Collectors.toMap( 3 Person::getId, // keyMapper 4 p -> p, // valueMapper 5 (existing, replacement) -> existing // mergeFunction 6 )).values());

在这个例子中,用Person::getId作为键映射函数,即使用id字段作为键,当出现重复时,mergeFunction决定保留哪一个对象,在本例中保留第一个出现的对象。

两种方法都可以对基于特定字段的列表进行去重,可以根据具体情况选择适合的方法。

    
 
增加排序
 
List<Person> distinctAndSortedList = list.stream() 33 .collect(Collectors.toMap( 34 Person::getId, // keyMapper 35 p -> p, // valueMapper 36 (existing, replacement) -> existing // mergeFunction, keep the first 37 )).values().stream() 38 .sorted(Comparator.comparing(Person::getName)) // sort by the name field 39 .collect(Collectors.toList());

它会按照Person对象的name字段进行排序。

如果你想要降序排序,可以使用Comparatorreversed方法:

 
1.sorted(Comparator.comparing(Person::getName).reversed()) // sort b

===========================

Optional.ofNullable(this.map.get("param"))
.filter(StringUtils::isNotBlank)
.map(JSON::parseObject)
.orElse(new JSONObject());



posted @ 2019-10-17 16:03  ENU  阅读(195)  评论(0编辑  收藏  举报