1. 接口default 與 static 關鍵字html
/** * jdk8中接口可使用聲明default和static修飾的方法 * static修飾的方法和普通的方法同樣,能夠被直接調用 * default修飾的方法有方法體,就和普通的方法同樣,能夠被重寫,有點像抽象類的方法同樣,可是java是單繼承多實現的 */ public interface Today { void dream(); void striver(); default void victory(){ System.out.println("將來"); } static void test(){ System.out.println("接口裏的靜態方法"); } // jdk9 中還新增了private方法 private void test3() { System.out.println("私有方法"); }; }
2. base64 apijava
import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.util.Base64; /** * Base64是⽹絡上最多見的用於傳輸8Bit字節碼的編碼方式之一,Base64就是 * 一種基於64個可打印字符來表示二進制數據的方法 基於64個字符A-Z,a-z,0-9,+,/的編碼方式, * 是一種能將任意二進制數據用64種字元組合成字符串的方法, */ public class Main { public static void main(String[] args) throws Exception { /** * jdk8之前的寫法 * 編碼和解碼的效率⽐較差,公開信息說之後的版本會取消這個⽅法 */ BASE64Encoder encoder = new BASE64Encoder(); BASE64Decoder decoder = new BASE64Decoder(); byte[] textByte = "聖魔導師".getBytes("UTF-8"); //編碼 String encodedText = encoder.encode(textByte); System.out.println(encodedText);//5Zyj6a2U5a+85biI //解碼 System.out.println(new String(decoder.decodeBuffer(encodedText),"UTF-8"));//聖魔導師 /** * jdk8的寫法 * 編解碼銷量遠⼤於 sun.misc 和 Apache Commons Codec,能夠本身動手壓測一下速度 */ Base64.Decoder decoder2 = Base64.getDecoder(); Base64.Encoder encoder2 = Base64.getEncoder(); byte[] textByte2 = "聖魔導師".getBytes("UTF-8"); //編碼 String encodedText2 = encoder2.encodeToString(textByte2); System.out.println(encodedText);//5Zyj6a2U5a+85biI //解碼 System.out.println(new String(decoder2.decode(encodedText2), "UTF-8"));//聖魔導師 } }
3. 簡便的日期工具類mysql
import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; /** * SimpleDateFormat,Calendar等類的 API設計⽐較差,⽇期/時間對象⽐較,加減麻煩,Date 仍是⾮線程安全的; * 因此 Java 8經過發佈新的 java.time裏的工具類來簡化處理,如⽇期/時間的⽐較,加減,格式化。 * 核⼼類: * LocalDate:不包含具體時間的⽇期。 * LocalTime:不含⽇期的時間。 * LocalDateTime:包含了⽇期及時間。 */ public class Main { public static void main(String[] args) throws Exception { Main main = new Main(); LocalTime localTime = LocalTime.now(); System.out.println("如今時間:"+localTime);// 如今時間:23:00:49.476 System.out.println("============= localTime經常使用方法 ============="); main.localTime(); System.out.println("============= localDateTime經常使用方法 ============="); main.localDateTime(); } public void localTime(){ LocalDate today = LocalDate.now(); System.out.println("今天⽇期:" + today);// 今天⽇期:2019-11-07 //獲取年,⽉,⽇,周⼏ System.out.println("如今是哪年:"+today.getYear());// 如今是哪年:2019 System.out.println("如今是哪⽉(英文):"+today.getMonth());// 如今是哪⽉(英文):NOVEMBER System.out.println("如今是哪⽉(數字):"+today.getMonthValue());// 如今是哪⽉(數字):11 System.out.println("如今是⼏號:"+today.getDayOfMonth());// 如今是⼏號:7 System.out.println("如今是周⼏:"+today.getDayOfWeek());// 如今是周⼏:THURSDAY //加減年份, 加後返回的對象纔是修改的,舊的依然是舊的 LocalDate changeDate = today.plusYears(1); System.out.println("加後是哪年:"+changeDate.getYear());// 加後是哪年:2020 System.out.println("舊的是哪年:"+today.getYear());// 舊的是哪年:2019 //日期比較 System.out.println("isAfter:"+changeDate.isAfter(today));// isAfter:true /* //getYear() int 獲取當前⽇期的年份 //getMonth() Month 獲取當前⽇期的⽉份對象 //getMonthValue() int 獲取當前⽇期是第⼏⽉ //getDayOfWeek() DayOfWeek 表示該對象表示的⽇期是星期⼏ //getDayOfMonth() int 表示該對象表示的⽇期是這個⽉第⼏天 //getDayOfYear() int 表示該對象表示的⽇期是今年第⼏天 //withYear(int year) LocalDate 修改當前對象的年份 //withMonth(int month) LocalDate 修改當前對象的⽉份 //withDayOfMonth(int dayOfMonth) LocalDate 修改當前對象在當⽉的⽇期 //plusYears(long yearsToAdd) LocalDate 當前對象增長指定的年份數 //plusMonths(long monthsToAdd) LocalDate 當前對象增長指定的⽉份數 //plusWeeks(long weeksToAdd) LocalDate 當前對象增長指定的週數 //plusDays(long daysToAdd) LocalDate 當前對象增長指定的天數 //minusYears(long yearsToSubtract) LocalDate 當前對象減去指定的年數 //minusMonths(long monthsToSubtract) LocalDate 當前對象減去註定的⽉數 //minusWeeks(long weeksToSubtract) LocalDate 當前對象減去指定的週數 //minusDays(long daysToSubtract) LocalDate 當前對象減去指定的天數 //compareTo(ChronoLocalDate other) int ⽐較當前對象和other對象在時間上的⼤⼩,返回值若是爲正,則當前對象時間較晚, //isBefore(ChronoLocalDate other) boolean ⽐較當前對象⽇期是否在other對象⽇期以前 //isAfter(ChronoLocalDate other) boolean ⽐較當前對象⽇期是否在other對象⽇期以後 //isEqual(ChronoLocalDate other) boolean ⽐較兩個⽇期對象是否相等 */ } public void localDateTime(){ // DateTimeFormatter是線程安全的SimpleDateFormat LocalDateTime ldt = LocalDateTime.now(); System.out.println(ldt);// 2019-11-07T23:12:29.056 DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); String ldtStr = dtf.format(ldt); System.out.println(ldtStr);// 2019-11-07 23:12:29 // 獲取指定的⽇期時間對象 LocalDateTime ldt2 = LocalDateTime.of(2020, 11, 11, 8, 20, 30); System.out.println(ldt2);// 2020-11-11T08:20:30 // 計算⽇期時間差 java.time.Duration LocalDateTime today = LocalDateTime.now(); System.out.println(today);// 2019-11-07T23:12:29.070 LocalDateTime changeDate = LocalDateTime.of(2020,10,1,10,40,30); System.out.println(changeDate);// 2020-10-01T10:40:30 Duration duration = Duration.between( today,changeDate);//第⼆個參數減第⼀個參數 System.out.println(duration.toDays());//兩個時間差的天數 System.out.println(duration.toHours());//兩個時間差的⼩時數 System.out.println(duration.toMinutes());//兩個時間差的分鐘數 System.out.println(duration.toMillis());//兩個時間差的毫秒數 System.out.println(duration.toNanos());//兩個時間差的納秒數 } }
4. lambda 與 函數式編程linux
import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; /** * 面向對象編程是對數據的抽象(各類各樣的POJO類); * 函數式編程則是對行爲的抽象(將行爲做爲一個參數進行傳遞) * 所謂的函數編程,便可理解是將一個函數(也稱爲「行爲」)做爲⼀個參數進行傳遞 * * lambda表達式 使⽤場景:接口中只能有一個方法, * 好比Runnable接口裏的run方法;Comparator接口裏的compareTo方法 * Lambda 表達式的實現方式在本質是以匿名內部類的方式進行實現的 */ public class Main { public static void main(String[] args) throws Exception { new Thread(new Runnable() { @Override public void run() { System.out.println("jdk8之前建立線程"); } }); //()對應run()沒有一個參數,->後面是方法體內容 //若是{}中的代碼只有⼀行,⽆論有返回值,能夠省略{}、return、分號,其餘則須要加上 new Thread(()-> System.out.println("lambda表達式建立線程")); List<String> list = Arrays.asList("a","c","d","b","e"); // jdk8之前排序 Collections.sort(list, new Comparator<String>() { @Override public int compare(String o1, String o2) { return o2.compareTo(o1); } }); // lambda表達式排序 //,前面的對應接口前面的參數,a b 對應compare裏面的參數 Collections.sort(list,(a,b)->b.compareTo(a)); } } ================== 自定義函數是編程(1) ==================== /** * 像Runnable接口同樣用FunctionalInterface註解修飾 * 加了這個註解後,接口裏面必須有且只能有一個方法 */ @FunctionalInterface public interface Test1<R,T>{ // R表示返回值,T表示參數類型,t1 t2是具體參數 R operator(T t1, T t2); } public class Compute { /** * 定義一個函數方法 * 須要傳入a和b兩個參數, * 後面的Test1<Integer,Integer> of就是傳入的一個函數(行爲),of是隨便起的一個別名 */ public static Integer operator(Integer a,Integer b,Test1<Integer,Integer> of){ return of.operator(a,b); } } public class Main { public static void main(String[] args) throws Exception { System.out.println(Compute.operator(2,3,(a,b)-> a+b)); System.out.println(Compute.operator(2,3,(a,b)-> a-b)); System.out.println(Compute.operator(2,3,(a,b)-> a*b)); System.out.println(Compute.operator(2,3,(a,b)-> a/b)); } } ================== 自定義函數是編程(2) ==================== @FunctionalInterface public interface Test2{ void test(); } public class Main { public static void main(String[] args) throws Exception { Main.casesc(()-> System.out.println("函數式編程案例二")); } public static void casesc(Test2 t){ t.test(); } }
5. 四大核心函數式接口git
Lambda表達式必須先定義接口,建立相關方法以後才能調用,這樣作十分不便,其實java8已經內置了許多接口, 例以下面四個功能型接口.全部標註@FunctionalInterface註解的接口都是函數式接口redis
public class Main { public static void main(String[] args) throws Exception { Consumer<String> c1 = obj->System.out.println(obj+": 調⽤用短信接⼝口發送短信,或者打印⽇日誌"); c1.accept("訂單id—001"); Consumer<List> c2 = obj->{ if(obj==null || obj.size()<1)return; obj.forEach(o-> System.out.println(o)); }; List<Integer> list = Arrays.asList(2,4,0,8,9,7); c2.accept(list); } }
public class Main { public static void main(String[] args) throws Exception { Student student = newStudent(); System.out.println(student); } public static Student newStudent(){ Supplier<Student> supplier = ()-> { Student student = new Student(); student.setName("默認名稱"); return student; }; return supplier.get(); } } class Student{ private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + '}'; } }
Function:只能接受一個參數 用法一(用一個類實現接口裏面的邏輯,而後直接調用) public class FunctionObj implements Function { @Override public Object apply(Object o) { return "對參數:"+o+"通過處理後返回結果"; } } public class Main { public static void main(String[] args) throws Exception { System.out.println(test("hello",new FunctionObj())); } public static String test(String a ,FunctionObj functionObj){ return functionObj.apply(a).toString(); } } 用法二(左邊規定好參數和返回值類型,右邊寫方法體具體邏輯) public class Main { public static void main(String[] args) throws Exception { Function<Integer,Integer> func = p->{ return p*100; }; System.out.println(func.apply(12)); Function<Integer,Boolean> function = a->a<100; System.out.println(function.apply(97)); } } ================================================================= BiFunctionObj:能接受兩個參數 // 用法一 public class BiFunctionObj implements BiFunction { @Override public Object apply(Object o, Object o2) { return (Integer.valueOf(o.toString())+Integer.valueOf(o2.toString())); } } public class Main { public static void main(String[] args) throws Exception { System.out.println(test(2,5,new BiFunctionObj())); } public static Integer test(Integer a,Integer b, BiFunctionObj func){ return Integer.valueOf(func.apply(a,b).toString()); } } // 用法二 public class Main { public static void main(String[] args) throws Exception { BiFunction<Integer, Integer,Boolean> func1 = (a,b)->{ return a>b; }; System.out.println(func1.apply(1,5)); BiFunction<String, String,String> func2 = (a,b)->a+b; System.out.println(func2.apply("hellow","world")); } }
public class Main { public static void main(String[] args) throws Exception { Predicate<Integer> predicate = a->a>10; List<Integer> list = Arrays.asList(1,54,9,34,3); for(Integer l : list){ if(predicate.test(l)) System.out.println(l); } } }
6. 流操做spring
/** * Stream:經過將集合轉換爲這麼⼀種叫作 「流」的元素隊列,可以對集合中的每一個元素進行任意操做。總共分爲4個步驟: * 數據元素即是原始集合:如List、Set、Map等 * 生成流:能夠是串行流stream() 或者並行流 parallelStream() * 中間操做:能夠是 排序,聚合,過濾,轉換等 * 終端操做:統一收集返回一個流 * * 通常都採用stream,由於集合操做通常裏面就幾百條數據,多線程的並行流效率不必定就高,還會出現線程安全問題 */ public class Main { public static void main(String[] args) { List<String> list = Arrays.asList("張麻子","李蛋","王二狗","Angell"); List<Student> users = Arrays.asList(new Student("張三", 23), new Student("趙四", 24), new Student("二狗", 23), new Student("田七", 22), new Student("皮特", 20), new Student("Tony", 20), new Student("二柱子", 25)); /** * map:對集合的每一個對象作處理 */ List<String> collect = list.stream().map(obj->"哈哈"+obj).collect(Collectors.toList()); list.forEach(obj->System.out.println(obj)); System.out.println("----------------"); collect.forEach(obj->System.out.println(obj)); /** * filter:boolean判斷,用於條件過濾 */ System.out.println("----------------"); Set<String> set = list.stream().filter(obj->obj.length()>2).collect(Collectors.toSet()); set.forEach(obj->System.out.println(obj)); /** * sorted:對流進行天然排序 */ System.out.println("----------------"); Set<String> sorteds = list.stream().sorted().collect(Collectors.toSet()); sorteds.forEach(obj->System.out.println(obj)); // 自定義排序規則 // 根據長度排序(正序) System.out.println("----------------"); List<String> resultList = list.stream().sorted(Comparator.comparing(obj -> obj.length())).collect(Collectors.toList()); resultList.forEach(obj->System.out.println(obj)); System.out.println("----------------"); // 根據長度排序(倒序) List<String> resultList2 = list.stream().sorted(Comparator.comparing(obj -> obj.length(),Comparator.reverseOrder())).collect(Collectors.toList()); resultList2.forEach(obj->System.out.println(obj)); System.out.println("----------------"); // 手動指定排序規則(根據年齡大小排序) List<Student> collect2 = users.stream().sorted( Comparator.comparing(Student::getAge,(x,y)->{ if(x>y) { return 1; }else { return -1; } }) ).collect(Collectors.toList()); collect2.forEach(obj->System.out.println(obj.getAge()+" : "+obj.getProvince())); /** * limit:截取包含指定數量的元素 */ System.out.println("----------------"); List<String> collect3 = list.stream().limit(2).collect(Collectors.toList()); collect3.forEach(obj->System.out.println(obj)); /** * allMatch:匹配全部元素,只有所有符合才返回true */ System.out.println("----------------"); boolean flag = list.stream().allMatch(obj->obj.length()>2); System.out.println(flag); System.out.println("----------------"); /** * anyMatch:匹配全部元素,至少一個元素知足就爲true */ boolean flag2 = list.stream().anyMatch(obj->obj.length()>2); System.out.println(flag2); System.out.println("----------------"); /** * max和min:最大值和最小值 */ Optional<Student> max = users.stream().max(Comparator.comparingInt(Student::getAge)); System.out.println(max.get().getAge()+" : "+max.get().getProvince()); System.out.println("----------------"); Optional<Student> min = users.stream().min((s1, s2)->Integer.compare(s1.getAge(),s2.getAge())); System.out.println(min.get().getAge()+" : "+min.get().getProvince()); /** * reduce:對Stream中的元素進行計算後返回一個惟一的值 */ // 計算全部值的累加 int value = Stream.of(1, 2, 3, 4, 5).reduce((item1, item2) -> item1 + item2).get(); // 100做爲初始值,而後累加全部值 int value2 =Stream.of(1, 2, 3, 4, 5).reduce(100, (sum, item) -> sum + item); // 找出最大值 int value3 =Stream.of(1, 4, 5, 2, 3).reduce((x,y)->x>y?x:y).get(); System.out.println(value); System.out.println(value2); System.out.println(value3); } } class Student { private String province; private int age; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String province, int age) { this.age = age; this.province = province; } }
7. 終端操做收集器:Collectorsql
/** * 數據結構收集:Collectors */ public class Main { public static void main(String[] args) throws Exception { List<String> data = Arrays.asList("張三","王五","李四"); List<String> list = data.stream().collect(Collectors.toList()); Set<String> set = data.stream().collect(Collectors.toSet()); LinkedList<String> linkedList = data.stream().collect(Collectors.toCollection(LinkedList::new)); System.out.println(list); System.out.println(set); System.out.println(linkedList); /* Collectors.toMap() Collectors.toSet() Collectors.toCollection() :⽤用⾃自定義的實現Collection的數據結構收集 Collectors.toCollection(LinkedList::new) Collectors.toCollection(CopyOnWriteArrayList::new) Collectors.toCollection(TreeSet::new) */ } } //============================================================ /** * 拼接函數:joining */ public class Main { public static void main(String[] args) throws Exception { List<String> list = Arrays.asList("springBoot","springCloud","netty"); String result1 = list.stream().collect(Collectors.joining()); String result2 = list.stream().collect(Collectors.joining("——")); String result3 = list.stream().collect(Collectors.joining("—", "【","")); String result4 = Stream.of("hello", "world").collect(Collectors.joining("—", "【", "】")); System.out.println(result1); System.out.println(result2); System.out.println(result3); System.out.println(result4); } } //============================================================ /** * 分組:partitioningBy */ public class Main { public static void main(String[] args) throws Exception { List<String> list = Arrays.asList("sdfsdf","xxxx","bbb","bbb"); Map<Boolean, List<String>> collect = list.stream().collect(Collectors.partitioningBy(obj -> obj.length() > 3)); System.out.println(collect); } } //============================================================ /** * 分組:group by * 統計:counting */ public class Main { public static void main(String[] args) throws Exception { List<Student> students = Arrays.asList( new Student("⼴東", 23), new Student("⼴東", 24), new Student("⼴東", 23), new Student("北京", 22), new Student("北京", 20), new Student("北京", 20), new Student("海南", 25)); // 經過名稱分組 Map<String, List<Student>> listMap = students.stream().collect(Collectors.groupingBy(obj -> obj.getProvince())); listMap.forEach((key, value) -> { System.out.println("========"); System.out.println(key); value.forEach(obj -> { System.out.println(obj.getAge()); }); }); System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); // 根據名稱分組,並統計每一個分組的個數 Map<String, Long> map = students.stream().collect(Collectors.groupingBy(Student::getProvince, Collectors.counting())); map.forEach((key,value)->{ System.out.println(key+"省人數有"+value); }); } } class Student { private String province; private int age; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Student(String province, int age) { this.age = age; this.province = province; } } //============================================================ /** * 統計函數:summarizing */ public class Main { public static void main(String[] args) throws Exception { List<Student> students = Arrays.asList( new Student("⼴東", 23), new Student("⼴東", 24), new Student("⼴東", 23), new Student("北京", 22), new Student("北京", 20), new Student("北京", 20), new Student("海南", 25)); // summarizingInt; summarizingLong; summarizingDouble IntSummaryStatistics summaryStatistics = students.stream().collect(Collectors.summarizingInt(Student::getAge)); System.out.println("平均值:" + summaryStatistics.getAverage()); System.out.println("人數:" + summaryStatistics.getCount()); System.out.println("最大值:" + summaryStatistics.getMax()); System.out.println("最小值:" + summaryStatistics.getMin()); System.out.println("總和:" + summaryStatistics.getSum()); } } /** * reduce:對Stream中的元素進行計算後返回一個惟一的值 */ // 計算全部值的累加 int value = Stream.of(1, 2, 3, 4, 5).reduce((item1, item2) -> item1 + item2).get(); // 100做爲初始值,而後累加全部值 int value2 =Stream.of(1, 2, 3, 4, 5).reduce(100, (sum, item) -> sum + item); // 找出最大值 int value3 =Stream.of(1, 4, 5, 2, 3).reduce((x,y)->x>y?x:y).get(); System.out.println(value); System.out.println(value2); System.out.println(value3);
1. try-with-resource編程
import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; public class Main { public static void main(String[] args) throws Exception { String path = "/Users/jack/Desktop/t.txt"; /** * jdk 1.7之前關閉資源通常是在finally裏面操做的 */ OutputStream out = new FileOutputStream(path); try { out.write(("一壺漂泊,浪跡天涯難入喉;你走以後,酒暖回憶思念瘦").getBytes()); } catch (Exception e) { e.printStackTrace(); }finally { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * jdk1.7的時候,能夠在try()裏聲明資源,會在try-catch代碼塊結束後自動關閉掉。 * try結束後自動調用的close方法,這個動做會早於finally裏調用的方法 * bu管是否出現異常,try()裏的實例都會被調用close方法 * try裏面能夠聲明多個自動關閉的對象,越早聲明的對象,會越晚被close掉 */ try (OutputStream out2 = new FileOutputStream(path);){ out2.write(("一壺漂泊,浪跡天涯難入喉;你走以後,酒暖回憶思念瘦").getBytes()); } catch (Exception e) { e.printStackTrace(); } /** * jdk1.9以後,對try()作了改進,在try外進行初始化,在括號內引用 */ OutputStream out3 = new FileOutputStream(path); try (out3) { out3.write(("一壺漂泊,浪跡天涯難入喉;你走以後,酒暖回憶思念瘦").getBytes()); } catch (Exception e) { e.printStackTrace(); } } }
2. streamjson
public class Main { /** * jdk9中的Stream流新增了兩個api */ public static void main(String[] args) throws Exception { /** * takeWhile:遍歷每一個對象,直到遇到第⼀個false時,返回前面全部元素,若是沒有false,將返回⼀一個空的 Stream */ List<String> list1 = List.of("springboot","java","html","","git").stream() .takeWhile(obj->!obj.isEmpty()).collect(Collectors.toList()); System.out.println(list1); /** * dropWhile:與takeWhile相反 */ List<String> list2 = List.of("springboot","java","html","","git").stream() .dropWhile(obj->!obj.isEmpty()).collect(Collectors.toList()); System.out.println(list2); } }
3. of建立只讀集合
public class Main { public static void main(String[] args) throws Exception { /** * JDK9以前建立只讀集合 */ List<String> list = new ArrayList<String>(); list.add("張三"); list.add("李四"); list.add("王五"); list.remove(0); System.out.println(list); //設置爲只讀List集合 // unmodifiableMap(map); unmodifiableMap(set); list = Collections.unmodifiableList(list); // 報錯:java.lang.UnsupportedOperationException //list.remove(0); System.out.println(list); /** * jdk9建立只讀集合 */ List<String> list2 = List.of("mysql", "linux", "redis"); list2.remove(0); System.out.println(list2); } }
1. var做爲局部變量類型推斷標識符
public class Main { // var做爲局部變量類型推斷標識符 public static void main(String[] args) throws Exception { var strVar = "springboot"; System.out.println(strVar instanceof String); //根據10L 推斷long 類型 var longVar = Long.valueOf(10l); System.out.println(longVar instanceof Long); //根據 true推斷 boolean 類型 var flag = Boolean.valueOf("true"); System.out.println(flag instanceof Boolean); // 推斷 ArrayList<String> var listVar = new ArrayList<String>(); System.out.println(listVar instanceof ArrayList); // 推斷 Stream<String> var streamVar = Stream.of("aa", "bb", "cc"); System.out.println(streamVar instanceof Stream); if(flag){ System.out.println("這個是 flag 變量,值爲true"); } for (var i = 0; i < 10; i++) { System.out.println(i); } try (var input = new FileInputStream("validation.txt")) { } } }
1. httpclinet
import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.time.Duration; import java.util.concurrent.CompletableFuture; /** * 這個功能在JDK9中引入,在JDK10中獲得了更新,在JDK11才發佈 */ public class Main { private static final URI uri = URI.create("https://www.cnblogs.com/wlwl/"); public static void main(String[] args) { testHttp2(); } /** * get請求 */ private static void testGet() { // 建立鏈接兩種方式: var httpClient = HttpClient.newHttpClient(); var httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofMillis(5000)).build(); // 封裝請求參數(默認get請求) HttpRequest request = HttpRequest.newBuilder().timeout(Duration.ofMillis(3000)) .header("key1", "v1") .uri(uri).build(); try { var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } catch (Exception e) { e.printStackTrace(); } } /** * post請求 */ private static void testPost() { HttpClient httpClient = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder().uri(uri) .POST(HttpRequest.BodyPublishers.ofString("phone=13113777337&pwd=1234567890")) // from表單要用下面格式發送 //.header("Content-Type", "application/json") //.POST(HttpRequest.BodyPublishers.ofString("{\"phone\":\"13113777337\",\"pwd\":\"1234567890\"}")) .build(); try { var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); } catch (Exception e) { e.printStackTrace(); } } /** * 異步GET請求 */ private static void testAsynGet() { var httpClient = HttpClient.newBuilder().build(); var request = HttpRequest.newBuilder().timeout(Duration.ofMillis(3000)) .header("key1", "v1") .uri(uri).build(); try { // 異步請求經過CompletableFuture實現 CompletableFuture<String> result = httpClient.sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body); System.out.println(result.get()); } catch (Exception e) { e.printStackTrace(); } } /** * 發送http2請求 * HTTP2協議的強制要求https,若是目標URI是HTTP的,則沒法使用HTTP 2協議 */ private static void testHttp2() { var httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofMillis(3000)) .version(HttpClient.Version.HTTP_2) .build(); var request = HttpRequest.newBuilder().timeout(Duration.ofMillis(3000)) .header("key1", "v1") .uri(uri) .build(); try { var response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body()); System.out.println(response.version()); } catch (Exception e) { e.printStackTrace(); } } }
1. switch
public class Main { public static void main(String[] args) { testOldSwitch1(); } /** * 舊:沒有break,則匹配的case後⾯面會⼀一直輸出 */ public static void testOldSwitch1(){ int i = 1; switch(i){ case 0: System.out.println("zero"); //break; case 1: System.out.println("one"); //break; case 2: System.out.println("two"); //break; default: System.out.println("default"); } } /** * 新:使用箭頭函數,不用聲明break,會自動終止,支持多個值匹配,使用逗號分隔 */ public void testNewSwitch(int i){ switch(i){ case 0 -> { System.out.println("zero"); System.out.println("這是多⾏行行語句句"); } case 1,11,111 -> System.out.println("one"); case 2 -> System.out.println("two"); default -> System.out.println("default"); } } }
2. 多行文本塊
public class Main { public static void main(String[] args) { /** * 舊:在java代碼裏面編寫多行源碼帶有特殊字符則須要轉義,如HTML,sql等 */ String html = "<html>\n" + "<body>\n" + "<p>Hello, world</p>\n" + "</body>\n" + "</html>\n"; String query = "SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB`\n" + "WHERE `CITY` = 'INDIANAPOLIS'\n" + "ORDER BY `EMP_ID`, `LAST_NAME`;\n"; /** * 新:不用對轉義字符進行轉義 */ String html2 = """" <html> <body> <p>Hello, world</p> </body> </html> """; String query = """ SELECT `EMP_ID`, `LAST_NAME` FROM `EMPLOYEE_TB` WHERE `CITY` = 'INDIANAPOLIS' ORDER BY `EMP_ID`, `LAST_NAME`; """; } }