File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help me .doubt in Collections

 
sai Venka
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is my doubt

LinkedList(Collection c)

This is one of the LinkedList Constructor.when i construct this LinkedList i could pass any collection object.like ArrayList or HashMap.since these are all implements Collection Interface.but when i casting this as

5. HashMap one = new HashMap();
6. HashMap two = new HashMap();
7. two.put("2","two");
8. one.put("1", two);
9. Collection col = one.values();
10.ArrayList l = (ArrayList) col;

i am getting ClassCastException. here,my doubt is though ArrayList implements the Collection Interface why we cannot cast like this.i thought this due to different data structure.but when i do the below it is working fine.how.?

5. HashMap one = new HashMap();
6. HashMap two = new HashMap();
7. two.put("2","two");
8. one.put("1", two);
9. Collection col = one.values();
10.LinkedList l1 = new LinkedList(col);

//This col contains HashMap.different data structure.Could you please anyone explain this.
 
Timmy Marks
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An ArrayList is a Collection, but a Collection does not need to be an ArrayList. You cannot cast down the hierarchy like this unless you know that the Collection you have is an ArrayList. In the second example, you are calling a constructor which takes a Collection and returns a LinkedList filled with the elements of the Collection. There is also an ArrayList(Collection c) constructor if you need an ArrayList, but you can't just arbitrarily cast a Collection as an ArrayList (or as a LinkedList for that matter)
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Timmy Marks:
An ArrayList is a Collection, but a Collection does not need to be an ArrayList. You cannot cast down the hierarchy like this unless you know that the Collection you have is an ArrayList. In the second example, you are calling a constructor which takes a Collection and returns a LinkedList filled with the elements of the Collection. There is also an ArrayList(Collection c) constructor if you need an ArrayList, but you can't just arbitrarily cast a Collection as an ArrayList (or as a LinkedList for that matter)


thanx timmy...but its not a complete answer ...
 
Timmy Marks
Ranch Hand
Posts: 226
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx timmy...but its not a complete answer ...


And what, in your opinion, would a complete answer look like? If you have a question about my answer or a question about the original post, please ask it. If you don't understand the answer, tell me which part you don't understand, but posts like this one are not an incintive for me to try and help.

Furthermore, I feel that the answer I gave is a complete answer. The reason it doesn't work is in the second case, a constructor using a collection is called, but in the first case an illegal cast is attempted.

Beyond that, I try to go into detail about why the code is wrong, explaining, albiet very briefly, that one cannot arbitrarily cast objects into other objects just because they have common anscestors.

Any lingering doubts?

Tim


P.S. I didn't add anything else to my previous reply, only reworded it. If it is now a "complete answer" then the problem lies with either my English or yours.
 
amit taneja
Ranch Hand
Posts: 813
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya ..to be honest i didn't read the question carefully

but the answer is if we
do like

ArrayList L =new ArrayList(col);
will it work ?


and ArrayList l =(ArrayList)col will work when col hold the refence of Arraylist object


do comment if need

regards
 
Philip Heller
author
Ranch Hand
Posts: 119
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since the API declares that the return type of HashMap.values() is Collection, all you know is that some kind of collection will be returned. It might be an ArrayList, and it might not. It might be an instance of some inner class inside HashMap.

If you're curious, you can check the source code for HashMap. Or, even simpler, run the following:



I got , so it really is an inner class.

IMPORTANT NOTE :
Code like this is great for satisfying curiosity. That's all it's good for! Don't EVER rely on knowledge of how a class works, if that knowledge doesn't appear in the API. When the next rev of Java comes out, it's fair play for them to change the returned object to some other class that still implements Collection.
 
sai Venka
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sounds good.Thanks Philip.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic