【问题标题】:how to form a group of same values using Hash Map如何使用哈希映射形成一组相同的值
【发布时间】:2019-08-07 13:52:46
【问题描述】:

我从数组列表中检索数据。 Array list 已经有一个值与 Hash Map Types (key ,values) 相同;从 Array 列表中检索这些数据并映射到 Hash Map 变量后。我必须通过一个名为 tax 的变量对它们进行分组。

1) 在Hash map中添加add

 HashMap<String, String> personq = new HashMap<String, String>();
                                personq.put("product_id",tv4);
                                personq.put("productname",tv2);
                                personq.put("exit",exit);
                                personq.put("product",product_1);
                                personq.put("unit_price",tv3);
                                personq.put("tax", String.valueOf(gst));
                                personq.put("purchase_cost",String.valueOf(tv9));
                                personq.put("cost", String.valueOf((two.format(finalcp))));
                                final_cart_list_1.add(personq);

之后将这些 Hash Map 值添加到称为 final_cart_list_1 类型的数组列表初始化的 Arraylist

ArrayList<HashMap<String, String>> final_cart_list_1;

创建另一个哈希映射来存储 final_cart_list_1 中的值

 HashMap<String, String> personMap = new HashMap<String, String>();



for(int i=0;i<final_cart_list_1.size();i++){
                    personMap = final_cart_list_1.get(i);// getting values from arraylist and storing into hashmap
                    Log.d("valuesinHashamap ",""+personMap);
                    tax = Float.parseFloat(personMap.get("tax"));
                    Log.d("tax_values",""+  tax);
                    qty=Integer.parseInt(personMap.get("qty"));
                    Log.d("qty_values",""+qty);
                  unitprice=Float.parseFloat(personMap.get("unit_price"));
                    Log.d("untiprice_values",""+qty);
                  }

所有的操作都是在购物车的基础上完成的。我在购物车中有 4 个产品 下面我发布 personMap hashMap

的 logcat 值
2019-08-07 18:08:35.501 13378-13378/com.example.dewsys.technicianapp D/valuesinHashamap: {product_id=500, unit_price=220.000000, exit="1", cost=281.60, purchase_cost=200.0, productname=System(or)Laptop/ANTIVIRUS/K7/asd/test, qty=1, tax=28.0, product="0"}
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/valuesinHashamap: {product_id=501, unit_price=126.250000, exit="1", cost=161.60, purchase_cost=101.0, productname=System(or)Laptop/ANTIVIRUS/K7/asd/test1, qty=1, tax=28.0, product="0"}
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/valuesinHashamap: {product_id=500, unit_price=220.000000, exit="1", cost=281.60, purchase_cost=200.0, productname=System(or)Laptop/ANTIVIRUS/K7/asd/test, qty=1, tax=28.0, product="0"}
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/valuesinHashamap: {product_id=500, unit_price=220.000000, exit="1", cost=281.60, purchase_cost=200.0, productname=System(or)Laptop/ANTIVIRUS/K7/asd/test, qty=1, tax=28.0, product="0"}

之后我为所有产品取产品特定的税值

  tax = Float.parseFloat(personMap.get("tax"));

记录税的猫值在下面

2019-08-07 18:08:35.501 13378-13378/com.example.dewsys.technicianapp D/tax_values: 28.0
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/tax_values: 28.0
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/tax_values: 28.0
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/tax_values: 28.0

之后,我将产品的特定数量、所有产品的单价值与仅含税相同。

数量日志

2019-08-07 18:08:35.501 13378-13378/com.example.dewsys.technicianapp D/qty_values: 1
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/qty_values: 1
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/qty_values: 1
2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/qty_values: 1

单价logcat

 2019-08-07 18:08:35.501 13378-13378/com.example.dewsys.technicianapp D/untiprice_values: 1
    2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/untiprice_values: 1
    2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/untiprice_values: 1
    2019-08-07 18:08:35.502 13378-13378/com.example.dewsys.technicianapp D/untiprice_values: 1

之后我需要创建一个数组或数组列表。 (基本概念是按税收分组产品和税收计算。)

如果税值相同,我们需要将这些税、数量、单价相加并推入创建的数组或数组列表中。

如果税值不匹配,他们想单独添加想要与数量和单价一起添加并推送到创建的数组或数组列表中。

我有一步一步的算法

  1. 获取税值。

  2. 第一次直接进入循环,我们要按税组推送数量、税金、单价等数据。

3.第二次进入循环检查当前税值和以前的税值。如果当前和以前的税值是,那么我们要将以前和当前税的税,数量,单价,数量,单价加在一起。否则当前和以前的税不匹配分开想要添加它并通过新的税值组单独附加

各位,我是 Java 编程新手。我有我上面通过算法或提示预测的理想。所以请帮助通过哪种方法解决这个问题

【问题讨论】:

  • 我只是想澄清一下,您想根据税对详细信息进行分组,如果税值与之前添加的相同意味着旧+新或[旧,新,..]

标签: java android arraylist hashmap


【解决方案1】:

希望对你的工作有所帮助

       List<Map<String, String>> strings = new ArrayList<>();
    Map<String, String> stringStringMap = new HashMap<>();
    stringStringMap.put("tax", "123");
    stringStringMap.put("quantity", "1");
    stringStringMap.put("prices", "123");

    Map<String, String> stringStringMap2 = new HashMap<>();
    stringStringMap2.put("tax", "123");
    stringStringMap2.put("quantity", "1");
    stringStringMap2.put("prices", "123");

    Map<String, String> stringStringMap3 = new HashMap<>();
    stringStringMap3.put("tax", "1234");
    stringStringMap3.put("quantity", "1");
    stringStringMap3.put("prices", "123");

    strings.add(stringStringMap3);
    strings.add(stringStringMap2);
    strings.add(stringStringMap);
     Map<String, List<Map<String, String>>> tax = strings.stream().collect(Collectors.groupingBy(
        o -> o.get("tax"), Collectors.toList()));
    System.out.println(tax);

这会像这样打印出来

{123=[{quantity=1, tax=123, prices=123}, {quantity=1, tax=123, prices=123}], 1234=[{quantity=1, tax=1234, prices=123}]}

这里所有具有价值 123 的税都归为一组,1234 的价值分别归为一组。

随时发表评论或提问 谢谢

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多