If I have an ArrayList in a Java 6 program as follows:
ArrayList<Keyword> = new ArrayList<Keyword>();
Where a Keyword has an int keywordNo, String text, int frequency and a few other fields.
How can I retrieve an element from the ArrayList if I know the keywordText but not the position of the element or the value of field keywordNo?
I know that I could use a loop and simply read through the ArrayList comparing Strings until I find the element but is there any better alternative?
Keywordobject matching a givenkeywordText?Listinterface has anindexOf()method which returns the index of a given input Object, but it uses theequals()method which might not work for your case if the other fields in aKeywordbe unknown.equalsmethod for the class in question.