如何确保返回StringList将被订购:Scala

时间:2017-12-19 00:54:19

标签: scala

我正在使用Scala 2.11.8

我正在尝试从我的属性文件中读取查询。每个查询集都有多个部分(如下所述)

我有一定的序列,必须执行这些查询。

代码:

select.caseInc.2 = Select emp_salary, emp_dept_id from employees
select.caseLocal.1 = select one
select.caseLocal.3 = select three
select.caseRemote.2 = Select e1.emp_name, d1.dept_name, e1.salary from emp_1 e1 join dept_1 d1 on(e1.emp_dept_id = d1.dept_id)
select.caseRemote.1 = Select * from departments
select.caseInc.1 = Select emp_id, emp_name from employees
select.caseLocal.2 = select two
select.caseLocal.4 = select four

PropertyFile内容:

Select emp_id, emp_name from employees
Select emp_salary, emp_dept_id from employees
select one
select two
select three
select four

输出:

{{1}}

正如我们在输出中看到的那样,结果是排序。在该属性中,如果您看到我已经尝试对序列中的查询进行编号,则应该运行。(将caseInc,caseLocal作为参数传递)。

使用getStringList()我总是根据我提供的序列号获得排序列表。

即使我尝试使用toArray()& toArray()。toSet我得到了排序输出。

目前为止

但是如何确保它总是以我在属性文件中提供的排序顺序返回。我很困惑,因为不知何故我无法找到说明返回列表将被排序的API。

1 个答案:

答案 0 :(得分:0)

我认为你可以依靠这个事实。查看DefaultTransformer的代码,您可以看到以下逻辑:

    } else if (requested == ConfigValueType.LIST && value.valueType() == ConfigValueType.OBJECT) {
        // attempt to convert an array-like (numeric indices) object to a
        // list. This would be used with .properties syntax for example:
        // -Dfoo.0=bar -Dfoo.1=baz
        // To ensure we still throw type errors for objects treated
        // as lists in most cases, we'll refuse to convert if the object
        // does not contain any numeric keys. This means we don't allow
        // empty objects here though :-/
        AbstractConfigObject o = (AbstractConfigObject) value;
        Map<Integer, AbstractConfigValue> values = new HashMap<Integer, AbstractConfigValue>();
        for (String key : o.keySet()) {
            int i;
            try {
                i = Integer.parseInt(key, 10);
                if (i < 0)
                    continue;
                values.put(i, o.get(key));
            } catch (NumberFormatException e) {
                continue;
            }
        }
        if (!values.isEmpty()) {
            ArrayList<Map.Entry<Integer, AbstractConfigValue>> entryList = new ArrayList<Map.Entry<Integer, AbstractConfigValue>>(
                    values.entrySet());
            // sort by numeric index
            Collections.sort(entryList,
                    new Comparator<Map.Entry<Integer, AbstractConfigValue>>() {
                        @Override
                        public int compare(Map.Entry<Integer, AbstractConfigValue> a,
                                Map.Entry<Integer, AbstractConfigValue> b) {
                            return Integer.compare(a.getKey(), b.getKey());
                        }
                    });
            // drop the indices (we allow gaps in the indices, for better or
            // worse)
            ArrayList<AbstractConfigValue> list = new ArrayList<AbstractConfigValue>();
            for (Map.Entry<Integer, AbstractConfigValue> entry : entryList) {
                list.add(entry.getValue());
            }
            return new SimpleConfigList(value.origin(), list);
        }
    }

注意如何将键解析为整数值,然后使用Integer.compare

进行排序