主頁 > 後端開發 > 吊打面試官@@@Java中jdk8新特性--Lambda運算式、函式式介面、Stream API

吊打面試官@@@Java中jdk8新特性--Lambda運算式、函式式介面、Stream API

2021-09-06 08:12:46 後端開發

一、jdk8新特性簡介

二、Lambda運算式

簡單理解一下Lambda運算式

public class LambdaTest {
    @Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("我愛北京天安門");
            }
        };
        r1.run();
        System.out.println("******************************************");
        Runnable r2 = () -> System.out.println("我愛北京故宮");
        r2.run();
    }
    @Test
    public void test2(){
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return Integer.compare(o1,o2);
            }
        };
        int compare1 = com1.compare(12,21);
        System.out.println(compare1);
        System.out.println("**********************************");
        //Lambda運算式的寫法
        Comparator<Integer> com2 = (o1,o2) -> Integer.compare(o1,o2);
        int compare2 = com2 .compare(32,21);
        System.out.println(compare2);
        System.out.println("************************************");
        //方法參考
        Comparator<Integer> com3 = Integer :: compare;
        int compare3 = com3.compare(32,21);
        System.out.println(compare2);
    }

Lambda運算式的使用

1.舉例:(o1,o2) -> Integer.compare(o1,o2);
2.格式:
->:lambda運算子 或 箭頭運算子
->:左邊:lambda形參串列 (其實就是介面中的抽象方法的形參串列)
->:右邊:lambda體 (其實就是重寫的抽象方法的方法體)
3.Lambda運算式的使用:(分為6種情況介紹)
總結:(重點看這個)
->左邊:lambda形參串列的引數型別可以省略(型別推斷);如果lambda形參串列只有一個引數,其一對()也可以省略

->右邊:lambda體應該使用一對{}包裹;如果lambda體只有一條執行陳述句(可能時return陳述句),可以省略這一對{}和return關鍵字
4.Lambda運算式的本質:作為函式式介面的實體
5.如果一個介面中,只宣告了一個抽象方法,則此介面就稱為函式式介面

public class LambdaTest1 {
    //語法格式一:無參,無回傳值
    @Test
    public void test1(){
        Runnable r1 = new Runnable() {
            @Override
            public void run() {
                System.out.println("我愛北京天安門");
            }
        };
        r1.run();
        System.out.println("******************************************");
        Runnable r2 = () -> {
            System.out.println("我愛北京故宮");
        };

        r2.run();
    }
    //語法格式二:Lambda需要一個引數,但是沒有回傳值,
    @Test
    public void test2(){
        Consumer<String> con = new Consumer<String>() {

            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        };
        con.accept("謊言和誓言的區別什么?");
        System.out.println("**********************************");
        Consumer<String>con1 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("一個是聽得人當真了,一個是說的人當真了");
    }
    //語法格式三:資料型別可以省略,因為可由編譯器推斷得出,稱為"型別推斷"
    @Test
    public void test3(){
        Consumer<String> con1 = (String s) -> {
            System.out.println(s);
        };
        con1.accept("一個是聽的人當真了,一個是說的人當真了");
        System.out.println("****************************");
        Consumer<String> con2 = (s) -> {
            System.out.println(s);
        };
        con2.accept("一個是聽的人當真了,一個是說的人當真了");
    }
    @Test
    public void test4(){
        ArrayList<String> objects = new ArrayList<>();//型別推斷(泛型)
        int[] arr = {1,2,3};//型別推斷(int[] arr = new int[]{1,2,3};)
    }
    //語法格式四:Lambda若只需要一個引數時,引數的小括號可以省略
    @Test
    public void test5(){
        Consumer<String> con1 = (s) -> {
            System.out.println(s);
        };
        con1.accept("一個是聽的人當真了,一個是說的人當真了");
        System.out.println("****************************");
        Consumer<String> con2 = s -> {
            System.out.println(s);
        };
        con2.accept("一個是聽的人當真了,一個是說的人當真了");
    }
    //語法格式五:Lambda需要兩個或兩個以上的引數,多條執行陳述句,并且可以有回傳值
    @Test
    public void test6(){
        Comparator<Integer> com1 = new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                System.out.println(o1);
                System.out.println(o2);
                return Integer.compare(o1,o2);
            }
        };
        System.out.println(com1.compare(12,21));
        System.out.println("*******************************");
        Comparator<Integer> com2 = (o1,o2) -> {
            System.out.println(o1);
            System.out.println(o2);
            return o1.compareTo(o2);
        };
        System.out.println(com2.compare(12,6));
    }
    //語法格式六:當Lambda體只有一條陳述句時,return與大括號若有,都可以省略
    @Test
    public void test7(){
        Comparator<Integer> com1 = (o1,o2) ->{
            return o1.compareTo(o2);
        };
        System.out.println(com1.compare(12,6));
        System.out.println("******************************");
        Comparator<Integer> com2 = (o1,o2) -> o1.compareTo(o2);
        System.out.println(com2.compare(12,21));
    }
    @Test
    public void test8(){
        Consumer<String> con1 = s -> {
            System.out.println(s);
        };
        con1.accept("一個是聽的人當真了,一個是說的人當真了");
        System.out.println("*********************************");

        Consumer<String> con2 = s -> System.out.println(s);
        con2.accept("一個是聽的人當真了,一個是說的人當真了");
    }
}

