2016-02-05 147 views
0

我正在制作洗衣程序。用户选择这样的项目。HashMap <String,ArrayList <Integer>>获取值

enter image description here

,我有三对的TabBar(干,洗脸,洗和铁)。 而我存储在三个不同的用户选择的物品商店HashMap<String, ArrayList<Integer>>

我的价值存储在像这样的哈希映射。

Key:Name, Value:[Quantity, Default-Price, Qty-Total, position] 
    Key: Trouser, Value: [2, 8, 16, 3] 
    Key: Shirt, Value: [3, 15, 45, 0] 
    Key: Coat, Value: [2, 7, 14, 2] 
    Key: Pants, Value: [3, 10, 30, 1] 

我想在三个不同的HashMap中总和第三个元素(16,45,14,30)。 对于所有项目总计。

+0

到目前为止你做了什么?你有一个示例代码? – Rudra

+0

你想做什么?获取每个列表的最大值?获取每个列表的第三个值?还有别的吗? –

+0

在这里看到我的旧帖子http://stackoverflow.com/questions/35199498/dynamic-listview-with-button-click-get-grand-total –

回答

1

如果map是你HashMap<String, ArrayList<Integer>>然后遍历地图,并从ArrayList第三值,总结it.Try此,

HashMap<String, ArrayList<Integer>> map = new HashMap<>(); 
    int sum = 0; 
    for (Map.Entry<String, ArrayList<Integer>> item : map.entrySet()) { 
     sum += item.getValue().get(2); 
    } 
0
Map<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>(); 
     // create list one and store values 
     List<Integer> valSetOne = new ArrayList<Integer>(); 
     valSetOne.add(5); 
     valSetOne.add(8); 
     // create list two and store values 
     List<Integer> valSetTwo = new ArrayList<Integer>(); 
     valSetTwo.add(11; 
     valSetTwo.add(15); 
     // create list three and store values 
     List<Integer> valSetThree = new ArrayList<Integer>(); 
     valSetThree.add(6); 
     valSetThree.add(55); 
     // put values into map 
     map.put(1, valSetOne); 
     map.put(2, valSetTwo); 
     map.put(3, valSetThree); 
     // iterate and display values 
     System.out.println("Fetching Keys and corresponding [Multiple] Values n"); 
     for (Map.Entry<Integer, List<Integer>> entry : map.entrySet()) { 
      Integer key = entry.getKey(); 
      List<Integer> values = entry.getValue(); 
      System.out.println("Key = " + key); 
      System.out.println("Values = " + values + "n"); 
     } 
0

总结数组的第3个索引然后将其存储在变量中有多难。

比方说,这是你的HashMap

HashMap<String, ArrayList<Integer>> clothes = new HashMap<>(String,ArrayList<Integer>); 

这些值..

Key: Trouser, Value: [2, 8, 16, 3] 
Key: Shirt, Value: [3, 15, 45, 0] 
Key: Coat, Value: [2, 7, 14, 2] 
Key: Pants, Value: [3, 10, 30, 1] 

int grandTol = 0; 
for (Map.Entry<String, List<Integer>> cloth : clothes.entrySet()) { 
    grandTol += entry.getValue().get(2); 

} 

通过HashMap中只需循环,并添加了值。

+0

*条目*无法解析 –

+0

检查现在。我编辑了我的答案。 – Ritesh

0

试试这个。

int sum = map.values().stream().mapToInt(v -> v.get(2)).sum(); 
3

在此处查找完整的代码。

public static void main(String[] args) { 
    Integer granddTotal = 0; 
    HashMap<String, ArrayList<Integer>> map = new HashMap<String, ArrayList<Integer>>(); 
    ArrayList<Integer> list = new ArrayList<Integer>(); 
    list.add(2); 
    list.add(8); 
    list.add(16); 
    list.add(3); 
    map.put("Trouser", list); 

    list = new ArrayList<Integer>(); 
    list.add(3); 
    list.add(15); 
    list.add(45); 
    list.add(0); 
    map.put("Shirt", list); 

    list = new ArrayList<Integer>(); 
    list.add(2); 
    list.add(7); 
    list.add(14); 
    list.add(2); 
    map.put("Coat", list); 

    list = new ArrayList<Integer>(); 
    list.add(3); 
    list.add(10); 
    list.add(30); 
    list.add(1); 
    map.put("Pant", list); 


    for(ArrayList<Integer> listData : map.values()) { 
     granddTotal = granddTotal + listData.get(2); 
    } 
    System.out.println(granddTotal); 
}  
相关问题