[英]How can I search for ArrayList elements in a Queue?
I have an Arraylist
containing accounts: 我有一个包含帐户的
Arraylist
:
static List<Account> accounts = new ArrayList<>();
where an Account
has these attributes: Account
具有以下属性:
private int accountId;
private String name;
private String email;
private double balance;
private String date;
and a Queue
of transactions 和交易
Queue
static Queue<Transaction> transactions = new LinkedList<Transaction>();
where a Transaction
has the following attributes Transaction
具有以下属性
private int transactionId;
private String type;
private double amount;
private String dateTime;
I want to use the accountId
to search for all the transactions made with the specified accountId
in my transactions' Queue
. 我想使用的
accountId
以搜索与指定进行的所有交易accountId
在我的交易Queue
。
How can I achieve this ? 我该如何实现?
Given an Account
class as follows: 给定一个
Account
类,如下所示:
package test;
public class Account {
private static int currentAccountId = 0;
private int accountId;
public Account() {
this.accountId = ++currentAccountId;
}
@Override
public String toString() {
return String.valueOf(this.accountId);
}
}
You should add a field of type Account
to your Transaction
class: 您应该在
Transaction
类中添加一个类型为Account
的字段:
package test;
import java.util.Date;
public class Transaction {
private static int currentTransactionId = 0;
private final int transactionId;
private final Account account;
private final String type;
private final double amount;
private final Date dateTime;
public Transaction(final Account account, final String type, final double amount) {
this.account = account;
this.type = type;
this.amount = amount;
this.dateTime = new Date();
this.transactionId = ++currentTransactionId;
}
public int getTransactionId() {
return this.transactionId;
}
public Account getAccount() {
return this.account;
}
public String getType() {
return this.type;
}
public double getAmount() {
return this.amount;
}
public Date getDateTime() {
return this.dateTime;
}
@Override
public String toString() {
return "Transaction: "
+ this.transactionId
+ "/ Account:"
+ this.account.toString()
+ " / Type:"
+ this.type
+ " / Amount:"
+ this.amount
+ " / Date:"
+ this.dateTime.toGMTString()
+ System.lineSeparator();
}
}
Then you can search for an Account
in your Queue
by reference. 然后,您可以通过引用在
Queue
搜索Account
。 Example with 3 accounts, 5 transactions, if you search for transactions related to account1: 如果您搜索与account1相关的交易,则示例包含3个帐户,5个交易:
package test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class Runner {
public static void main(final String[] args) {
List<Account> accounts = new ArrayList<Account>();
Account account1 = new Account();
Account account2 = new Account();
Account account3 = new Account();
accounts.add(account1);
accounts.add(account2);
accounts.add(account3);
Queue<Transaction> transactions = new LinkedList<Transaction>();
transactions.add(new Transaction(account1, "CREDIT", 10d));
transactions.add(new Transaction(account1, "CREDIT", 20d));
transactions.add(new Transaction(account2, "CREDIT", 5d));
transactions.add(new Transaction(account3, "CREDIT", 8d));
transactions.add(new Transaction(account1, "DEBIT", 3d));
List<Transaction> account1Transactions = new ArrayList<Transaction>();
for (Transaction transaction : transactions) {
if (transaction.getAccount().equals(account1)) {
account1Transactions.add(transaction);
}
}
System.out.println(Arrays.toString(account1Transactions.toArray()));
}
}
Running the program prints out the following output: 运行程序将输出以下输出:
[Transaction: 1/ Account:1 / Type:CREDIT / Amount:10.0 / Date:15 Dec 2015 15:56:54 GMT
, Transaction: 2/ Account:1 / Type:CREDIT / Amount:20.0 / Date:15 Dec 2015 15:56:54 GMT
, Transaction: 5/ Account:1 / Type:DEBIT / Amount:3.0 / Date:15 Dec 2015 15:56:54 GMT
]
Which matches the 3 transactions related to account1. 匹配与account1相关的3个交易。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.