Groovy - ArrayList,增加项目数量而不是重新添加

时间:2017-04-05 06:36:52

标签: arraylist groovy

我有两个数组列表(我将简化此问题),如下所示:

public boolean mediate(MessageContext mc) {
    org.apache.axis2.context.MessageContext amc = ((Axis2MessageContext) mc).getAxis2MessageContext();
        try {
            String contentID = amc.getAttachmentMap().getAllContentIDs()[0];
            DataHandler dh = amc.getAttachment(contentID);
            dh.getDataSource().getName();
            InputStream is = null;
            try {
                is = dh.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line = null;
                while ((line = br.readLine()) != null) {
                    System.out.println("client read:" + line);
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } 
    }
    return true;
}

另一个我打电话为def originalList = [[name: "Ben", age: 21, weight: 80], [name: "Martin", age: 36, weight: 99], [name: "Sammy", age: 18, weight: 65], [name: "Ben", age: 28, weight: 75], [name: "Ben", age: 28, weight: 120]] ,它开始为空,即newList,但会添加属性def newList = []nameage

现在我要做的是循环浏览occurrences并将其项目添加到originalList,但如果newList已包含同名的项目age属性我想增加项目的newList属性,例如:

occurrences

我将如何完成这项工作?这是我尝试过的。

def newList = [[name: "Ben", age: 21, occurrences: 1], [name: "Martin", age: 36, occurrences: 1], [name: "Sammy", age: 18, occurrences: 1], [name: "Ben", age: 28, occurrences: 2]]

看起来它不起作用,因为originalList.each { newList.eachWithIndex { nl, i -> if(nl.name.equals(it.name) && nl.age == it.age) { nl.occurrances++ } else { newList.add([name: it.name, age: it.age, occurrances: 0]) } } } 永远不会循环,因为它从空开始,因为它永远不会循环,它永远不会增加newList.eachWithIndex属性或将项目添加到列表中。

3 个答案:

答案 0 :(得分:3)

你可以这样做,例如:

def originalList = [
    [name: "Ben", age: 21, weight: 80], 
    [name: "Martin", age: 36, weight: 99], 
    [name: "Sammy", age: 18, weight: 65], 
    [name: "Ben", age: 28, weight: 75], 
    [name: "Ben", age: 28, weight: 120]
]

originalList
    .groupBy( { it.name }, { it.age } )
    .collect { k1, v1 -> v1.collect { k2, v2 -> [name: k1, age: k2, occurrences: v2.size()] } }
    .flatten()

第一步输入按nameage分组,每个键Map,然后是valuesv1){处理{1}}以获取统计信息。由于上述操作导致嵌套k1,因此在最后调用List

答案 1 :(得分:0)

修复原始算法中的错误(使用地图会更有效率!):

originalList.each {
    def found=false
    newList.eachWithIndex { nl, i ->
        if(nl.name.equals(it.name) && nl.age == it.age) {
            nl.occurrances++
            found = true
        }
    }
    if (!found) {
       newList.add([name: it.name, age: it.age, occurrances: 1])
    }
}

更新:带有地图的版本,按要求...

def map = [:]

originalList.each {
    def key = it.name+"@"+it.age  // or something better, keeping it simple here

    if (map[key]) {
      map[key].occurances++
    }
    else {
      def entry = [name: it.name, age: it.age, occurrances: 1]
      map[key] = entry
      newList.add(entry)
    }
}    

答案 2 :(得分:-1)

我不是一个常规的开发人员,因此我将在java中编写代码片段。请翻译一下。

Map<String,Integer> map=new HashMap<String,Integer>();//this map stores name and occurances

for(Employee e: originaleList){
    int oldCount=map.get(e.getName()) == null ? 0 : map.get(e.getName());
    map.put(e.getName(), oldCount + 1);

    newList.add(new Employee(e.getName(), e.getAge(), map.get(e.getName())));
    }