gpt4 book ai didi

java - 需要帮助 'combining' Java ArrayList 中存在部分重复项

转载 作者:太空宇宙 更新时间:2023-11-04 13:26:50 26 4
gpt4 key购买 nike

        public class Pair{

public String key;
public String value;
public String amount;

public Pair(String key, String value, String amount){
this.key = key;
this.value = value;
this.amount = amount;
}

public Pair(String key, String value){
this.key = key;
this.value = value;
}

public String getKey(){
return key;
}
public String getValue(){
return value;
}
public String getAmount(){
return amount;
}
public void setAmount(String amount){
this.amount = amount;
}
}
<小时/>
        List<Pair> pairs = new ArrayList<Pair>();
List<Pair> duplicates = new ArrayList<Pair>();
List<Pair> duplicatesWithAmounts = new ArrayList<Pair>();

for (String line:lines){

String[] lineparts = line.split(",");

if ( line.startsWith("Date") || lineparts[3].equals("0.00") || lineparts[3].equals("-0.00") || lineparts[3].equals("000") || lineparts[3].equals("0000") || lineparts[3].equals("00000") ) {
continue;
}

String description = lineparts[4];
String acct = lineparts[2];
String amt = lineparts[3];

Pair pair = new Pair(description, acct);
Pair pair2 = new Pair(description, acct, amt);

if(!pairs.isEmpty() && pairs.contains(pair)) {
duplicates.add(pair);
duplicatesWithAmounts.add(pair2);
}

pairs.add(pair);

}


for (String linefromFile:lines){

String[] lineparts = linefromFile.split(",");

String description = lineparts[4];
String acct = lineparts[2];
String amt = lineparts[3];

Pair pair = new Pair(description, acct);
Pair pair2 = new Pair(description, acct, amt);

if(!duplicates.contains(pair)){
continue;
}
duplicates.remove(pair);

//Here is where I'm lost....

}

注意:金额目前是字符串,它们相加时将传递给新的 BigDecimal,但尚未到达该位置

注2:“lines”是 CSV 字符串的数组列表

我需要在注释所在的地方做的事情(说这里是我迷失的地方......),是根据它们的键和值将重复项合并到duplicatesWithAmounts中,并且对于键和值匹配的每个对象,将金额相加(可能有负数,但如果我们使用BigDecimal,那应该不重要)。因此,如果我有 3 行具有相同的键和值(无论金额不同),我将以 1 行包含该键和值以及 3 个金额的总和结束。

可能存在重复的示例数据的想法:

“0001,测试1,147.00”

“0001,测试1,129.00”

“0001,测试1,-17.00”

“0002,测试2,7.00”

“0002,测试2,-7.00”

“0003,测试3,30.00”

“0003,测试3,-12.00”

我想结束:

“0001,测试1,259.00”

“0002,测试2,0.00”

“0003,测试3,18.00”

非常感谢任何帮助,我是一名初级开发人员,一直在努力应对这个时间敏感的项目。我确信从一开始就有更好的方法来做到这一点,没有我的 Pair 类等,也没有我的对数组,所以如果有人向我展示也很好。但我也很想知道如何从我目前拥有的东西中获得我需要的东西,以用于学习目的。这些都在“进行交易行”部分内。

<小时/>

好吧,我尝试了斯特凡的方法,虽然我以为我已经弄清楚了一切。我正在正确输出中的行数,没有重复,但是每行的所有行数似乎都非常大。比应有的大得多。

package src;

import java.math.BigDecimal;

