File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Collections API Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collections API" Watch "Collections API" New topic
Author

Collections API

Muni K Reddy
Ranch Hand

Joined: Aug 23, 2007
Posts: 74
Hello Guys,

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 ]
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

Here is a link from the Sun tutorials:

Sun Tutorial On The Collections Framework

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.

Kaydell
[ September 11, 2007: Message edited by: Kaydell Leavitt ]
Muni K Reddy
Ranch Hand

Joined: Aug 23, 2007
Posts: 74
Originally posted by Kaydell Leavitt:
[QB]Here is a link from the Sun tutorials:

Sun Tutorial On The Collections Framework

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.

Kaydell

QB]

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.


For example:

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?

    Thanks
    Vivek
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3419
        
      12
    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.


    Joanne
    Kaydell Leavitt
    Ranch Hand

    Joined: Nov 18, 2006
    Posts: 688

    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.

    Best Regards,

    Kaydell
    [ September 12, 2007: Message edited by: Kaydell Leavitt ]
    Muni K Reddy
    Ranch Hand

    Joined: Aug 23, 2007
    Posts: 74
    Thanks Joanne and Kaydell !
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Collections API
     
    Similar Threads
    Humane Interfaces?
    Collections
    Using memory tricks to pass SCJP (maybe slightly Off Topic)
    Getting specific Objects out of a collection
    Getting Error to Stack program