• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Casting through diffrent tree branches.

 
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have an application that 'used' to send back collections of User objects in an ArrayList. There came a time when the ArrayList was too specific and we had to get more general so we changed our prog to return a Collection. The reason for this was because there were times when we needed a HashSet to guarantee uniqueness and an ArrayList wouldn't do this without programming the uniqueness into the method that would 'add' the elements.
If I have an ArrayList that is returned as a Collection and is then down cast into a HashSet I should have no problem becuase the 'architecture' of the ArrayList / HashSet is almost the same. I.E. They mimic a one dimensional array of 'Objects'.
My problem is here.. what happens if I upcast from ArrayList to Collection (the returned type), my 'calling' program receives this Collection and down casts it into a HashMap. This is still a collection, but, it is a functional diffrent collection in the sanse that is almost mimics a 2 dimensional array of key/value pairs.
Will my HashMap be populated with the objects in the collection with null or "" keys? Or will we lost something in the cast?
I know that you should not lose functionaly when you cast, but what about functionality that was never there to begin with?
 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
I think I may have found my answer.
If I am correct I can't go from ArrayList -> Collection -> HashMap because HashMap implements Map interface and not Collection or List. So I was mistaken, they are not on the same 'path' in the hierarchy tree.
I can however go from ArrayList -> Collection -> HashSet because HashSets are Lists which are sub classes of the Collection interface.

[This message has been edited by John Bateman (edited March 09, 2001).]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
here is the hierachy of the collection:
Collection-->List
Collection-->Set--> SortedSet
Map-->SortedMap
also, the Collection interfacces and the classes that implement them:
List-->ArrayList, Vector, and LinkedList
Set-->HashSet
SortedSet-->TreeSet
Map-->HashMap, HashTable
SortedMap-->TreeMap
hope this will help.
good day ^__^*
reply
    Bookmark Topic Watch Topic
  • New Topic