public class WDETotalsByDescription{

public String start;
public String end;
public String account;
public BigDecimal amount = new BigDecimal(0);

public String getStart() {
return start;
}

public void setStart(String start) {
this.start = start;
}

public String getEnd() {
return end;
}

public void setEnd(String end) {
this.end = end;
}

public String getAccount() {
return account;
}

public void setAccount(String account) {
this.account = account;
}

public BigDecimal getAmount() {
return amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

public WDETotalsByDescription(){

}

}
<小时/>
public static ArrayList<String> makeWestPacDirectEntry(ArrayList<String> lines, ParametersParser pp){

ArrayList<String> fileLines = new ArrayList<String>();

//制作头记录

    fileLines.add("0 " +
formatField(pp.getBank(), 21, RIGHT_JUSTIFIED, BLANK_FILLED) +
" " +
formatField(pp.getNameOfUser(), 26, LEFT_JUSTIFIED, BLANK_FILLED) +
formatField(pp.getNumberOfUser(),6,LEFT_JUSTIFIED,BLANK_FILLED) +
formatField(pp.getDescription(),12,LEFT_JUSTIFIED,BLANK_FILLED) +
MakeSapolRMH.getDate() +
formatField("",40,true,true));

//建立交易行

    BigDecimal totals = new BigDecimal(0);

HashMap<String, WDETotalsByDescription> tempList = new HashMap<String, WDETotalsByDescription>();


//find duplicates

Map<Pair, Pair> pairMap = new HashMap<Pair, Pair>();

for (String line : lines) {
String[] lineparts = line.split(",");

if (line.startsWith("Date") || lineparts[3].equals("0.00") || lineparts[3].equals("-0.00")
|| lineparts[3].equals("000") || lineparts[3].equals("0000") || lineparts[3].equals("00000")) {
continue;
}

String description = lineparts[4];
String acct = lineparts[2];
String amt = lineparts[3];

Pair newPair = new Pair(description, acct, amt);

if (!pairMap.containsKey(newPair)) {
pairMap.put(newPair, newPair);
} else {
Pair existingPair = pairMap.get(newPair);

BigDecimal mergedAmount = new BigDecimal(existingPair.getAmount()).movePointRight(2).add((new BigDecimal(newPair.getAmount()).movePointRight(2)));
existingPair.setAmount(mergedAmount.toString());
}
}

Set<Pair> mergedPairs = pairMap.keySet();

////////////////////

for (String linefromFile:lines){

String[] lineparts = linefromFile.split(",");

if ( linefromFile.startsWith("Date") || lineparts[3].equals("0.00") || lineparts[3].equals("-0.00") || lineparts[3].equals("000") || lineparts[3].equals("0000") || lineparts[3].equals("00000") ) {
continue;
}

for(Pair p:mergedPairs) {

WDETotalsByDescription wde = new WDETotalsByDescription();

String line = new String("1");

line += formatField (lineparts[1], 7, RIGHT_JUSTIFIED, BLANK_FILLED);
line += formatField (lineparts[2], 9, RIGHT_JUSTIFIED, BLANK_FILLED);
line += " " + formatField(pp.getTransactionCode(),2,LEFT_JUSTIFIED,ZERO_FILLED);

wde.setStart(line);
wde.setAmount(new BigDecimal(p.getAmount()));

line = formatField (lineparts[4], 32, LEFT_JUSTIFIED, BLANK_FILLED);
line += formatField (pp.getExernalDescription(), 18, LEFT_JUSTIFIED, BLANK_FILLED);
line += formatField (lineparts[5], 7, RIGHT_JUSTIFIED, BLANK_FILLED);
line += formatField (lineparts[6], 9, RIGHT_JUSTIFIED, BLANK_FILLED);
line += formatField (pp.getNameOfRemitter(), 16, LEFT_JUSTIFIED, BLANK_FILLED);
line += formatField ("", 8, RIGHT_JUSTIFIED, ZERO_FILLED);

wde.setEnd(line);
wde.setAccount(lineparts[4]);

if(!tempList.containsKey(wde.getAccount())){
tempList.put(wde.getAccount(), wde);
}
else{
WDETotalsByDescription existingWDE = tempList.get(wde.getAccount());
existingWDE.setAmount(existingWDE.getAmount().add(wde.getAmount()));
tempList.put(existingWDE.getAccount(), existingWDE);
}

}
}

for(WDETotalsByDescription wde:tempList.values()){

String line = new String();

line = wde.getStart()
+ formatField(wde.getAmount().toString(), 10, RIGHT_JUSTIFIED, ZERO_FILLED)
+ wde.getEnd();

if (formatField(wde.getAmount().toString(), 10, RIGHT_JUSTIFIED, ZERO_FILLED) != "0000000000") {
totals = totals.add(wde.getAmount());
fileLines.add(line);
}
}

//进行偏移记录,2012 年 11 月请求。

    String offset = new String();

offset += "1";
offset += pp.getAccountNumber(); // ###-### ######## (or else they will have to use spaces (###-### ######)) 16 chars total
offset += " ";

if (pp.isCredit()){
offset += "50";
} else {
offset += "13";
}

offset += formatField(totals.toString(), 10, RIGHT_JUSTIFIED, ZERO_FILLED);
offset += formatField(pp.getNameOfRemitter(), 16, LEFT_JUSTIFIED, BLANK_FILLED);
offset += " " +
formatField(pp.getInternalDescription(), 28, RIGHT_JUSTIFIED, BLANK_FILLED) +
" ";
offset += pp.getAccountNumber(); // ###-### ######## (or else they will have to use spaces (###-### ######)) 16 chars total
offset += formatField(pp.getNameOfRemitter(), 16, LEFT_JUSTIFIED, BLANK_FILLED);
offset += "00000000";

fileLines.add(offset);

//制作预告片记录

    String trailerRecord = "7999-999            ";

trailerRecord += formatField("", 10, RIGHT_JUSTIFIED, ZERO_FILLED);
trailerRecord += formatField(totals.toString(), 10, RIGHT_JUSTIFIED, ZERO_FILLED);
trailerRecord += formatField(totals.toString(), 10, RIGHT_JUSTIFIED, ZERO_FILLED);
trailerRecord += formatField("", 24, RIGHT_JUSTIFIED, BLANK_FILLED);
trailerRecord += formatField(Integer.toString(fileLines.size()-1), 6, RIGHT_JUSTIFIED, ZERO_FILLED);
trailerRecord += formatField("", 40, RIGHT_JUSTIFIED, BLANK_FILLED);
fileLines.add(trailerRecord);

return fileLines;
}
}

最佳答案

您可以使用 HashMap<Pair, Pair>解决这个问题:

    Map<Pair, Pair> pairMap = new HashMap<Pair, Pair>();

for (String line : lines) {
String[] lineparts = line.split(",");

if (line.startsWith("Date") || lineparts[3].equals("0.00") || lineparts[3].equals("-0.00")
|| lineparts[3].equals("000") || lineparts[3].equals("0000") || lineparts[3].equals("00000")) {
continue;
}

String description = lineparts[4];
String acct = lineparts[2];
String amt = lineparts[3];

Pair newPair = new Pair(description, acct, amt);
if (!pairMap.containsKey(newPair)) {
pairMap.put(newPair, newPair);
} else {
Pair existingPair = pairMap.get(newPair);
String mergedAmount = existingPair.getAmount() + newPair.getAmount();
existingPair.setAmount(mergedAmount);
}
}

Set<Pair> mergedPairs = pairMap.keySet();

为此,Pair必须override hashCode and equals ,这样两个不同的Pair如果键值相等,则认为实例相等。下面是一个由 Eclipse 生成的示例实现:

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((key == null) ? 0 : key.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Pair other = (Pair) obj;
if (key == null) {
if (other.key != null) {
return false;
}
} else if (!key.equals(other.key)) {
return false;
}
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}

关于java - 需要帮助 'combining' Java ArrayList 中存在部分重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32572240/

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