gpt4 book ai didi

java - 如何使用 Hash Map 形成一组相同的值

转载 作者:行者123 更新时间:2023-12-02 01:56:27 25 4
gpt4 key购买 nike

我从数组列表中检索数据。数组列表已经有一个与 HashMap 类型相同的值(键,值);从数组列表中检索这些数据并映射到 HashMap 变量后。我必须通过一个名为“税”的变量对它们进行分组。

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);

之后将这些 HashMap 值添加到名为final_cart_list_1类型的数组列表初始化的Arraylist中

ArrayList<HashMap<String, String>> final_cart_list_1;

创建另一个 HashMap 来存储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"));

税收的 log cat 值如下

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 编程新手。我有我上面通过算法或提示预测的理想。请帮助通过哪种方法解决这个问题

最佳答案

希望这对您的工作有所帮助

       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 则单独分组。

欢迎评论或提问谢谢

关于java - 如何使用 Hash Map 形成一组相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57396057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com