【发布时间】:2015-12-10 21:27:12
【问题描述】:
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,但还没有到达那里
note2:“lines”是一个 CSV 字符串的数组列表
我需要在注释所在的位置(说这里是我迷路的地方......)根据它们的键和值以及键和值匹配的每个对象将重复项组合在重复项中,合计金额(可能有负数,但如果我们使用 BigDecimal,这无关紧要)。因此,如果我有 3 行具有相同的键和值(不管数量不同),我将以 1 行带有该键和值以及 3 个数量的总和结束。
可能重复的示例数据的想法WithAmounts:
"0001, test1, 147.00"
"0001, test1, 129.00"
"0001, test1, -17.00"
"0002, test2, 7.00"
"0002, test2, -7.00"
"0003, test3, 30.00"
"0003, test3, -12.00"
我想以什么结尾:
"0001, test1, 259.00"
"0002, test2, 0.00"
"0003, test3, 18.00"
非常感谢任何帮助,我是一名初级开发人员,一直在为这个时间敏感的项目而苦苦挣扎。我确信从一开始就有更好的方法来做到这一点,没有我的 Pair 类等,也没有我的数组对,所以如果有人也向我展示那很好.. 但我也很想知道如何用我目前拥有的东西得到我需要的东西,用于学习目的。这都在“制作交易行”部分。
好的,所以我尝试了 Stefan 的方法,虽然我认为我已经弄清楚了。我正在纠正输出中的行数,没有重复,但是每行的所有数量似乎都非常大。比他们应该的要大得多。
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;
}
}
【问题讨论】:
-
您是否针对 java 1.8 进行编译?
-
这根本行不通,.contains() 调用将始终返回 false,因为您每次都在创建一个新对象。可能会考虑使用具有键/值对的
Map对象。它将使您能够使用 map.containsKey()。 -
实际上,这里有很多错误......不仅仅是尝试使用新的对象实例作为键......可能希望与您团队中更高级别的开发人员合作。
-
最大的问题,没有更高级别的开发者......
标签: java list loops arraylist duplicates