• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Searching String in Object ArrayList

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Dinesh Remje
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


wow! if it is an academic interest then consider recursion !
 
Jayesh A Lalwani
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
reply
    Bookmark Topic Watch Topic
  • New Topic