This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Casting through diffrent tree branches.

 
John Bateman
Ranch Hand
Posts: 320
  • Mark post as helpful
  • send pies
  • 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
  • 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).]
 
Martha Yeh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • 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 ^__^*
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic