雖然jdk1.8已經(jīng)是很久以前更新的了硼啤,但是開發(fā)人員對(duì)于1.8的新特性使用可能不是很多过牙,接下來(lái)文章會(huì)說(shuō)一些1.8中用的比較多的功能威彰,趕緊學(xué)起來(lái)
1肝谭,首先用的最多的是stream流的使用
以前我們對(duì)于集合的遍歷是這樣的(舉個(gè)例子)
for (User user : list) {
//執(zhí)行便利后的操作
user.setAge(user.getAge()+1);
}
stream在jdk1.8中使用
list.stream().forEach(r->{
r.setAge(r.getAge()+1);
});
在1.8中流對(duì)于集合還有很便利的操作
//獲取集合中年齡大于10的用戶
list.stream().filter(user -> user.getAge()>10).collect(Collectors.toList());
//根據(jù)用戶的年紀(jì)排序
list.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());
//獲取該集合中年齡最大的用戶
list.stream().max(Comparator.comparing(User::getAge)).get();
//將用戶集合中的用戶年齡單獨(dú)取出作為一個(gè)集合(常用與日常開發(fā)中根據(jù)主表id去查詢子表數(shù)據(jù)的業(yè)務(wù)中)
list.stream().map(User::getAge).collect(Collectors.toList());
//將集合按照key是用戶id掘宪,value是用戶本省的map集合
list.stream().collect(Collectors.toMap(User::getId, Function.identity()));
這些是開發(fā)中常用的一些流的操作,我們?cè)陂_發(fā)中可以組合起來(lái)操作
2攘烛,接下來(lái)是對(duì)于流的操作(輸入流,輸出流)
眾所周知魏滚,我們?cè)谥笆褂幂斎肓鳎敵隽鞯臅r(shí)候每次使用完之后都需要關(guān)閉流坟漱,但是在1.8以后我們還需要關(guān)閉嗎鼠次?不需要了,1.8已經(jīng)幫我們實(shí)現(xiàn)了自動(dòng)關(guān)閉的功能了芋齿,那編碼和以前有什么區(qū)別呢腥寇,1.8又是怎么樣實(shí)現(xiàn)的呢?
那這其中最關(guān)鍵的一個(gè)接口
public interface AutoCloseable {
/**
* Closes this resource, relinquishing any underlying resources.
* This method is invoked automatically on objects managed by the
* {@code try}-with-resources statement.
*
* <p>While this interface method is declared to throw {@code
* Exception}, implementers are <em>strongly</em> encouraged to
* declare concrete implementations of the {@code close} method to
* throw more specific exceptions, or to throw no exception at all
* if the close operation cannot fail.
*
* <p> Cases where the close operation may fail require careful
* attention by implementers. It is strongly advised to relinquish
* the underlying resources and to internally <em>mark</em> the
* resource as closed, prior to throwing the exception. The {@code
* close} method is unlikely to be invoked more than once and so
* this ensures that the resources are released in a timely manner.
* Furthermore it reduces problems that could arise when the resource
* wraps, or is wrapped, by another resource.
*
* <p><em>Implementers of this interface are also strongly advised
* to not have the {@code close} method throw {@link
* InterruptedException}.</em>
*
* This exception interacts with a thread's interrupted status,
* and runtime misbehavior is likely to occur if an {@code
* InterruptedException} is {@linkplain Throwable#addSuppressed
* suppressed}.
*
* More generally, if it would cause problems for an
* exception to be suppressed, the {@code AutoCloseable.close}
* method should not throw it.
*
* <p>Note that unlike the {@link java.io.Closeable#close close}
* method of {@link java.io.Closeable}, this {@code close} method
* is <em>not</em> required to be idempotent. In other words,
* calling this {@code close} method more than once may have some
* visible side effect, unlike {@code Closeable.close} which is
* required to have no effect if called more than once.
*
* However, implementers of this interface are strongly encouraged
* to make their {@code close} methods idempotent.
*
* @throws Exception if this resource cannot be closed
*/
void close() throws Exception;
}
你去看看jdk中常用的流觅捆,你去看看他們的底層代碼赦役,沒錯(cuò)都繼承/實(shí)現(xiàn)了這個(gè)接口,這個(gè)接口的作用就是幫我們關(guān)閉流栅炒,那我們現(xiàn)在代碼中應(yīng)該怎么寫呢?
try (FileOutputStream out = new FileOutputStream(new File("C://XXX"));
FileInputStream in = new FileInputStream(new File("C://XXX"));
) {
in.read();
out.write(12);
} catch (IOException e) {
e.printStackTrace();
}
這只是舉個(gè)例子掂摔,具體的業(yè)務(wù)邏輯啥的根據(jù)自己功能來(lái)寫,這兩個(gè)流就會(huì)在執(zhí)行完之后自動(dòng)關(guān)閉赢赊,當(dāng)然我們也可以模擬jdk對(duì)于流的操作乙漓,比如我們?cè)谑褂胷edis來(lái)做分布式鎖的時(shí)候我們可以給我們的分布式鎖的工具類實(shí)現(xiàn)AutoCloseable 接口重寫close方法,然后在close方法中完成對(duì)與鎖的釋放释移,根據(jù)自己業(yè)務(wù)需求可自由發(fā)揮使用
3叭披,Optional的使用
Optional可以在很大程度上減少空指針的出現(xiàn),話不多說(shuō)直接上代碼
ArrayList<User> list = new ArrayList<>();
//判斷user是否為空如果為空就返回新創(chuàng)建的user
Optional.ofNullable(user).orElse(new User());
Optional<User> max = list.stream().max(Comparator.comparing(User::getAge));
//判斷對(duì)象是否存在
boolean present = max.isPresent();
//獲取返回的具體的對(duì)象實(shí)體
User user1 = max.get();
4秀鞭,時(shí)間類LocalDateTime及其周邊
在jdk1.8以后對(duì)于時(shí)間日歷的操作更加方便了
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis() * 1000);
//獲取年
calendar.get(Calendar.YEAR);
//獲取月
calendar.get(Calendar.MONTH);
//獲取日
calendar.get(Calendar.DAY_OF_MONTH);
//獲取時(shí)
calendar.get(Calendar.HOUR_OF_DAY);
//獲取分
calendar.get(Calendar.MINUTE);
//獲取秒
calendar.get(Calendar.MILLISECOND);
現(xiàn)在的操作
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
int dayOfMonth1 = now.getDayOfMonth();
int hour1 = now.getHour();
int minute1 = now.getMinute();
int second = now.getSecond();
更加方便的是對(duì)時(shí)間/日期的操作
LocalDateTime now = LocalDateTime.now();
int year = now.getYear();
int monthValue = now.getMonthValue();
int dayOfMonth = now.getDayOfMonth();
int dayOfMonth1 = now.getDayOfMonth();
int hour1 = now.getHour();
int minute1 = now.getMinute();
int second = now.getSecond();
//添加一天
LocalDateTime localDateTime = now.plusDays(1);
//添加一個(gè)小時(shí)
LocalDateTime localDateTime1 = now.plusHours(1);
//添加一分鐘;
LocalDateTime localDateTime2 = now.plusMinutes(1);
//添加一周
LocalDateTime localDateTime3 = now.plusWeeks(1);
//比較兩個(gè)時(shí)間的大小
int i = localDateTime.compareTo(localDateTime3);
localDateTime1.isAfter(localDateTime2);
localDateTime1.isBefore(localDateTime2);
localDateTime1.isEqual(localDateTime2);
//此外還提供了更加便捷的創(chuàng)建日歷趋观、時(shí)間的方式
LocalDateTime localDateTime4 = LocalDateTime.of(2020, 10, 21, 15, 30, 22);
LocalDate localDate = LocalDate.of(2020, 10, 21);
LocalTime localTime = LocalTime.of(12, 20, 12, 223);
//本月的第1天
localDate.with(TemporalAdjusters.firstDayOfMonth());
//下月的第1天
localDate.with(TemporalAdjusters.firstDayOfNextMonth());
localDate.with(TemporalAdjusters.firstDayOfNextYear());
//日期轉(zhuǎn)時(shí)間戳ZoneOffset.of("+8")設(shè)置的是時(shí)區(qū)
Long second1 = localDateTime4.toEpochSecond(ZoneOffset.of("+8"));
//獲取毫秒數(shù)
Long milliSecond = localDateTime4.toInstant(ZoneOffset.of("+8")).toEpochMilli();
//轉(zhuǎn)字符串
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
String format = localDateTime4.format(formatter);
//還有一種創(chuàng)建的時(shí)候帶時(shí)區(qū)的日歷類(方法相似就不做闡述了)
ZoneId zoneId = ZoneId.of("UTC+1");
ZonedDateTime zonedDateTime = ZonedDateTime.now(zoneId);
在實(shí)際開發(fā)中靈活運(yùn)用可以有效地提高開發(fā)的效率和代碼質(zhì)量扛禽。