三、函式式介面

1.什么是函式式介面

2.如何理解函式式介面

總結:只有函式式介面在實體化的時候,才可以用上Lambda運算式!!!!!!!!!

3.Java內置四大核心函式式介面

java內置的4大核心函式式介面
 消費型介面Consumer<T>       void accept(T t)
 供給型介面Supplier<T>       T get()
 函式式介面Function<T,R>     R apply(T t)
 斷定型介面Predicate<T>      boolean test(T t)

public class LambdaTest2 {
    @Test
    public void test1(){
        happyTime(500, new Consumer<Double>() {
            @Override
            public void accept(Double aDouble) {
                System.out.println("學習太累了,去天上人間買了瓶礦泉水,價格為:" + aDouble);
            }
        });
        System.out.println("*************************************");
        happyTime(400,money -> System.out.println("學習太累了,去天上人間喝兩口礦泉水,價格為:" + money));
    }
    public void happyTime(double money, Consumer<Double> con){
        con.accept(money);
    }
    @Test
    public void test2(){
        List<String> list = Arrays.asList("北京", "南京", "天津", "東京", "西京", "普京");
        List<String> filterStrs = filterString(list, new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return s.contains("京");
            }
        });
        System.out.println(filterStrs);
        List<String> filterStrs1 = filterString(list, s -> s.contains("京"));
        System.out.println(filterStrs1);
    }
    //根據給定的規則,過濾集合中的字串,此規則由Predicate的方法決定
    public List<String> filterString(List<String> list,Predicate<String> pre){
        ArrayList<String> filterList = new ArrayList<>();
        for(String s : list){
            if(pre.test(s)){
                filterList.add(s);
            }
        }
        return filterList;
    }

四、方法參考與構造器參考

方法參考

1.使用情境:當要傳遞給Lambda體的操作,已經有實作的方法了,可以使用方法參考!
2.方法參考,本質上就是Lambda運算式,而Lambda運算式作為函式式介面的實體,所以,方法參考,也是函式式介面的實體,
3.使用格式:類(或物件) :: 方法名
4.具體分為如下的三種情況:
情況1 物件 :: 非靜態方法
情況2 類 :: 靜態方法
情況3 類 :: 非靜態方法
5.方法參考使用的要求:

> 要求介面中的抽象方法的形參串列和回傳值型別與方法參考的方法的形參串列和回傳值型別相同!(針對于情況1、2)

> 當函式式介面方法的第一個引數是需要參考方法的呼叫者,并且第二個引數是需要參考方法的引數(或無引數)時:ClassName::methodName(針對于情況3)

6.使用建議:

