• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Help me .doubt in Collections

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 817
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 119
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sounds good.Thanks Philip.
 
Doe, a deer, a female deer. Ray, a pockeful of sun. Me, a name, I call my tiny ad ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic