JDK1.8部分新特性

public static void main(String[] args) {
        List<Integer> list = Arrays.asList(3, 7, 2, 6);

        //老版本排序方法
        Collections.sort(names, new Comparator<String>() {
        @Override
        public int compare(String a, String b) {
            return b.compareTo(a);
            }
        });

        //Lambda 表达式,java函数式编程
        Collections.sort(list, Comparator.naturalOrder());//顺序排序
        Collections.sort(list, Integer::compareTo);//顺序排序另一种写法
        Collections.sort(list, Comparator.reverseOrder());//倒序排序

        //foreach循环
        list.forEach(maps -> {
            System.out.println(maps);
        });
        
        //map的循环
        Map<Integer,Object> map=new HashMap<>();
        map.put(1, "one");
        map.put(2, "two");
        map.put(3, "three");
        map.forEach((k, v) -> System.out.println(k + "=" + v));
    

        //java的链式编程,其中StringBuilder就是其最为典型
        StringBuilder builder = new StringBuilder();
        builder.append("blake").append("bob").append("alice").append("linese").append("eve");
        System.out.println(builder);

        //链式编程的原理其实就是方法最后返回当前对象this
        //jdk中 StringBuffer 源码
        //@Override
        //public StringBuilder append(String str) {
        //      super.append(str);
        //      return this;
        // }
    }

链式编程实战使用如下:业务为商品价格计算,面向切面思想。

//价格计算类,链式编程模式
@Service
public class DoctorPrice {

    @Autowired
    private AnswerService answerService;

    @Autowired
    private UserService userService;


    private VisitCategoryEnum typeEnum;

    public static final int ZERO = 1;
    public static final int VIP_Free = 2;
    public static final int VIP_ZERO = 3;
    public static final int VIP_DISCOUNT = 4;
    public static final int ORIGINALPRICE = 5;

    public DoctorPrice setPriceType(VisitCategoryEnum priceType) {
        typeEnum = priceType;
        return this;
    }

    private long userId;//原价

    private long doctorId;//原价

    private Map<String, Object> result;

    public Map<String, Object> result() {
        return result;
    }

    public DoctorPrice setUserId(long userId) {
        this.userId = userId;
        return this;
    }

    public DoctorPrice setDoctorId(long doctorId) {
        this.doctorId = doctorId;
        return this;
    }

    public DoctorPrice show() {
        if (userId == 0 && doctorId == 0) {
            throw new ServiceException("医生或者用户id不能为空");
        }
        if (typeEnum == null) {
            throw new ServiceException("请设置价格的类型");
        }
        switch (typeEnum) {
            case graphic:
                result = getShowAnswerPrice();
                break;
            case phone:
                result = getShowPhonePrice();
                break;
            case Outpatient:
                result = getShowOutpatientPrice();
                break;
            case department:
                result = getShowDepartmentPrice();
                break;
            case video:
                result = getShowVideoPrice();
                break;
            default:
                break;
        }
        return this;
    }

    public DoctorPrice build() {
        if (userId == 0 && doctorId == 0) {
            throw new ServiceException("医生或者用户id不能为空");
        }
        if (typeEnum == null) {
            throw new ServiceException("请设置价格的类型");
        }
        switch (typeEnum) {
            case graphic:
                result = getBuildAnswerPrice();
                break;
            case phone:
                result = getBuildPhonePrice();
                break;
            case Outpatient:
                result = getBuildOutpatientPrice();
                break;
            case department:
                result = getBuildDepartmentPrice();
                break;
            case video:
                result = getBuildVidelPrice();
                break;
            default:
                break;
        }
        return this;
    }
}

//使用处
Map<String, Object> build = this.doctorPrice.setPriceType(visitCategoryEnum)
                .setUserId(userId)
                .setDoctorId(doctorId)
                .build()
                .result();
        int type = ModelUtil.getInt(build, "type");
        BigDecimal price = ModelUtil.getDec(build, "price", BigDecimal.ZERO);
        BigDecimal marketPrice = ModelUtil.getDec(build, "originalprice", BigDecimal.ZERO);
        BigDecimal originalprice = ModelUtil.getDec(build, "doctorprice", BigDecimal.ZERO);
        Double vipdiscount = ModelUtil.getDouble(build, "vipdiscount", 1);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值