如果給函式式介面提供實體,恰好滿足方法參考的使用情境,大家就可以考慮使用方法參考給函式式介面提供實體,如果大家不熟悉方法參考,那么還可以使用lambda運算式,

 情況一:物件 :: 實體方法
    Consumer中的void accept(T t)
    PrintStream中的void println(T t)
    @Test
    public void test1(){
        Consumer<String> con1 = str -> System.out.println(str);
        con1.accept("北京");
        System.out.println("**************************");
        PrintStream ps = System.out;
        Consumer<String> con2 = ps :: println;
        con2.accept("beijing");
    }
    //Supplier中的T get()
    //Employee中的String getName()
    @Test
    public void test2(){
        Employee emp = new Employee(1001, "Tom", 23, 5600);
        Supplier<String> sup1 = () -> emp.getName();
        System.out.println(sup1.get());
        System.out.println("****************************");
        Supplier<String> sup2 = emp :: getName;
        System.out.println(sup2.get());
    }
    //情況二:類 :: 靜態方法
    //Comparator中的int compare(T t1,T t2)
    //Integer中的int compare(T t1,T t2)
    @Test
    public void test3(){
        Comparator<Integer> com1 = (t1, t2) -> Integer.compare(t1,t2);
        System.out.println(com1.compare(12,21));
        System.out.println("*********************************");
        Comparator<Integer> com2 = Integer :: compare;
        System.out.println(com2.compare(12,3));
    }
    //Function中的R apply(T t)
    //Math中的Long round(Double d)
    @Test
    public void test4(){
        Function<Double, Long> func = new Function<Double, Long>() {
            @Override
            public Long apply(Double d) {
                return Math.round(d);
            }
        };
        System.out.println("************************************");
        Function<Double,Long> func1 = d -> Math.round(d);
        System.out.println(func1.apply(12.3));

        System.out.println("*****************************");
        Function<Double,Long> func2 = Math :: round;
        System.out.println(func2.apply(12.6));
    }
    //情況三:類 :: 實體方法(有難度)
    //String 中的 int t1.compareTo(t2)
    @Test
    public void test5(){
        Comparator<String> com1 = (s1,s2) -> s1.compareTo(s2);
        System.out.println(com1.compare("abc","abd"));
        System.out.println("*****************************");
        Comparator<String> com2 = String :: compareTo;
        System.out.println(com2.compare("abd","abm"));
    }
    //BiPredicate中的boolean test(T t1,T t2);
    //String中的boolean t1.equals(t2)
    @Test
    public void test6(){
        BiPredicate<String,String> pre1 = (s1, s2) -> s1.equals(s2);
        System.out.println(pre1.test("abc","abc"));
        System.out.println("**************************");
        BiPredicate<String,String> pre2 = String :: equals;
        System.out.println(pre1.test("abc","abd"));
    }
    //Function中的R apply(T t)
    //Employee中的String getName();
    @Test
    public void test7(){
        Employee employee = new Employee(1001, "Jerry", 23, 6000);
        Function<Employee,String> func1 = e -> e.getName();
        System.out.println(func1.apply(employee));
        System.out.println("****************************");
        Function<Employee,String> func2 = Employee :: getName;
        System.out.println(func2.apply(employee));
    }

2.構造器參考和陣列參考

一、構造器參考
和方法參考類似,函式式介面的抽象方法的形參串列構造器的形參串列一致,
抽象方法的回傳值型別即為構造器所屬的類的型別
二、陣列參考
大家可以把陣列看作是一個特殊的類,則寫法與構造器參考一致,

構造器參考
    Supplier中的T get()
    Employee的空參構造器:Employee()
    @Test
    public void test1(){
        Supplier<Employee> sup = new Supplier<Employee>(){
            @Override
            public Employee get(){
                return new Employee();
            }
        };
        System.out.println("****************************");
        Supplier<Employee> sup1 = () -> new Employee();
        System.out.println(sup1.get());
        System.out.println("***********************************");
        Supplier<Employee> sup2 = Employee :: new;
        System.out.println(sup2.get());
    }
    //Function中的R apply(T t)
    @Test
    public void test2(){
        Function<Integer,Employee> func1 = id -> new Employee(id);
        Employee employee = func1.apply(1001);
        System.out.println(employee);
        System.out.println("********************************");
        Function<Integer,Employee> func2 = Employee :: new;
        Employee employee1 = func2.apply(1002);
        System.out.println(employee1);
    }
    //BiFunction中的R apply(T t,U u)
    @Test
    public void test3(){
        BiFunction<Integer,String,Employee> func1 = (id,name) -> new Employee(id,name);
        System.out.println(func1.apply(1001,"Tom"));
        System.out.println("**********************************");
        BiFunction<Integer,String,Employee> func2 = Employee :: new;
        System.out.println(func2.apply(1002,"Tom"));
    }
    //陣列參考
    //Function中的R apply(T t)
    @Test
    public void test4(){
        Function<Integer,String[]> func1 = length -> new String[length];
        String[] arr1 = func1.apply(5);
        System.out.println(Arrays.toString(arr1));
        System.out.println("************************");
        Function<Integer,String[]> func2 = String[] :: new;
        String[] arr2 = func2.apply(10);
        System.out.println(Arrays.toString(arr2));
    }

