gpt4 book ai didi

java - 将两个数组列表中的值相加并返回第三个列表

转载 作者:搜寻专家 更新时间:2023-11-01 01:15:25 27 4
gpt4 key购买 nike

如果存在于两个列表中,则通过总结与相同元素相关的数据来创建第三个数组列表,否则插入新数据

我从两个数组列表中创建了两个映射,并将 Id 作为键,然后通过组合两个映射中的键创建了一个集合。利用集合中的值,我查询了两个列表并得出了总和。如果我遵循此方法,我会看到预期的输出,但想知道在 Java 1.6 中是否有任何其他有效的方法来实现此目的

----------------Java----------------

public class Stock {

private int stockCode;
private int stockQuantity;
private int stockValue;

public int getStockCode() {
return stockCode;
}

public int getStockQuantity() {
return stockQuantity;
}

public int getStockValue() {
return stockValue;
}

public Stock(int stockCode, int stockQuantity, int stockValue) {
this.stockCode = stockCode;
this.stockQuantity = stockQuantity;
this.stockValue = stockValue;
}

public static void main(String[] args){
List<Stock> oldStock = new ArrayList<Stock>();
Stock s1 = new Stock(1, 20, 16000);
Stock s2 = new Stock(2, 10, 5000);
Stock s3 = new Stock(3, 15, 3000);
oldStock.add(s1);
oldStock.add(s2);
oldStock.add(s3);

List<Stock> newStock = new ArrayList<Stock>();
Stock s4 = new Stock(5, 5, 2500);
Stock s5 = new Stock(1, 10, 8000);
Stock s6 = new Stock(3, 10, 2000);
newStock.add(s4);
newStock.add(s5);
newStock.add(s6);

List<Stock> netStock = new ArrayList<Stock>();

Map<Integer, Stock> oldStockMap = new HashMap<Integer, Stock>();
for(Stock os:oldStock){
oldStockMap.put(os.getStockCode(),os);
}
Map<Integer, Stock> newStockMap = new HashMap<Integer, Stock>();
for(Stock ns:newStock){
newStockMap.put(ns.getStockCode(),ns);
}
Set<Integer> keySet = new HashSet<Integer>();
keySet.addAll(oldStockMap.keySet());
keySet.addAll(newStockMap.keySet());

for(Integer ks:keySet){
Integer netStockQ=0;
Integer netStockV=0;
if(oldStockMap.get(ks)!=null && newStockMap.get(ks)!=null) {
netStockQ =oldStockMap.get(ks).getStockQuantity() + newStockMap.get(ks).getStockQuantity();
netStockV = oldStockMap.get(ks).getStockValue() + newStockMap.get(ks).getStockValue();
} else if(oldStockMap.get(ks)==null && newStockMap.get(ks)!=null){
netStockQ = newStockMap.get(ks).getStockQuantity();
netStockV = newStockMap.get(ks).getStockValue();
} else if(oldStockMap.get(ks)!=null && newStockMap.get(ks)==null){
netStockQ =oldStockMap.get(ks).getStockQuantity();
netStockV = oldStockMap.get(ks).getStockValue();
}
netStock.add(new Stock(ks,netStockQ, netStockV));
}

for(Stock ns: netStock){
System.out.println(ns.getStockCode() +"- Quantity - "+ ns.getStockQuantity()+"- Value -"+ns.getStockValue());
}
}
}

----------------输出----------------

1- Quantity - 30- Value -24000
2- Quantity - 10- Value -5000
3- Quantity - 25- Value -5000
5- Quantity - 5- Value -2500

最佳答案

您可以执行以下操作(假设您无法升级到 Java 8)

  • 创建一个以股票代码为键,股票为值的映射
Map<Integer, Stock> netStockMap = new HashMap<Integer, Stock>();
  • 然后将所有旧库存条目添加到该 map ,如下所示。
for (Stock oldStockItem: oldStock) {
netStockMap.put(oldStockItem.getStockCode(), oldStockItem);
}
  • 然后对于每个新的股票条目,检查 map 中是否存在旧条目。如果存在,则创建一个新的库存条目,将旧条目和新条目结合起来。否则只需将新的股票条目添加到 map 中即可。
for (Stock newStockItem: newStock) {
Integer stockCode = newStockItem.getStockCode();
Stock oldStockItem = netStockMap.get(stockCode);
if (oldStockItem != null) {
Stock netStockItem = new Stock(stockCode, oldStockItem.getStockQuantity() + newStockItem.getStockQuantity(),oldStockItem.getStockValue() + newStockItem.getStockValue());
netStockMap.put(stockCode, netStockItem);

} else {
netStockMap.put(stockCode, newStockItem);
}
}
  • 然后获取 map 的值作为最终的股票 list
List<Stock> netStock = new ArrayList<Stock>(netStockMap.values());

关于java - 将两个数组列表中的值相加并返回第三个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57899921/

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