aspose file tools*
The moose likes Beginning Java and the fly likes Searching String in Object ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Searching String in Object ArrayList" Watch "Searching String in Object ArrayList" New topic
Author

Searching String in Object ArrayList

Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
Can anyone let me know that how can i search for a particular string in object ArrayList.
I have an ArrayList in which i store stateid and statename.
Now i want to get the stateid by searching for the statename in the arraylist. I dont want to use a for loop.
I came to know about the comparator inteface, but can anyone let me know about how can i use it for searching.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Dinesh Remje wrote:I dont want to use a for loop...

Erm...why not?

I came to know about the comparator inteface, but can anyone let me know about how can i use it for searching.

With a for (or for-each) loop.

It's the nature of the beast. A List contains "things"; and, since you don't know what they are, you have to search for them.

Now, if you're asking whether there's another way of storing them that would help you to find a particular value, that's an entirely different question; and the answer is: yes there is.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
I dont want to use for loop as the size of the arraylist can increase in future. Also if the string which i want to search is present at last in the arraylist, then i have to traverse the whole arraylist, each time when i want to search the same string and hence i don't like for loop.

Also i have used linkedhashmap and it worked also, but sometime i want to search for the name with the help id and at that time i need to make id as the key and sometimes i want to search the id with the help of name i need to make string as the key and thats where i need to create two linked hashmap and hence i dropped it. And hence i stick to the use of an arraylist. Can you please let me know how to use the arraylist for searching.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Dinesh Remje wrote:I dont want to use for loop as the size of the arraylist can increase in future.

Irrelevant, since all Lists have a size() method that tells you what its current size is.

Also if the string which i want to search is present at last in the arraylist, then i have to traverse the whole arraylist, each time when i want to search the same string and hence i don't like for loop.

Well, like I said - it's the nature of the beast. A List doesn't have any particular order, so you really have no choice but to search the whole thing. And don't forget that for every time you have to search the whole thing, there will be other occasions when it's the first element you find.

Also i have used linkedhashmap and it worked also, but sometime i want to search for the name with the help id and at that time i need to make id as the key and sometimes i want to search the id with the help of name i need to make string as the key and thats where i need to create two linked hashmap and hence i dropped it.

Well in my opinion, that was a mistake. And if you have one search that you do a lot more often than the other, then a LinkedHashMap is probably exactly what you want - although obviously, the latter search will take a lot longer then the first.

And hence i stick to the use of an arraylist. Can you please let me know how to use the arraylist for searching.

We're going around in circles here. Unless you can arrange elements in a particular order, you MUST search the whole List. Now there are a few options for ordering elements:
1. Sorting.
2. Creating a SortedSet or a Map from your List.
but either of those things will involve going through the entire List, even if you don't actually write the code.

Winston
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Winston Gutkowski wrote:Unless you can arrange elements in a particular order, you MUST search the whole List. Now there are a few options for ordering elements:
1. Sorting.
2. Creating a SortedSet or a Map from your List.
but either of those things will involve going through the entire List, even if you don't actually write the code.

Correct. If your list is relatively static - that is you have few additions but have to search in it a lot, then building the list, then sorting it, then using an search routine which relies on the sort-order can help a lot. The java.util.Collections class can help you with this.

If you change the list often relative to how often you need to search through it, constantly sorting the list will likely make things worse.

As has been mentioned, a Map makes a great tool for looking one value up based on another (get id from name, or get name from id). The problem is that you seem to want to have efficient lookups in both directions. You might look at one of the bi-directional map implementations found in third-party collection APIs:
Like Google Guava's BiMap
or Apache Common's BidiMap.


Steve
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Dinesh Remje wrote:I have an ArrayList in which i store stateid and statename.


And so your list is extremely small. That means that there is no point in spending any time in trying to reduce the amount of time spent in searching the list, since that amount is going to be extremely small as well.

Unless your application is completely concerned with searching that list -- which I expect it isn't.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2404
    
  28

Actually I wouldn't throw the 2 map solution out of the door too quickly. Having a map on top of a list is just like having an index on a database. It makes searches faster, but it comes at a cost. The more indexes you add, the more different kind of searches become faster, but inserts get slower.

Having 2 maps is exactly like that. 2 maps comes at the cost of memory and slight overhead of insertion into the map. It's not wrong to have 2 maps. The question that you should ask yourself is "is it worth the cost?"
Dinesh Remje
Ranch Hand

Joined: Jan 26, 2010
Posts: 57
No, i don't want to use two hashmap at a time it is not that worth....

Yup my list is extremely small, but at this point of time, my application is basically does a lot of searching in the arraylist, once storing the values in the arraylist is done.

Will Comparator inteface will help in searching the arraylist, by overriding the compare method.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Dinesh Remje wrote:I dont want to use a for loop.

wow! if it is an academic interest then consider recursion !
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2404
    
  28

If you don't want to keep 2 hash maps, then the only thing you can do is keep 2 sorted lists, and use a binary search.

However, if your list is small, the overhead of maintaining 2 lists will be higher than doing a for loop. How frequently do you have to search? How big is your list? How frequent are the updates?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Dinesh Remje wrote:Yup my list is extremely small, but at this point of time, my application is basically does a lot of searching in the arraylist, once storing the values in the arraylist is done.
Will Comparator inteface will help in searching the arraylist, by overriding the compare method.

Yes, because you can set up one for each type of search you want to do; but what it won't do is make the search any faster unless List elements are sorted in the same order as the Comparator.

If they are, you can use Collections.binarySearch(), but it's highly likely that for Lists with less than ≈30 elements, this will be no quicker than searching the whole thing with a loop, even if you do it on a sorted copy.

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Searching String in Object ArrayList