五、Stream API

1.Stream API的說明

2.為什么要使用Stream API

1.Stream關注的是對資料的運算,與CPU打交道
集合關注的是資料的存盤,與記憶體打交道

2.
A.Stream 自己不會存盤元素,
B.Stream 不會改變源物件,相反,他們會回傳一個持有結果的型Stream
C.Stream 操作是延遲執行的,這意味著他們會等到需要結果的時候才執行
3.Stream 執行流程
A.Stream的實體化
B.一系列的中間操作(過濾、映射、...)
C.終止操作


4.說明:
4.1 一個中間操作鏈,對資料源的資料進行處理
4.2 一旦執行終止操作,就執行中間操作鏈,并產生結果,之后,不會再被使用

3.創建Stream的四種方式

//創建Stream方式一:通過集合
    @Test
    public void test1(){
        List<Employee>employees = EmployeeData.getEmployees();
//        default Stream<E> stream(): 回傳一個順序流
        Stream<Employee> stream = employees.stream();
//        default Stream<E> parallelStream(): 回傳一個并行流
        Stream<Employee> parallelStream = employees.parallelStream();
    }
    //創建Stream方式二:通過陣列
    @Test
    public void test2(){
        int[] arr = new int[]{1,2,3,4,5,6};
        //呼叫Arrays類的static <T> stream(T[] array):回傳一個流
        IntStream stream = Arrays.stream(arr);
        Employee e1 = new Employee(1001,"Tom");
        Employee e2 = new Employee(1001,"Jerry");
        Employee[] arr1 = new Employee[]{e1,e2};
        Stream<Employee> stream1 = Arrays.stream(arr1);
    }
    //創建Stream方式三:通過Stream的of()
    @Test
    public void test3(){
        Steram<Integer> stream = Stream.of(1,2,3,4,5,6);
    }
    //創建Stream方式四:創建無限流(用的少)
    @Test
    public void test4(){
        //迭代
//        public static<T> Stream<T> iterate(final T seed,final UnaryOperator<T> f)
        //遍歷前10個偶數
        Stream.iterate(0,t -> t + 2).limit(10).forEach(System.out :: println);
        //生成
//        public static<T> Stream<T> generate(Supplier<T> s)
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }

