Suppose I have an ArrayList
of my custom Objects which is very simple. For example:
class Account
{
public String Name;
public In
You must use the Map for example:
private Map<String, int> AccountMap;
for (String account : accounts )
AccountMap.put(account, numberofid);
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.
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
.
I was thinking of extending ArrayList but I am sure there must be better way.
Generally speaking, this is poor design. Read Effective Java Item 16 for a better understanding as to why - or check out this article.
A better way to do this would be to use a 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.
accountMap.get(id);
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.
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.
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.
If you want to be able to index elements using an unique identifier, you may have a look into Map, and its implementation HashMap.
It could help you to solve your problem, by using a Map<Integer, Account>
.
map.put(id, account)
instead of 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:
for (Account acc : accounts) {
if (acc.getId() == yourId) {
return acc;
}
}
throw new NoSuchElementException();
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(Account a : accountList)
You could put this loop into a helper method that takes an Account
and compares it against each item.
For ordered lists, you have more efficient search options, but you will need to implement a search no matter what.