Efficient farewell 996, opening the door 2-7 java programming efficient combat: determination logic parametric - anonymous class

1.1 Key

1.2 Programming Code

 

 

1.1 Key

Use an anonymous inner class, but on the risk of memory leaks have been resolved there is no obvious solution

 

 

1.2 Programming Code

Commodity same entity class :( 2-3)

:( same enumeration class 2-3)

 

Goods class:

Package Penalty for com.imooc.zhangxiaoxi.lambda.cart; Import java.util.ArrayList; Import java.util.List; public class CartService { / ** * anonymous inner classes, there is a memory leak risk, not production projects referenced, but to back up the lambda expression references * / Private static List <Sku> = skuList new new the ArrayList <Sku> () {{the Add ( new new Sku (2020001, "UAV", 999.00,1,999.00 , SkuCategoryEnum.ELECTRONICS)); the Add ( new new Sku (2,020,002, "T-shirt", 50.00,2,100.00 , SkuCategoryEnum.CLOTHING)); the Add ( new new Sku (2020003, "of Human Bondage", 30.00,1,30.00 , SkuCategoryEnum.BOOKS)); the Add ( new new Sku (2020004, "Old Man", 20.00,1,20.00 , SkuCategoryEnum.BOOKS)); the Add (

new Sku (2020005, "prove safety and efficient programming", 288.00,1,288.00 , SkuCategoryEnum.BOOKS)); the Add ( new new Sku (2,020,006, "bulk shoes", 300.00,1,300.00 , SkuCategoryEnum.CLOTHING)); the Add ( new new Sku (2020007 "barbell", 2000.00,1,2000.00 , SkuCategoryEnum.SPROTS)); the Add ( new new Sku (2020008, "the ThinkPad", 5000.00,1,5000.00 , SkuCategoryEnum.ELECTRONICS));}}; public static List <Sku> getSkuList () { return skuList;} / ** * * versionTestA find the shopping cart all electronic products * * Note for loop * for (Sku sku: skuList) using * @return * / public static List <Sku> getElectronicsSkuList (List <Sku> skuList) {List <Sku> = lElectronicsList new new the ArrayList <Sku>(); for(SKU Sku: skuList) { IF (. Sku.getSkuCategory () the equals (SkuCategoryEnum.ELECTRONICS)) {lElectronicsList.add (SKU);}} / * for (int I = 0; I <skuList.size (); I ++ ) {IF (skuList.get (I) .getSkuCategory () the equals (SkuCategoryEnum.ELECTRONICS)) {lElectronicsList.add (skuList.get (I));.}} * / return lElectronicsList;} / ** * * versionTestB single dimensions condition parameters of the incoming different enum, print out a list of different products * * Note that the second argument: how the transfer of enumeration class parameters * * * @param skuList * @return * / public static list <Sku> filterSkuListByParam (List <Sku> skuList, skuCategoryEnum skuCategoryEnum) {List <Sku> = lElectronicsList new new the ArrayList <Sku> (); for (0 = I int; I <skuList.size (); I ++ ) { IF (skuList.get (I) .getSkuCategory () the equals (skuCategoryEnum)) {lElectronicsList.add (skuList.get (I));.}} return lElectronicsList;} / ** * versionTestC * * * egg and then a method may be one of the parameters is determined according to two parameters, * * If flag is true, enumerated type is determined, otherwise according to the total price is determined * * @param skuList * @return * / public static List <Sku> filterSkuListByParamB (List <Sku> skuList, skuCategoryEnum skuCategoryEnum, dTotalPrice Double, Boolean In Flag) {List <Sku> = lElectronicsList new new the ArrayList <Sku> (); for ( int I = 0; I <skuList.size (); I ++ ) { IF ((&& In Flag. skuList.get (I) .getSkuCategory () the equals (skuCategoryEnum)) || (! && skuList.get In Flag (I) .getTotalPrice ()> dTotalPrice)) {lElectronicsList.add (skuList.get (I));}} return lElectronicsList;} / ** * * versionTestD such an approach is not recommended for production, the class will cause an explosion. All to the lambda expression is introduced * * @param skuList * @return * / public static List <Sku> filterSkuListByParamPrediCate (List <Sku> skuList, SkuPrediCate skuPrediCate) {List <Sku> = lElectronicsList new new the ArrayList <Sku> (); for ( int I = 0; I <skuList.size (); I ++ ) { IF (skuPrediCate.test (skuList.get (I))) {lElectronicsList.add (skuList.get (I));lElectronicsList; }}

 

Predicate Interface:

Package com.imooc.zhangxiaoxi.lambda.cart; / **  * verb interfaces * / public interface SkuPrediCate { Boolean Test (Sku SKU);}


 

 

 

Test categories:

Package com.imooc.zhangxiaoxi.lambda.cart; 

Import com.alibaba.fastjson.JSON;
 Import org.junit.Test; 

Import java.util.List; 

public  class VersionTestE { 

    @Test 
   public  void filterSkuListByParam () { 
       List <Sku> = skuList CartService.getSkuList (); 

        / ** 
         * query using anonymous inner classes total greater than 1000 
         * / 
        List <Sku> getParamSkuList = CartService.filterSkuListByParamPrediCate (skuList, new new SkuPrediCate () { 
            @Override 
            public  Boolean Test (Sku SKU) {
                 return sku.getTotalPrice() > 1000;
            }
        });
        System.out.println(JSON.toJSONString(getParamSkuList,true));
    }

}

 

Print log:

[
    {
        "skuCategory":"SPROTS",
        "skuId":2020007,
        "skuName":"杠铃",
        "skuPrice":2000.0,
        "totalNum":1,
        "totalPrice":2000.0
    },
    {
        "skuCategory":"ELECTRONICS",
        "skuId":2020008,
        "skuName":"ThinkPad",
        "skuPrice":5000.0,
        "totalNum":1,
        "totalPrice":5000.0
    }
]

Process finished with exit code 0

 

Guess you like

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