3
我有兩個長字符串的地圖,我想通過比較一個地圖值與另一個地圖值來計算百分比。使用java 8 stream API比較2個地圖來計算百分比?
對於如:
Map<String, Long> map1 = new ConcurrentHashMap<String, Long>();
map1.put("test1", (long) 20);
map1.put("test2", (long) 30);
Map<String, Long> map2 = new ConcurrentHashMap<String, Long>();
map2.put("test1", (long) 120);
map2.put("test2", (long) 120);
我想MAP2值與MAP1值進行比較來計算測試1和測試2的值的平均值。
理想的結果應當是
resultmap = [test1 : "16.66%", test2 : "25%"]
你能告訴我們你如何計算這些值嗎?是test1 = 20/120 = 0.1667? – markspace
是test1 = 20/120 * 100 = 16.67%test2 = 30/120 * 100 = 25% –
我不知道「有缺陷」,但我很難考慮如何使用流來完成此操作。程序上,這並不難。 – markspace