要使用哪个java集合?
我很难决定哪个java集合最适合我的场景。目前,我正在读取一条记录,其中给出了一个“账号”和一个“客户名称”值 基于这些值,我需要根据第一个文件中给出的帐号和客户名称搜索另一个文件。问题是第二个文件中的帐号不是唯一的,所以我需要同时使用帐号和客户名称进行搜索 我希望将整个文件读入一个集合,并使用集合二进制搜索在第二个文件中查找关联的记录,而不是为从第一个文件读取的每个记录打开、读取、搜索或关闭第二个文件要使用哪个java集合?,java,collections,Java,Collections,我很难决定哪个java集合最适合我的场景。目前,我正在读取一条记录,其中给出了一个“账号”和一个“客户名称”值 基于这些值,我需要根据第一个文件中给出的帐号和客户名称搜索另一个文件。问题是第二个文件中的帐号不是唯一的,所以我需要同时使用帐号和客户名称进行搜索 我希望将整个文件读入一个集合,并使用集合二进制搜索在第二个文件中查找关联的记录,而不是为从第一个文件读取的每个记录打开、读取、搜索或关闭第二个文件 是否有某种类型的集合最适合此用途(如果有的话)?假设您有足够的内存,我可能会使用一个 其中C
是否有某种类型的集合最适合此用途(如果有的话)?假设您有足够的内存,我可能会使用一个 其中
CustomerRecord
是一个对象,其中包含您要查找的记录
然后创建一个密钥类:
public class AccountIdentifier {
public String accountNumber;
public String customerName;
public AccountIdentifier(String accountNumber, String customerName) {
this.accountNumber = accountNumber;
this.customerName = customerName;
}
public int hashCode() {
return (accountNumber+"#"+customerName).hashCode();
}
public boolean equals(Object obj) {
if(!(obj instanceof AccountIdentifier)) return false;
else {
AccountIdentifier id = (AccountIdentifier)obj;
return accountNumber.equals(id.accountNumber) && customerName.equals(id.customerName);
}
}
}
因此,您必须在内存中预加载第二个文件,方法是读取每条记录,并使用其中包含的数据创建一个CustomerRecord
实例,以及一个AccountIdentifier
以放入映射中:
map.put(账户标识符、客户记录)代码>
当需要搜索时,您从第一个文件中获得了accountNumber和customerName,然后执行以下操作:
AccountIdentifier accountIdentifier = new AccountIdentifier(accountNumber, customerName);
CustomerRecord record = theMap.get(accountIdentifier);
最后注释,如果你的文件太大而不能适应内存,那么你应该考虑使用一个缓存库,比如
假设你有足够的内存,我可能会使用.< /p>
其中CustomerRecord
是一个对象,其中包含您要查找的记录
然后创建一个密钥类:
public class AccountIdentifier {
public String accountNumber;
public String customerName;
public AccountIdentifier(String accountNumber, String customerName) {
this.accountNumber = accountNumber;
this.customerName = customerName;
}
public int hashCode() {
return (accountNumber+"#"+customerName).hashCode();
}
public boolean equals(Object obj) {
if(!(obj instanceof AccountIdentifier)) return false;
else {
AccountIdentifier id = (AccountIdentifier)obj;
return accountNumber.equals(id.accountNumber) && customerName.equals(id.customerName);
}
}
}
因此,您必须在内存中预加载第二个文件,方法是读取每条记录,并使用其中包含的数据创建一个CustomerRecord
实例,以及一个AccountIdentifier
以放入映射中:
map.put(账户标识符、客户记录)代码>
当需要搜索时,您从第一个文件中获得了accountNumber和customerName,然后执行以下操作:
AccountIdentifier accountIdentifier = new AccountIdentifier(accountNumber, customerName);
CustomerRecord record = theMap.get(accountIdentifier);
最后的注释,如果文件太大而不能适应内存,那么你应该考虑使用一个缓存库,比如
。你最好的方法是创建一个包含帐号和客户名称的对象。然后,您可以将客户文件读入a地图
。这里,CustomerInfo
是您的对象,它只包含客户名称和帐号,FileInfo
是您的对象,它包含从文件读取的所有信息。现在,您可以对地图进行简单的查找
请注意,您需要确保CustomerInfo
实现hashCode()
和equals()
,这样才能正常工作。您最好的选择是创建一个同时包含帐号和客户名的对象。然后,您可以将客户文件读入a地图
。这里,CustomerInfo
是您的对象,它只包含客户名称和帐号,FileInfo
是您的对象,它包含从文件读取的所有信息。现在,您可以对地图进行简单的查找
请注意,您需要确保CustomerInfo
实现hashCode()
和equals()
,这样才能正常工作。我认为这更多地取决于您如何定义记录,而不是实际的集合
您可以创建一个比较器
,它比较两条记录,并基本上考虑id和名称,如果这些匹配,则假定它是同一条记录
基于此,您可以使用定义的比较器在数组列表
(例如)中搜索符合条件的记录
只有当您只返回一个匹配项时,二进制搜索才有用,从方法签名可以看出,和在调用二进制搜索之前,您需要对集合进行排序
综上所述:
- 定义一个
比较器
,它接受两个记录
对象并检查它们是否具有相同的id/名称
- 例如,将所有记录加载到
数组列表中
- 把它们分类
- 调用
Collections.binarySearch
,使用已排序的集合和自定义比较器
我认为这更多地取决于你如何定义你的记录,而不是实际的收藏
您可以创建一个比较器
,它比较两条记录,并基本上考虑id和名称,如果这些匹配,则假定它是同一条记录
基于此,您可以使用定义的比较器在数组列表
(例如)中搜索符合条件的记录
只有当您只返回一个匹配项时,二进制搜索才有用,从方法签名可以看出,和在调用二进制搜索之前,您需要对集合进行排序
综上所述:
- 定义一个
比较器
,它接受两个记录
对象并检查它们是否具有相同的id/名称
- 例如,将所有记录加载到
数组列表中
- 把它们分类
- 调用
Collections.binarySearch
,使用已排序的集合和自定义比较器
为什么不更快一点呢
创建一个类Customer:
public class Customer {
private final int accountNumber;
private final String customerName;
public Customer (int accountNumber, String customerName) {
this.accountNumber = accountNumber;
this.customerName = customerName;
}
public boolean equals(Object o) {
//check if accountNumber and customerName are equal
}
public int hashCode() {
return 13*accountNumber + 31*customerName.hashCode();
}
}
public class CustomerBucket() {
private final int forAccountNumber;
private Map<String, Customer> map = HashMap<String, Customer>();
public CustomerBucket(int forAccountNumber) {
//...
}
public boolean equals(Object o) {
return o.forAccountNumber == this.forAccountNumber;
}
public int hashCode() {
return forAccountNumber;
}
}
public class AccountSearcher {
private final Set<CustomerBucket> set = new HashSet<CustomerBucket>();
public Customer getCustomer(int accountNumber, String name) {
return set.get(accountNumber).get(name);
}
}
公共类客户{
私人最终国际帐户号码;
私有最终字符串customerName;
公共客户(int accountNumber,字符串customerName){
this.accountNumber=accountNumber;
this.customerName=客户名称;
}
公共布尔等于(对象o){