你好, friend 们,我有一个自定义数组列表,其中包括如下项目:[t_id(PK) cat_id(FK), cat_name,total]。
假设我在上面输入五个值,例如 ({1,1,c1,100},{2,1,c1,200},{3,1,c1,300},{4,1, c2,400},{5,1,c2,500})
现在我想要类别 "c1"
和 "c2"
的总值
意味着输出应该是这样的
"c1=600" and "c2=900".
所以知道我该如何解决
最佳答案
我不知道 bean 怎么样,在这里我尝试了一些可能对你有帮助的东西,
Map<String, Integer> map = new HashMap<String, Integer>();
for(Custom custom : customList)
{
if(map.containsKey(custom.getCat_name()))
{
Integer i = map.get(custom.getCat_name());
map.put(custom.getCat_name(), i+custom.getTotal());
}
else
{
map.put(custom.getCat_name(), custom.getTotal());
}
}
System.out.println("Map : "+map);
输出: map :{c1=600, c2=900}
关于java - 如何从android中的一个Arraylist中获取单独的数据项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22680877/