赞
踩
int a = 0b110110
int a = 0b110_110
switch(str)
case a:
break;
case b:
break;
List<String> list = new Arraylist<>();
try(){
} catch(IOExcepton | NullPointerException e) {
}
private static void method() { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader("a.txt"); fw = new FileWriter("b.txt"); int ch = 0; while ((ch = fr.read()) != -1) { fw.write(ch); } } catch (IOException e) { e.printStackTrace(); } finally { if (fw != null) { fw.close(); } if (fw != null) { fr.close(); } } // 改进版的代码 try (FileReader fr = new FileReader("a.txt"); FileWriter fw = new FileWriter("b.txt");) { int ch = 0; while ((ch = fr.read()) != -1) { fw.write(ch); } } catch (IOException e) { e.printStackTrace(); } }
new Thread( () -> System.out.println("In Java8, Lambda expression rocks !!") ).start();
LocalDate.now(); // 获取当前时间
ZonedDateTime.now(Clock.systemUTC()); // 获取UTC时区的时间
(1)Optional.of(T t): 创建一个Optional 实例
(2)Optional.empty(): 创建一个空的Optional 实例
(3)Optional.ofNullable(T t):若t不为null,创建 Optional 实例,否则创建空实例
(4)isPresent():判断是否包含值 orElse(T t): 如果调用对象包含值,返回该值,否则返回t
(5)orElseGet(Supplier s):如果调用对象包含值,返回该值,否则返回s获取的值
(6)map(Function f):如果有值对其处理,并返回处理后的Optional,否则返回Optional.empty()
(7)flatMap(Function mapper):与 map 类似,要求返回值必须是Optional
Map<Integer, Integer> map = new HashMap<>(); map.put(10, 10); map.put(31, 31); map.put(53, 53); map.put(86, 86); map.put(20, 20); // 转list List<Integer> list = map.keySet().stream().collect(Collectors.toList()); // 过滤获取偶数 list = list.stream().filter(item -> item % 2 == 0).collect(Collectors.toList()); // 从大到小排序 list = list.stream().sorted((a, b) -> b - a).collect(Collectors.toList()); list.stream().forEach(item -> {System.out.println(item);});
public interface Test{
default void hello(){
System.out.println("我是接口默认方法,哈哈!");
}
}
静态方法:接口中可以实现静态方法,示例如下:
public interface Test {
public static void sysHello() {
System.out.println("你好啊,我是接口静态方法!");
}
}
public class Test03 { public static void main(String[] args) { Operater o=arr -> { int sum=0; for(int n:arr){ sum+=n; } System.out.println("数组的和为:"+sum); }; fun(o); } public static void fun(Operater operater){ int[] arr={2,3,4,5,6,7,11}; operater.getSum(arr); } } @FunctionalInterface interface Operater{ //求数组的和 public abstract void getSum(int[] arr); }
对象::实例方法
类::静态方法
类::实例方法
类::new
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。