如何在队列中搜索 ArrayList 元素?
How can I search for ArrayList elements in a Queue?
我有一个 Arraylist
包含帐户:
static List<Account> accounts = new ArrayList<>();
其中 Account
具有以下属性:
private int accountId;
private String name;
private String email;
private double balance;
private String date;
和 Queue
笔交易
static Queue<Transaction> transactions = new LinkedList<Transaction>();
其中 Transaction
具有以下属性
private int transactionId;
private String type;
private double amount;
private String dateTime;
我想使用 accountId
在我的交易 Queue
中搜索使用指定 accountId
进行的所有交易。
我怎样才能做到这一点?
给定一个 Account
class 如下:
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);
}
}
您应该将 Account
类型的字段添加到您的 Transaction
class:
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();
}
}
然后您可以在您的 Queue
中通过引用搜索 Account
。有 3 个账户、5 笔交易的示例,如果您搜索与账户 1 相关的交易:
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()));
}
}
运行 程序打印出以下输出:
[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
]
与 account1 相关的 3 笔交易匹配。
我有一个 Arraylist
包含帐户:
static List<Account> accounts = new ArrayList<>();
其中 Account
具有以下属性:
private int accountId;
private String name;
private String email;
private double balance;
private String date;
和 Queue
笔交易
static Queue<Transaction> transactions = new LinkedList<Transaction>();
其中 Transaction
具有以下属性
private int transactionId;
private String type;
private double amount;
private String dateTime;
我想使用 accountId
在我的交易 Queue
中搜索使用指定 accountId
进行的所有交易。
我怎样才能做到这一点?
给定一个 Account
class 如下:
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);
}
}
您应该将 Account
类型的字段添加到您的 Transaction
class:
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();
}
}
然后您可以在您的 Queue
中通过引用搜索 Account
。有 3 个账户、5 笔交易的示例,如果您搜索与账户 1 相关的交易:
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()));
}
}
运行 程序打印出以下输出:
[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
]
与 account1 相关的 3 笔交易匹配。