java - 如何使用哈希映射形成一组相同的值
问题描述
我从数组列表中检索数据。Array list 已经有一个值与 Hash Map Types (key ,values) 相同;从 Array 列表中检索这些数据并映射到 Hash Map 变量后。我必须通过一个称为税的变量对它们进行分组。
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);
之后将这些哈希映射值添加到称为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"));
税的 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
之后,我将产品的特定数量、所有产品的单价值与税金相同。
数量 logcat
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
之后我需要创建一个数组或数组列表。(基本概念是按税收组合产品和计算税收。)
如果税值相同,我们需要将这些税、数量、单价相加并推入创建的数组或数组列表。
如果税值不匹配,他们想要单独添加想要与数量和单价一起添加并推送到创建的数组或数组列表中。
我有一步一步的算法
获得税收价值。
第一次直接进入循环,我们要按税组推送数量、税金和单价等数据。
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
单独分组。
欢迎评论或提问谢谢
推荐阅读
- php - 预处理节点:调用所有节点类型的函数
- java - 未在本地运行时,自定义 AccessDecisionManager 不会在 JPA 存储库方法调用上被调用
- android - 如何知道视频文件长度是否与其原始长度相同
- c# - 在 EntityCore 中使用 Postgre 的 lpad
- javascript - JS/TS 如何遍历对象数组并更改值
- python - 如何迭代命名元组对象?
- c# - Unity - 将大量顶点加载到 ComputeShader 以进行光线跟踪的高性能选项?
- excel - 查找值的所有匹配项的函数
- c++ - 将链表转换为指针数组时出错
- r - 是否可以生成带有随机数的 gganimate 图?