高效 告别996,开启java高效编程之门 3-2传统方式处理业务逻辑
Posted 扈江离与辟芷兮,纫秋兰以为佩。
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了高效 告别996,开启java高效编程之门 3-2传统方式处理业务逻辑相关的知识,希望对你有一定的参考价值。
1 重点
1.1 对sort方法使用的理解
2 代码演练
需求:
根据第一章需求,女盆友提出需求
* 1 打印所有商品
* 2 图书类的商品一定给买
* 3 最贵的买两件
* 4 打印最贵的两件商品的名称和总价
测试类:
package com.imooc.zhangxiaoxi.stream; import com.alibaba.fastjson.JSON; import com.imooc.zhangxiaoxi.lambda.cart.CartService; import com.imooc.zhangxiaoxi.lambda.cart.Sku; import com.imooc.zhangxiaoxi.lambda.cart.SkuCategoryEnum; import org.junit.Test; import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; /** * StreamVs * * @author 魏豆豆 * @date 2020/4/7 */ public class StreamVs { /** * 流和普通方法对比,本节应用普通方法 * * 根据第一章需求,女盆友提出需求 * 1 打印所有商品 * 2 图书类的商品一定给买 * 3 最贵的买两件 * 4 打印最贵的两件商品的名称和总价 */ /** * 普通方法 */ @Test public void oldCartHandle(){ //1 打印所有商品 List<Sku> skuList = CartService.getSkuList(); System.out.println("===================================================================================================="); for(Sku sku: skuList){ System.out.println(JSON.toJSONString(sku,true)); } //2 排除图书类 List<Sku> skuListNoBooks = new ArrayList<Sku>(); for(Sku sku:skuList){ if(!sku.getSkuCategory().equals(SkuCategoryEnum.BOOKS)){ skuListNoBooks.add(sku); } } //3 最贵的买两件 skuListNoBooks.sort(new Comparator<Sku>() { @Override public int compare(Sku o1, Sku o2) { if(o1.getTotalPrice()> o2.getTotalPrice()){ return -1; }else if(o1.getTotalPrice() < o2.getTotalPrice()){ return 1; }else{ return 0; } } }); List<Sku> skuListPreTwo = new ArrayList<Sku>(); for(int i = 0;i<2;i++){ skuListPreTwo.add(skuListNoBooks.get(i)); } System.out.println("===================================================================================================="); Double totalPrice = 0.0; for(Sku sku:skuListPreTwo){ totalPrice+=sku.getTotalPrice(); } List<String> skuNameList = new ArrayList<String>(); for (Sku sku:skuListPreTwo){ skuNameList.add(sku.getSkuName()); } System.out.println("商品总价为:"+totalPrice); System.out.println("商品名称为:"+ JSON.toJSONString(skuNameList,true)); } }
其他类参考2-3:
打印日志:
D:javajdkjdk9jdk-9+181_windows-x64_rijava-se-9-rijdk-9injava.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4libidea_rt.jar=35416:D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4in" -Dfile.encoding=UTF-8 -classpath "D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4libidea_rt.jar;D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4pluginsjunitlibjunit-rt.jar;D:javadevolopKitideaanZhIntelliJ IDEA Community Edition 2018.1.4pluginsjunitlibjunit5-rt.jar;F:xiangmu3Xin est996 arget est-classes;F:xiangmu3Xin est996 argetclasses;F:xiangmu3Xin est996libcomgoogleguavaguava28.2-jreguava-28.2-jre.jar;F:xiangmu3Xin est996libcomgoogleguavafailureaccess1.0.1failureaccess-1.0.1.jar;F:xiangmu3Xin est996libcomgoogleguavalistenablefuture9999.0-empty-to-avoid-conflict-with-guavalistenablefuture-9999.0-empty-to-avoid-conflict-with-guava.jar;F:xiangmu3Xin est996libcomgooglecodefindbugsjsr3053.0.2jsr305-3.0.2.jar;F:xiangmu3Xin est996liborgcheckerframeworkchecker-qual2.10.0checker-qual-2.10.0.jar;F:xiangmu3Xin est996libcomgoogleerrorproneerror_prone_annotations2.3.4error_prone_annotations-2.3.4.jar;F:xiangmu3Xin est996libcomgooglej2objcj2objc-annotations1.3j2objc-annotations-1.3.jar;F:xiangmu3Xin est996libjunitjunit4.12junit-4.12.jar;F:xiangmu3Xin est996liborghamcresthamcrest-core1.3hamcrest-core-1.3.jar;F:xiangmu3Xin est996libcomalibabafastjson1.2.58fastjson-1.2.58.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit3 com.imooc.zhangxiaoxi.stream.StreamVs,oldCartHandle ==================================================================================================== { "skuCategory":"ELECTRONICS", "skuId":2020001, "skuName":"无人机", "skuPrice":999.0, "totalNum":1, "totalPrice":999.0 } { "skuCategory":"CLOTHING", "skuId":2020002, "skuName":"T-shirt", "skuPrice":50.0, "totalNum":2, "totalPrice":100.0 } { "skuCategory":"BOOKS", "skuId":2020003, "skuName":"人生的枷锁", "skuPrice":30.0, "totalNum":1, "totalPrice":30.0 } { "skuCategory":"BOOKS", "skuId":2020004, "skuName":"老人与海", "skuPrice":20.0, "totalNum":1, "totalPrice":20.0 } { "skuCategory":"BOOKS", "skuId":2020005, "skuName":"剑指高效编程", "skuPrice":288.0, "totalNum":1, "totalPrice":288.0 } { "skuCategory":"CLOTHING", "skuId":2020006, "skuName":"大头皮鞋", "skuPrice":300.0, "totalNum":1, "totalPrice":300.0 } { "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 } ==================================================================================================== 商品总价为:7000.0 商品名称为:[ "ThinkPad", "杠铃" ] Process finished with exit code 0
以上是关于高效 告别996,开启java高效编程之门 3-2传统方式处理业务逻辑的主要内容,如果未能解决你的问题,请参考以下文章