I have an arraylist of strings. Within that,I have to find an entry which contains a particular substring. ArrayList 'contains' does not allow regular expressions - is there some way to retrieve the appropriate element without actually traversing through each entry? Also,when iterating through the entries(if at all required) ,is it possible to retrieve the index from the iterator?
I don't know of any method in the standard Java API that does exactly what you need. It's ofcourse easy to write a utility method which loops through the array and checks every string until it finds a string that matches. There's no way to do this without traversing the array. Note that the 'contains' method of ArrayList also traverses the array behind the curtains.
To get the index, you could use ListIterator instead of a plain Iterator. ListIterator contains methods 'nextIndex()' and 'previousIndex()', see the API documentation for details. To get a ListIterator instead of a plain Iterator from your ArrayList, call the method 'listIterator()' instead of 'iterator()' on the ArrayList.
The arraylist method contains overrides AbstractCollection.contains. In the API, AC contains states: "Returns true if this collection contains the specified element. More formally, returns true if and only if this collection contains at least one element e such that (o==null ? e==null : o.equals(e)).
This implementation iterates over the elements in the collection, checking each element in turn for equality with the specified element."
What this means to you is that the contains method is already doing the iteration work for you, and is using obj1.equals(obj2).
Therefore, I would suggest creating your own FancyString object that extends String and overrides equals to perform the regex that you'd like to perform.
Jeff, that's not a very elegant solution, and I don't think it is going to work.
Think about this: What would the implementation of the equals(...) method in class MyString look like? Remember that the object passed to it is another MyString object. Note that the equals() method should be implemented conform to what is described for Object.equals() in the API documentation. How are you going to compare those two MyString objects so that it will work according to what Ranadhir needs?
Also, probably Ranadhir is getting that array of strings from somewhere else in his software, that doesn't know about the MyString class. Now, he could first convert the array of strings to an array of MyString objects, but that would be very cumbersome. He could also use MyString in his software everywhere instead of the standard String, but that is strange and you'll loose all the benefits of the standard String class, including the special optimalisations that the Java compiler performs for strings.
[ June 15, 2007: Message edited by: Jesper Young ] [ June 15, 2007: Message edited by: Jesper Young ]
Joined: Jun 15, 2007
Jesper, Thanks for your comments. For the most part, you are correct. If I were in Ranadhir's situation, I would first look for a commons collection solution because they often have exactly what I'm looking for, maybe something akin to Satou's solution. If not, I would probably just iterate through the ArrayList, or secondarily implement your suggestion which was to write a utility class or method to save from writing the loop several times. It would be ridiculous to use this kind of custom string-like object throughout an application, and I concede that in a software system you're likely to receive an ArrayList of Strings intact and would not want to convert all Strings to this type of object. And for that, I'm sorry, I should have elaborated my intentions regarding this solution before posting it. So what I should have said is , if you really do not want to manually iterate over an ArrayList and want to make use of a regex in the contains method, and you have no concern for anything else and are programming in your own little ivory tower, you can do something like below...
Obviously, ostentatious concerns about elegance, the founding fathers desires for use of the equals() method, and String optimalisations do not come in to play here.