4.Stream的中間操作及其測驗

    @Test
    public void test1(){
        List<Employee> list = EmployeeData.getEmployees();
//        filter(Predicate p)--接收Lambda,從流中排除某些元素
        Stream<Employee> stream = list.stream();
        //練習:查詢員工表中薪資大于7000的員工資訊
        stream.filter(e -> e.getSalary() > 7000).forEach(System.out::println);
        System.out.println();
//      limit(n)--截斷流,使其3元素不超過給定數量
        list.stream().limit(3).forEach(System.out::println);
        System.out.println();
//        skip(n) -- 跳過元素,回傳一個扔掉了前n個元素的流,若流中元素不足n個,則回傳一個空流,與limit(n)互補
        list.stream().skip(3).forEach(System.out::println);
//        distinct()--篩選,通過流所生成元素的hashCode()和equals()去除重復元素
        list.add(new Employee(1010,"劉強東",40,8000));
        list.add(new Employee(1010,"劉強東",40,8000));
        list.add(new Employee(1010,"劉強東",40,8000));
        list.add(new Employee(1010,"劉強東",40,8000));
        list.add(new Employee(1010,"劉強東",40,8000));
        list.stream().distinct().forEach(System.out::println);
    }
    //映射
    @Test
    public void test2(){
//        map(Function f)--接收一個函式作為引數,將元素轉換成其他形式或提取資訊,該函式
        List<String> list = Arrays.asList("aa","bb","cc","dd");
        list.stream().map(str -> str.toUpperCase()).forEach(System.out::println);
//        練習1:獲取員工姓名長度大于3的員工的姓名,
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<String> namesStream = employees.stream().map(employee::getName);
        namesStream.filter(name -> name.length() > 3).forEach(System.out::println);
//        練習2
        Stream<Stream<Character>> streamStream = list.stream().map(StreamAPITest1::fromStringToStream);
        streamStream.forEach(s ->{
            s.forEach(System.out::println);
        });
        System.out.println();
//        flatMap(Function f)--接受一個函式作為引數,將流中的每個值都換成另一個流,然后
        Stream<Character> characterStream = list.stream().flatMap(StreamAPITest1::fromStringToStream);
        characterStream.forEach(System.out::println);
    }
    //將多個字串中的多個字符構成的集合轉換為對應的Stream的實體
    public static Stream<Character> fromStringToStream(String str){
        ArrayList<Character> list = new ArrayList<>();
        for(Character c : str.toCharArray()){
            list.add(c);
        }
        return list.stream();
    }
    //幫助理解Stream映射的集合例子
    @Test
    public void test3(){
        ArrayList list1 = new ArrayList();
        list1.add(1);
        list1.add(2);
        list1.add(3);
        ArrayList list2 = new ArrayList();
        list2.add(4);
        list2.add(5);
        list2.add(6);
//        list1.add(list2);
        list1.addAll(list2);
        System.out.println(list1);
    }
    @Test
    public void test4(){
//        sorted()--自然排序
        List<Integer> list = Arrays.asList(12,43,65,34,87,0,-98,7);
        list.stream().sorted().forEach(System.out::println);
        //拋例外,原因:Employee沒有實作Comparable介面
        List<Employee> employees = EmployeeData.getEmployees();
        employees.stream().sorted((e1,e2) ->{
            int ageValue = Integer.compare(e1.getAge(),e2.getAge());
            if(ageValue != 0){
                return ageValue;
            }else{
                return Double.compare(e1.getSalary(),e2.getSalary());
            }
        }).forEach(System.out::println);
    }

5.Stream的終止操作及其測驗

Collector需要使用Collectors提供實體,

    @Test
    public void test1(){
        List<Employee> employees = EmployeeData.getEmployees();
//        allMatch(Predicate p)--檢查是否匹配所有元素
//        練習:是否所有的員工的年齡都大于18
        boolean allMatch = employees.stream().allMatch(e -> e.getAge() > 18);
        System.out.println(allMatch);
//        anyMatch(Predicate p)--檢查是否至少匹配一個元素
//        練習:是否存在員工的工資大于10000
        boolean anyMatch = employees.stream().anyMatch(e -> e.getSalary() > 10000);
        System.out.println(anyMatch);
//        noneMatch(Predicate p)--檢查是否沒有匹配的元素
//        練習:是否存在員工姓"雷"
        boolean noneMatch = employees.stream().noneMatch(e -> e.getName().startWith("雷"));
        System.out.println(noneMatch);
//        findFirst--回傳第一個元素
        Optional<Employee> employee = employees.stream().findFist();
        System.out.println(employee);
//        findAny--回傳當前流中的任意元素
        Optional<Employee> employees = employees.parallelStream().findAny();
        System.out.println(employee1);

    }
    @Test
    public void test2(){
//        count--回傳流中元素的總個數
        long count = employees.stream().filter(e -> e.getSalary() > 5000).count();
        System.out.println(count);
//        max(Comparator c)--回傳流中最大值
//        練習:回傳最高的工資:
        Stream<Double> salaryStream = employees.stream().map(e -> e.getSalary());
        Optional<Double> maxSalary = salaryStream.max(Double::compare);
        System.out.println(maxSalary);
//        min(Comparator c)--回傳流中最小值
//        練習:回傳最低工資的員工
        Optional<Employee> employee = employees.stream().min((e1,e2) -> Double.compare(e1.getSalary(),e2.getSalary()));
        System.out.println(employee);
//        forEach(Consumer c)--內部迭代
        employees.stream().forEach(System.out::println);
        employees.forEach(System.out::println);
    }
    //歸約
    @Test
    public void test3(){
//        reduce(T identity,BinaryOperator)--可以將流中元素反復結合起來,得到一個值,回傳T
//        練習1:計算1-10的自然數的和
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
        Integer sum = list.stream().reduce(0,Itneger::sum);
        System.out.println(sum);
//        reduce(BinaryOperator)--可以將流中元素反復結合起來,得到一個值,回傳Optional(T)
//        練習2:計算公司所有員工工資的總和
        List<Employee> employees = EmployeeData.getEmployees();
        Stream<Double> salaryStream = employees.stream().map(Employee::getSalary);
        Optional<Double> sumMoney = salaryStream.reduce((d1, d2) -> d1 + d2);
        System.out.println(sumMoney);
    }
    //3-收集
    @Test
    public void test4(){
//        collect(Collector c)--將流轉換為其他形式,接受一個Collector介面的實作,用于給Stream中元素做匯總的方法
//        練習1:查找工資大于6000的員工,結果回傳一個List或Set
        List<Employee> employees = EmployeeData.getEmployees();
        List<Employee> employeeList = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toList());
        employeeList.forEach(System.out::println);
        System.out.println();
        Set<Employee> employeeSet = employees.stream().filter(e -> e.getSalary() > 6000).collect(Collectors.toSet());
        employeeSet.forEach(System.out::println);
    }

