2019-04-26 Java8一些新特性

途中、感受

Local 日期時間類

LocalDate localDate = LocalDate.now();
LocalTime localTime = LocalTime.now();
LocalDateTime localDateTime = LocalDateTime.now();
System.out.println(localDate);
System.out.println(localTime);
System.out.println(localDateTime);
Instant instant = Instant.now();
OffsetDateTime offsetDateTime = instant.atOffset(ZoneOffset.ofHours(8));
System.out.println(instant);
System.out.println(offsetDateTime);

Lambda表達式

Comparator<Integer> com1 = new Comparator<Integer>() {
    @Override
    public int compare(Integer o1, Integer o2) {
      return Integer.compare(o1, o2);
    }
};
System.out.println(com1.compare(12,13));

System.out.println("===========");
// Lambda 表達式
Comparator<Integer> com2 = (o1, o2) -> Integer.compare(o1, o2);
System.out.println(com2.compare(12,13));

List<String> stringList = new ArrayList<>();
stringList.removeIf(str-> str.contains("aaa")); // 刪除List中包含aaa的

// 方法引用
Comparator<Integer> com3 = Integer::compare;
System.out.println(com3.compare(12,13));

方法引用
類(或?qū)ο螅?: 方法名
對象 :: 非靜態(tài)方法
類 :: 靜態(tài)方法
類 :: 非靜態(tài)方法

Consumer<String> con1 = str -> System.out.println(str);
con1.accept("北京");

Consumer<String> con2 = System.out::println;
con2.accept("北京");

Person person = new Person("張三", "23");
Supplier<String> supplier = () -> person.getName();
System.out.println(supplier.get());

Supplier<String> supplier1 = person::getName;
Function<Person, String> func3 = Person::getName;
System.out.println(supplier1.get());

Function<Double, Long> func = (o) -> Math.round(o);
func.apply(1.11);

Function<Double, Long> func1 = Math::round;
func1.apply(1.12);

Supplier<Person> func4 = () -> new Person();
Supplier<Person> func5 = Person::new; // new Person()

BiFunction<String, String, Person> fun6 = Person::new; // new Person(String name, String age)

Function<Integer, String[]> fun7 = String[]::new; // new String[Integer length]
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 《ilua》速成開發(fā)手冊3.0 官方用戶交流:iApp開發(fā)交流(1) 239547050iApp開發(fā)交流(2) 1...
    葉染柒丶閱讀 11,519評論 0 11
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴謹 對...
    cosWriter閱讀 11,674評論 1 32
  • java8新特性 原創(chuàng)者:文思 一、特性簡介 速度更快 代碼更少,增加了Lambda 強大的Stream API ...
    文思li閱讀 3,099評論 1 1
  • 一、重要數(shù)據(jù)結(jié)構(gòu)和JVM的改動 1.HashMap的改動 HashMap維護了一個Entry數(shù)組,put(K ke...
    一只愛java的貓閱讀 899評論 0 0
  • 前段時間一直在看lambda表達式,但是總感覺吃不透,在深入了解lambda表達式的時候,需要很多基礎(chǔ)的知識棧。這...
    西瓜真好吃丶閱讀 2,798評論 0 7

友情鏈接更多精彩內(nèi)容