如何为类似的行添加Arraylist项

时间:2015-10-28 05:38:20

标签: java arraylist

我有一个类型为SearchListPojo的动态Arraylist sList,它具有属性区域,位置和数量。可以有n个具有相似区域和位置的项目。我想检索并添加具有相似区域和位置的相应数量的项目。

我的SearchListPojo:

public class SearchListPojo 
{
String area, location, quantity;

    public String getQuantity() {
        return quantity;
    }

    public void setQuantity(String quantity) {
        this.quantity = quantity;
    }

    public String getLocation() {
        return location;
    }

    public int compareTo(SearchList o2) {
        // TODO Auto-generated method stub
        return 1;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getArea() {
        return area;
    }

    public void setArea(String area) {
        this.area = area;
    }

    @Override
    public boolean equals(Object o) {

        if (o == null || !(o instanceof SearchList))
            return false;
        return ((SearchList) o).area.equals(this.area)
                && ((SearchList) o).location.equals(this.location);

    }

    @Override
    public String toString() {
        return "SearchList [area=" + area + ", location=" + location + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((area == null) ? 0 : area.hashCode());
        result = prime * result
                + ((location == null) ? 0 : location.hashCode());
        return result;
    }
}

假设有5个项目:

 [area=Area1, location=Location1, quantity=3], 
 [area=Area1, location=Location1, quantity=7], 
 [area=Area3, location=Location2, quantity=2], 
 [area=Area3, location=Location2, quantity=22], 
 [area=Area3, location=Location1, quantity=10]

然后在添加类似项目的数量后,列表应为:

[area=Area1, location=Location1, quantity=10],  
[area=Area3, location=Location2, quantity=24], 
[area=Area3, location=Location1, quantity=10]

如何比较列表sList的每一行并添加类似项目的数量?

2 个答案:

答案 0 :(得分:1)

我会尝试以下几行。我使用Map,其键实际上是区域和位置的组合。对于每个唯一的区域/位置对,我保持数量的总计,最后我将此信息打印到控制台。

Map<String, Integer> countMap = new HashMap<String, Integer>();

for (SearchListPojo slp : sList) {
    String key = slp.getArea() + "-" + slp.getLocation();
    int quantity = Integer.parseInt(slp.getQuantity());
    int total = countMap.get(key) != null ? countMap.get(key) : 0;
    total += quantity;

    countMap.put(key, total);
}

// this for loop iterates over the Map and prints out the quantities
// for similar items
for (Map.Entry<String, Integer> entry : countMap.entrySet()) {
    String areaName     = entry.getKey().split("-")[0];
    String locationName = entry.getKey().split("-")[1];
    Integer quantity = entry.getValue();

    System.out.print("area=" + areaName + ", ");
    System.out.print("location=" + locationName + ", ");
    System.out.print("quantity=" + quantity);
}

顺便说一句,您正在执行的操作与SQL可以处理的GROUP BY操作非常相似。话虽这么说,如果你的SearchListPojo对象最终来自你的数据层,你最好还是在那里做这个繁重的工作。

答案 1 :(得分:0)

将字段数量类型更改为long,如果不使用多线程,请在服务类中尝试此代码:

public static HashMap <String, SearchListPojo> mapOfPojo = new HashMap<>();

public static void putForEquals(SearchListPojo pojo) {
SearchListPojo currentPojo = mapOfPojo.get(pojo.getArea() + pojo.getLocation());
if (currentPojo != null) {
    currentPojo.setQuantity(currentPojo.getQuantity() + pojo.getQuantity());
} else {
    mapOfPojo.put(pojo.getArea() + pojo.getLocation(), pojo);
}
}

结果测试:

SearchListPojo slp1 = new SearchListPojo();
SearchListPojo slp2 = new SearchListPojo();
slp1.setArea("Ekb");
slp1.setLocation("Elmash");
slp1.setQuantity(500);
slp2.setArea("Ekb");
slp2.setLocation("Elmash");
slp2.setQuantity(700);

putForEquals(slp1);
putForEquals(slp2);

for (Map.Entry<String, SearchListPojo> entry : mapOfPojo.entrySet()) {
System.out.println(entry.getValue().getArea() + entry.getValue().getLocation() + entry.getValue().getQuantity());
}