我有2个月的2个数据集,公司名称,收入和费用 对于每家公司,我需要提供4月份的收入和支出详情以及上个月收入和支出的百分比变化。分数。
我可以使用哪个java集合来比较这两个数据集?
示例数据集:
Month Company_Name Income Expense
April A 100 100
April B 200 200
April C 300 300
April D 400 400
April E 500 500
Month Company_Name Income Expense
March A 50 50
March B 100 100
March C 200 200
March D 300 300
March G 600 600
输出应该是这样的
(Company Name: A, April Income: 100, April Expense: 100 percent Income change with previous month : 100)
(Company Name: B, April Income: 200, April Expense: 200 percent Income change with previous month : 100)
(Company Name: C, April Income: 300, April Expense: 300 percent Income change with previous month : 100)
(Company Name: D, April Income: 400, April Expense: 400 percent Income change with previous month : 100)
(Company Name: E, April Income: 500, April Expense: 500 percent Income change with previous month : N/A)
(Company Name: G, April Income: N/A, April Expense: N/A percent Income change with previous month : N/A)
这是我未曾尝试过的?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;
public class a {
public static void main(String[] args) {
HashMap<String, ArrayList<Integer>> marchMap=new HashMap<String, ArrayList<Integer>>();
HashMap<String, ArrayList<Integer>> aprilMap=new HashMap<String, ArrayList<Integer>>();
ArrayList<Integer> revenueDetails=new ArrayList<Integer>();
// company A's march income and expense details
revenueDetails.add(100); //income
revenueDetails.add(100); //expense
marchMap.put("A", revenueDetails);
// company B's march income and expense details
revenueDetails.add(200); //income
revenueDetails.add(200); //expense
marchMap.put("B", revenueDetails);
// company A's april income and expense details
revenueDetails.add(200); //income
revenueDetails.add(200); //expense
aprilMap.put("A", revenueDetails);
// company B's april income and expense details
revenueDetails.add(300); //income
revenueDetails.add(300); //expense
aprilMap.put("B", revenueDetails);
// doesnt seem to be working. prints: A, 100, 100 B,100,100
for (Entry<String, ArrayList<Integer>> entry : aprilMap.entrySet()) {
System.out.println("Company Name " + entry.getKey() + ": " + " Income: " + entry.getValue().get(0) + " Expense: " + entry.getValue().get(1));
}
}
}
答案 0 :(得分:0)
只需为要存储在地图中的值创建自己的类。
public class Main {
public static void main(String[] args) {
Map<String, IncomeExpense) march = new HashMap<>();
march.add(new IncomeExpense("March",100,100);
...
}
private static class IncomeExpense{
private String month;
private int income;
private int expense;
public IncomeExpense(String month, int income, int expense) {
this.income = income;
this.expense = expense;
}
public String toText() {
return month + " Income: " + income + ", " + month + " Expense: " + expense;
}
}
}
答案 1 :(得分:0)
您没有重新初始化ArrayList。所有条目都是不同的ArrayList。因此,在每个月添加每个公司条目后,您必须创建一个新的ArrayList,如下所示,
revenueDetails=new ArrayList<Integer>();
代码必须与下面的代码段相同,
ArrayList<Integer> revenueDetails=new ArrayList<Integer>();
// company A's march income and expense details
revenueDetails.add(100); //income
revenueDetails.add(100); //expense
marchMap.put("A", revenueDetails);
// company B's march income and expense details
revenueDetails=new ArrayList<Integer>();
revenueDetails.add(200); //income
revenueDetails.add(200); //expense
marchMap.put("B", revenueDetails);
// company A's april income and expense details
revenueDetails=new ArrayList<Integer>();
revenueDetails.add(200); //income
revenueDetails.add(200); //expense
aprilMap.put("A", revenueDetails);