Jump to content

Generic Collection questions of Java....


albertlee

Recommended Posts

1,

 

I really dont see any difference between Comparable interface and Comparator interface, beside their full class names are different and one has compare() method and the other has compareTo() method to be implemented.

SO, how does, for example, TreeSet, will ever involve the Comparator interface? (I know what Comparable does for TreeSet, or any sorted collection).

 

2,

 

What's the difference between:

List l = new ArrayList<String>();

and

List l = new ArrayList();

I see no difference in their functions.

 

3,

 

Collection<?> c = new ArrayList<String>();
[b]c.add(new Object());[/b]

why does the bold line causes a compile time error, while in the absense of it, there is no compile time error?

 

 

please help me

 

 

thanks

Link to comment
Share on other sites

1)

http://forum.java.sun.com/thread.jspa?threadID=624912&messageID=3559863

 

It is basically because they are meant for entirely different

circumstances. You'll notice that compare (from Comparator - 2 params) and compareTo (from Comparable - 1 param) take different numbers of parameters. compareTo is meant for when you have an object that you will be passing around and that object will need to be compared to other objects later on (for instance in a sortable collection). One object can then be compared to another by simply doing object1.compareTo(object2).

 

Comparator and compare() are meant (afaik, I haven't used it, only Comparable) for situations where a class/object (usually not the one you are implementing Comparator in/on) needs the ability to compare two objects (ie not compare itself to something else), possibly in a way/ordering that isn't the same as they would normally order themselves using Comparable.

 

2) and 3) are to do with a new feature in Java 1.5 called Generics. This allows you to use generic types/classes rather than specifying exactly that class/type a method will take as a parameter or will return or will use to store something inside etc. The class it will actually use can be inferred from the use of it later on in the code.

 

In the case of ArrayList and other collections (although it can be used in more than Collections), you pass the class that will be used as the "base" class for that collection when you create the ArrayList object. It then stores for instance String (as you passed that in) or any subclass of string, rather than storing Object or any subclass of object as it normally does.

 

This is beneficial in a couple of ways. First of all, it means that a compiler error can be generated if you pass in something of the wrong type into the ArrayList because it knows what type of objects the ArrayList is meant to store. For instance in (3), you tried to pass an Object into something that was meant to store String s and so it gave you an error as Object is not a subclass of String. Even though you are storing this ArrayList as a Collection which may use a different class as it's generic type, I'm pretty sure you are still confined by the original type used in ArrayList.

 

Secondly, you benefit from not having to typecast things all the time. Normally when you got something out of the ArrayList, you'd have to typecast it to String as get() for instance would return an Object, not a String. Now because the class that the ArrayList stores has been defined, rather than returning Object, it will return String, and so assuming you only want to deal with String (ie you haven't passed in some subclass of String and need to typecast it to that) you don't need to type cast it anymore.

 

There are other benefits to this and you can read more about this at -

 

http://java.sun.com/j2se/1.5.0/docs/guide/language/generics.html

http://java.sun.com/developer/technicalArticles/J2SE/generics/

 

Obviously, I used the example of extending String here, but you couldn't actually do this as String is declared final but you get the idea with other classes/types.

 

As far as I know, Generics are the same concept as C++ Templates although I haven't used either yet, only read about them (never needed them yet).

 

I haven't really used this much myself yet so I may have got some things wrong. Please correct me if I've made an error somewhere.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.