In order to understand most of the methods in Collections interface,I have downloaded the API's but Im not sure how to make sense of it. Is there a more detailed Collections API tutorial with more examples so I can appreciate the use of each method in it?
Thanks in advance! [ September 11, 2007: Message edited by: Muni K Reddy ]
I can't say that I understand it perfectly either, but I would concentrate first on the interfaces and then on the concrete classes that implement the interfaces.
If you don't understand some of it such as what a HashMap and what a LinkedList is, you should study the data structures and algorithms.
Thanks for your reply Kaydell! I have actually done that I know the stucture - Collections, Lists, Sets, Maps, SortedMap etc. I also know which classes implement or extend which interface or class respectively. I can define a List, Set, Map. But,, given a problem, I dont know how to browse and find the appropriate method in the Collection API.
Lets say I have a String like : "Hello Javaranch heLLo JavaRanch". I would like to do 2 things
Reverse the words : "JavaRanch heLLo Javaranch Hello"
&, I would like to delete duplicates - so the output is "Javaranch Hello"
Please note that the string could be of any length and the method that matches the words has to be case insensitive(i.e heLLO = Hello) and the output should be sentence case like "Javaranch Hello"
My approach would be to parse the line get the tokens into a String array, convert everything into a small case then put them into a set. and display them in reverse order.
My question is not necessarily about the solution to above example. How do I find out that the approach I employ is the best one? How do I find out if there is a method in the Collections API which makes my life easier?
First you have to learn the features of each of the interfaces. Check out the tutorial link above to do this. When you know the features of each interface, you will know which one is best for your needs. At this point look at the JavaDoc for the interface you have chosen. This will give you list of all the known implementing classes. You can then choose which of these classes best suits your requirements.
For example, lets assume you want a collection that is ordered and contains no duplicates. Now, from your knowledge of the interfaces, you know that any class that implements the Set interface will not contain any duplicates. So look at the JavaDoc for the Set interface. Near the top of this page is a list of Known SubInterfaces. You want an ordered set - there's a subinterface called SortedSet - that sounds promising. Click on the link. On that page there is a list of implementing classes - ConcurrentSkipListSet and TreeSet. Click on the links for both of those and see which one is best for you.
I agree with the poster that recommended the SortedSet type.
To get the String objects to put into the SortedSet, you can split the original String object using the space character as a delimiter. You can use the method of String called split(). I believe that this will give you an array of Strings. Once you have an array of String object, I believe that you can put them into the SortedSet by iterating over the array of String objects in reverse order.