對於可變列表取最小值

今天遇到一個問題,而之所以會遇到這種問題是因爲數據庫設計的問題.
數據表要保存一份年份的記錄,如2016年值1,2017年值是20,……目前需求是保存到2026年,也就是未來十年的數據.不知誰設計的表,居然是設計了10個字段分別保存這十個數據!!!!!!取值時,在根據年份寫了十個if語句來取值.額…….
現在問題來了,多條記錄時,要取得多條記錄中同一年份值最小的記錄.即如果數據如下:

2016 2017 2018
1 30 300
2 20 200
3 40 100

則取出數據爲:2016:1,2017:20:2018:100.
數據表已被設計成這樣,只能在此基礎上想辦法.
基本思路是:先取得年份列表,作爲一級循環.
然後就得記錄條數,作爲二級循環.然後取得每年的數據列表,在排序取得最小值.
我這個解法中,最關鍵的就是,構建一個列表,這個列表在每次循環時都會變化,然後需要動態的排序取值.
這個取最小值的過程,我是利用List重寫sort方法來實現的.
代碼如下:

 private BigDecimal getMin(List<BigDecimal> list, BigDecimal e) {
        BigDecimal beginYearRent;
        list.add(e);
        //升序排序
        Collections.sort(list, new Comparator<BigDecimal>() {
            public int compare(BigDecimal o1, BigDecimal o2) {
                return o1.compareTo(o2);
            }
        });
        beginYearRent=list.get(0);
        return beginYearRent;
    }

至於外面如何調用這個方法,就簡單做下說明如下,做了些許修改,只是爲了更好的理解上面的問題:

/**
     *
     * @param ids 記錄id
     * @param yearRangeList 時間段
     * @return
     * @throws Exception
     */
public String calMgrStandard(String ids, List<String> yearRangeList) throws Exception {
        String msg = "error";

        msg = "";
        String[] sIds =  ids.split(",");

        List<BigDecimal> list2015 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2016 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2017 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2018 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2019 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2020 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2021 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2022 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2023 = new ArrayList<BigDecimal>();
        List<BigDecimal> list2024 = new ArrayList<BigDecimal>();
        for (String yearRange : yearRangeList) {
            String[] yearRanges = yearRange.split("~");
            String[] beginDate = yearRanges[0].split("-");
            String[] endDate = yearRanges[1].split("-");

            int beginYear = Integer.valueOf(beginDate[0]);
            BigDecimal beginYearRent = BigDecimal.ZERO;
            BisStore store = null;

            for (String id : sIds) {
                if(StringUtils.isBlank(id)){
                    continue;
                }
//取得記錄
                store = this.getEntity(id);
                if(store == null){
                    continue;
                }

                BigDecimal[] djInfo = new BigDecimal[]{
                        store.getMgr2015(),
                        store.getMgr2016(),store.getMgr2017(),
                        store.getMgr2018(),store.getMgr2019(),
                        store.getMgr2020(),store.getMgr2021(),                  store.getMgr2022(),store.getMgr2023(),
                        store.getMgr2024()
                };
                if(2015==beginYear){
                    beginYearRent = getMin(list2015, djInfo[0]);
                }else if(2016==beginYear){
                    beginYearRent = getMin(list2016, djInfo[1]);
                }else if(2017==beginYear){
                    beginYearRent = getMin(list2017, djInfo[2]);
                }else if(2018==beginYear){
                    beginYearRent = getMin(list2018, djInfo[3]);
                }else if(2019==beginYear){
                    beginYearRent = getMin(list2019, djInfo[4]);
                }else if(2020==beginYear){
                    beginYearRent = getMin(list2020, djInfo[5]);
                }else if(2021==beginYear){
                    beginYearRent = getMin(list2021, djInfo[6]);
                }else if(2022==beginYear){
                    beginYearRent = getMin(list2022, djInfo[7]);
                }else if(2023==beginYear){
                    beginYearRent = getMin(list2023, djInfo[8]);
                }else if(2024==beginYear){
                    beginYearRent = getMin(list2024, djInfo[9]);
                }
            }
            msg += beginYearRent+"-";
        }
        return msg;
    }

以上就是我對這個問題想到的解法.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章