user1707035
user1707035

Reputation:

Searching in an arraylist consisting of userdefined datatype

I am trying to develop something like a Contact application where i need to add/delete/search/sort the Contacts. So, i'm using an ArrayList of MyContact objects as the data structure.

MyContact contains name and number, so to sort the ArrayList, I think it might be a time consuming task to look for a pattern in the ArrayList.

Currently i am traversing over each element and checking for the pattern. I have tried to first sort the list but that too is time-consuming.

Please suggest any better way to search in arraylist or if I should switch to another data type, which i think at this stage would be little risky.

Upvotes: 0

Views: 191

Answers (1)

Sean Connolly
Sean Connolly

Reputation: 5801

Sounds like you are trying to optimize early. It's a common stance in software development to make it work first and then, after identifying real performance issues, optimize. That said, you're not asking for software development advice but programming advice.

In terms of programming, you have two things to optimize here; performance and code.

To keep the code clean, try to utilize frameworks/conventions that already exist rather than roll your own. Take a look at uses of the Comparable interface and Java's Collections class. After that is the Apache Commons Collections library which has been an industry standard for some time. Google's Guava seems to be replacing Commons Collections as the standard these days for a lot of reasons, most notable being better Generics support.

As for performance, to sort or filter you need to either evaluate each record/object (at least once) for it's correct order/inclusion in the results. The only real alternative to this is to 'index' your data prior to searching. For this the (Java) industry standard is Lucene but is very likely overkill for your needs.

Upvotes: 2

Related Questions