996 bid farewell efficient, open the door to java programming and efficient method reference succinctly 2-15

2-14 and 2-12 repeated courses, so there is no issue to

Method 1 reference conditions

 Method of determining whether the reference scenario to meet the requirements of the time, we only need to look out into the ginseng and ginseng meets the interface requirements to



 

2 method reference scenario

 Four ways to reference types:

To a static method of Class :: staticMethod 
example of a method of an object point to an existing object :: instanceMethod
directed to any type of an example method C1ass :: instanceMethod
point Class :: new constructor

 

 

3 method references demo
package com.imooc.zhangxiaoxi.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * MethodReferenceTest
 *
 * Method of determining whether the reference scenario to meet the requirements of the time, we only need to look out into the reference parameters and meets the requirements of the interface can be
 *
 * @Author Wei Peas
 * @date 2020/4/6
 */
public class MethodReferenceTest {

    static class Sku{
        private String skuName;
        private Integer skuPrice;

        public Integer getSkuPrice() {
            return skuPrice;
        }

        /**
         * Static class method references
         * @Param Skul
         * @Param sku2
         * @Return 
         * / 
        public  static  int staUse (SKU sku1, Goa sku2) {
             return sku1.getSkuPrice () - sku2.getSkuPrice ();
        }

        public  int instanceComparePrice (SKU SKU) {
             return  this .getSkuPrice () - sku.getSkuPrice ();
        }
    }

    Class PriceComparator {
         public  int staUse2 (SKU sku1, Goa sku2) {
             return   sku1.getSkuPrice () - sku2.getSkuPrice ();
        }
    }

    /**
     * Sort receiving compare interfaces, which method compare method, the same type of receiving two parameters
     *
     * The case,
     * 1 two parameters (sku1, sku2) of the same type
     Their monovalent * 2 subtraction type return int
     * So they can be used as lambda sort of
     */
    public void test(){
        List<Sku> skuList = new ArrayList<Sku>();
        skuList.sort((sku1,sku2)->
                sku1.getSkuPrice()-sku2.getSkuPrice());


        // 1类名::方法名
        skuList.sort (SKU :: staUse);
        // 1展开 
        skuList.sort ((SKU sku1, Goa sku2) -> {
             return Sku.staUse (sku1, sku2);
        });

        @ 2 The method of the object instance name :: 
        PriceComparator priceComparator = new new PriceComparator ();
        skuList.sort(priceComparator::staUse2);
        // 2展开 
        skuList.sort ((SKU sku1, Goa sku2) -> {
             return priceComparator.staUse2 (sku1, sku2);
        });


        // 3 class name :: example of a method name 
        skuList.sort (Sku :: instanceComparePrice);
         // 3 expand 
        skuList.sort ((Sku Object, Sku SKU2) -> {
                     return object.instanceComparePrice (SKU2);
                }
        );

        // 4 reference point constructor 
     // skuList is empty, or null,
Optional.ofNullable (skuList) .orElseGet ( the ArrayList :: new new ); } }

 

 

 

Guess you like

Origin www.cnblogs.com/1446358788-qq/p/12640970.html