从TreeMap向ArrayList添加对象基于键约束

时间:2015-09-03 08:46:40

标签: java for-loop arraylist collections treemap

我正在处理需求,我需要从TreeMap向arrayList添加单个responseItems。我有Map键的对象数组(String类型)。 responseCodes的值可以是1-15和" NA"。

基于以下条件(在键上)我必须从Map获取responseItem并添加到ArrayList ..

1)如果响应代码为" NA"或" 0"我不需要将responseItem添加到我的arrayList。

2)如果响应代码为" 1"我必须使用responseCode值" 2"。

添加responseItems

3)如果响应代码是(2-15)之间的任何值,我必须添加所有其他responseItem,其响应代码值大于数组中currentResponse代码。

我已经使用一些嵌套的for循环实现了这一点,我假设这可以通过有效的方式完成。请仔细阅读需求和下面的代码片段并帮助我。

public List getResponseItemList(Object [] responseCodeArray){

    List itemsList = new ArrayList();

    if (responseCodeArray != null && responseCodeArray.length > 0) {
        for (int i = 1; i <= responseCodeArray.length; i++) {
            if (!((responseCodeArray[responseCodeArray.length - i]
                    .equals("NA")) | (responseCodeArray[responseCodeArray.length
                    - i].equals("0")))) {

                if (responseCodeArray[responseCodeArray.length - i]
                        .equals("1")) {
                    for (int j = 1; j <= responseCodeArray.length; j++) {
                        if ((responseCodeArray[responseCodeArray.length - j]
                                .equals("2"))) {
                            itemsList = itemsByResponseCodeMap
                                    .get(responseCodeArray[responseCodeArray.length
                                            - j]);
                        }
                    }
                } else {
                    int level = Integer
                            .parseInt((String) responseCodeArray[responseCodeArray.length
                                    - i]);
                    for (int x = 1; x <= responseCodeArray.length; x++) {
                        int innerLevel = Integer
                                .parseInt((String) responseCodeArray[responseCodeArray.length
                                        - x]);
                        if (innerLevel > level) {
                            itemsList = itemsByResponseCodeMap
                                    .get(responseCodeArray[responseCodeArray.length
                                            - x]);
                        }
                    }

                }

            }

        }
        Collections.sort(itemsList);// for sorting
        return itemsList;
    }

}

0 个答案:

没有答案