六、Optional類的使用

Optional類的重要意義:

Optional類:為了在程式中避免出現空指標例外而創建的
常用的方法:ofNullable(T t)
orElse(T t)

 /*
    Optional.of(T t):創建一個Optional 實體,t必須非空
    Optional.empty():創建一個空的Optional實體
    Optional.ofNullable(T t):t可以為null
     */
    @Test
    public void test1(){
        Girl girl = new Girl();
        girl = null;
        //of(T t):保證t是非空的
        Optional<Girl> optionalGirl = Optional.of(girl);
    }
    @Test
    public void test2(){
        Girl girl = new Girl();
//        //ofNullable(T t):t可以為null
        Optional<Girl> optionalGirl = Optional.ofNullable(girl);
        System.out.println(optionalGirl);
        //orElse(T t1):如果當前的Optional內部封裝的t是非空的,則回傳內部的t.
        //入股偶內部的t是空的,則回傳orElse()方法中的引數t1.
        Girl girl1 = optionalGirl.orElse(new Girl("憨憨"));
        System.out.println(girl1);
    }
    public String getGirlName(Boy boy){
        return boy.getGirl().getName();
    }
    @Test
    public void test3(){
        Boy boy = new Boy();
        boy = null;
        String girlName = getGirlName(boy);
        System.out.println(girlName);
    }
    //優化以后的getGirlName():
    public String getGirlName1(Boy boy){
        if(boy != null){
            Girl girl = boy.getGirl();
            if(girl != null){
                return girl.getName();
            }
        }
        return null;
    }
    @Test
    public void test4(){
        Boy boy = new Boy();
        boy = null;
        String girlName = getGirlName1(boy);
        System.out.println(girlName);//null
    }
    //使用Optional類的getGirlName()
    public String getGirlName2(Boy boy){
        Optional<Boy> boyOptional = Optional.ofNullable(boy);
        //此時的boy1一定非空
        Boy boy1 = boyOptional.orElse(new Boy(new Girl("漂亮妹妹")));
        Girl girl = boy1.getGirl();
        Optional<Girl> girlOptional = Optional.ofNullable(girl);
        //girl1 一定非空
        Girl girl1 = girlOptional.orElse(new Girl("古力娜扎"));
        return girl1.getName();
    }
    @Test
    public void test5(){
        Boy boy = null;
        String girlName = getGirlName2(boy);
        System.out.println(girlName);
    }

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/297825.html

標籤:java

上一篇:Java 多執行緒《II》— 啟動執行緒的三種方式和原始碼分析

下一篇:HashMap實作原理和原始碼詳細分析

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more