将值分配给列表的最短代码

什么可能是最短的代码:


public void update(final Product object, final Callback<Product> callback) {

    if(object.getIsDayDependent()) {

        Double priceDay0 = object.getSundayPrice();

        Double priceDay1 = object.getMondayPrice();

        Double priceDay2 = object.getTuesdayPrice();

        Double priceDay3 = object.getWednesdayPrice();

        Double priceDay4 = object.getThursdayPrice();

        Double priceDay5 = object.getFridayPrice();

        Double priceDay6 = object.getSaturdayPrice();

        List<DayPrice> dayPrices = new LinkedList<>();

        dayPrices.add(new DayPrice(0, priceDay0));

        dayPrices.add(new DayPrice(1, priceDay1));

        dayPrices.add(new DayPrice(2, priceDay2));

        dayPrices.add(new DayPrice(3, priceDay3));

        dayPrices.add(new DayPrice(4, priceDay4));

        dayPrices.add(new DayPrice(5, priceDay5));

        dayPrices.add(new DayPrice(6, priceDay6));

        object.setDayDependent(dayPrices);

    } else {

        object.setPrice(null);

        object.setDayDependent(new LinkedList<>());

    }

    callback.onSuccess(object);

}


慕仙森
浏览 112回答 2
2回答

收到一只叮咚

下面的代码并没有像减少语句数量那样最小化代码,但它也消除了不必要的变量:if(object.getIsDayDependent()) {&nbsp; &nbsp; List<Double> prices = Arrays.asList(object.getSundayPrice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.getMondayPrice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.getTuesdayPrice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.getWednesdayPrice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.getThursdayPrice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.getFridayPrice(),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.getSaturdayPrice());&nbsp; &nbsp; object.setDayDependent(IntStream.range(0, 7)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .mapToObj(i -> new DayPrice(i, prices.get(i)))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toCollection(LinkedList<Double>::new)));} else {&nbsp; &nbsp; object.setPrice(null);&nbsp; &nbsp; object.setDayDependent(new LinkedList<>());}

翻阅古今

在不修改关联类(产品类)作为挑战的情况下,我为此拥有的最短代码是这样的:&nbsp; &nbsp; &nbsp; &nbsp; if(object.getIsDayDependent())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double priceDay0 = object.getSundayPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double priceDay1 = object.getMondayPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double priceDay2 = object.getTuesdayPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double priceDay3 = object.getWednesdayPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double priceDay4 = object.getThursdayPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double priceDay5 = object.getFridayPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Double priceDay6 = object.getSaturdayPrice();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; object.setDayDependent(new LinkedList<>(Arrays.asList(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DayPrice(0, priceDay0),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DayPrice(1, priceDay1),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DayPrice(2, priceDay2),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DayPrice(3, priceDay3),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DayPrice(4, priceDay4),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DayPrice(5, priceDay5),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new DayPrice(6, priceDay6))));&nbsp; &nbsp; &nbsp; &nbsp; }不多,但也许有人可以回答一些更聪明的方法。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java