Java ArrayList 通过 Id 检索对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19774374/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ArrayList Retrieve object by Id
提问by Madhur Ahuja
Suppose I have an ArrayList<Account>
of my custom Objects which is very simple. For example:
假设我有一个ArrayList<Account>
非常简单的自定义对象。例如:
class Account
{
public String Name;
public Integer Id;
}
I want to retrieve the particular Account
object based on an Id
parameter in many parts of my application. What would be best way of going about this ?
我想Account
根据Id
应用程序许多部分中的参数检索特定对象。最好的方法是什么?
I was thinking of extending ArrayList
but I am sure there must be better way.
我正在考虑扩展,ArrayList
但我相信一定有更好的方法。
采纳答案by Amir Afghani
It sounds like what you really want to use is a Map
, which allows you to retrieve values based on a key. If you stick to ArrayList
, your only option is to iterate through the whole list and search for the object.
听起来您真正想要使用的是 a Map
,它允许您根据键检索值。如果您坚持使用ArrayList
,您唯一的选择就是遍历整个列表并搜索对象。
Something like:
就像是:
for(Account account : accountsList) {
if(account.getId().equals(someId) {
//found it!
}
}
versus
相对
accountsMap.get(someId)
This sort of operation is O(1)
in a Map
, vs O(n)
in a List
.
这种操作O(1)
在 a 中Map
,而O(n)
在 a 中List
。
I was thinking of extending ArrayList but I am sure there must be better way.
我正在考虑扩展 ArrayList 但我相信一定有更好的方法。
Generally speaking, this is poor design. Read Effective JavaItem 16 for a better understanding as to why - or check out this article.
一般来说,这是糟糕的设计。阅读Effective JavaItem 16 以更好地理解原因——或查看这篇文章。
回答by SpacePrez
ArrayList does not sort the elements contained. If you want to look for a single element in an ArrayList, you're going to need to loop through the list and compare each one to the value you're looking for.
ArrayList 不对包含的元素进行排序。如果您想在 ArrayList 中查找单个元素,您将需要遍历列表并将每个元素与您要查找的值进行比较。
Account foundAccount;
for(Account a : accountList){
if(a.Id == targetID){
foundAccount = a;
break;
}
}
if(foundAccount != null){
//handle foundAccount
}
else{
//not found
}
Alternatively, you can use a more intelligent data structure which does sort and keep information on the data contianed.
或者,您可以使用更智能的数据结构,它对所包含的数据进行排序和保存信息。
You'll want to research the Map interface, specifically the HashMap implementation. This lets you store each element in an order tied to a certain key. So you could place each of your objects in a HashMap with the Id as the key, and then you can directly ask the HashMap if it has an object of a certain key or not.
您需要研究 Map 接口,特别是 HashMap 实现。这使您可以按与某个键相关联的顺序存储每个元素。所以你可以把你的每个对象放在一个以Id为键的HashMap中,然后你可以直接询问HashMap是否有某个键的对象。
回答by Vae
Assuming that it is an unordered list, you will need to iterate over the list and check each object.
假设它是一个无序列表,您将需要遍历该列表并检查每个对象。
for(int i = 0; i < sizeOfList; i++) {
list.get(i).equals(/* What you compare against */)
}
There's also the other for
syntax:
还有其他for
语法:
for(Account a : accountList)
You could put this loop into a helper method that takes an Account
and compares it against each item.
您可以将此循环放入一个辅助方法中,该方法接受 anAccount
并将其与每个项目进行比较。
For ordered lists, you have more efficient search options, but you will need to implement a search no matter what.
对于有序列表,您有更高效的搜索选项,但无论如何您都需要执行搜索。
回答by FelasDroid
You must use the Map for example:
例如,您必须使用 Map:
private Map<String, int> AccountMap;
for (String account : accounts )
AccountMap.put(account, numberofid);
回答by Guillaume Poussel
Extending ArrayList
is almost never a good solution to your problem. This is a base Java implementation of List
, which allows you to store objects in a specific order, and retrieve them by their index.
扩展ArrayList
几乎从来都不是解决问题的好方法。这是 的基本 Java 实现List
,它允许您按特定顺序存储对象,并通过索引检索它们。
If you want to be able to index elements using an unique identifier, you may have a look into Map
, and its implementation HashMap
.
如果您希望能够使用唯一标识符对元素进行索引,您可以查看Map
它的实现HashMap
。
It could help you to solve your problem, by using a Map<Integer, Account>
.
它可以帮助您解决问题,通过使用Map<Integer, Account>
.
- Inserting objects:
map.put(id, account)
instead oflist.add(account)
- Retrieving objects:
map.get(id)
- 插入对象:
map.put(id, account)
代替list.add(account)
- 检索对象:
map.get(id)
This will be the fastest implementation. But, if you cannot change this, you can still iterate through your ArrayList
and find the right account:
这将是最快的实现。但是,如果您无法更改此设置,您仍然可以遍历您ArrayList
的帐户并找到正确的帐户:
for (Account acc : accounts) {
if (acc.getId() == yourId) {
return acc;
}
}
throw new NoSuchElementException();
回答by Raghu
A better way to do this would be to use a Map.
更好的方法是使用 Map。
In your case, you could implement it in the following way
在您的情况下,您可以通过以下方式实现它
Map<account.getId(), account>
you can use the "get" method to retrieve the appropriate account object.
您可以使用“get”方法来检索适当的帐户对象。
accountMap.get(id);
回答by Ahamadullah Saikat
Simple Solution:
简单的解决方案:
Account account = accountList.stream().filter(a -> a.getId() == YOUR_ID).collect(Collectors.toList()).get(0);