Get your CodeRanch badge!*
The moose likes Beginning Java and the fly likes Collection question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Collection question" Watch "Collection question" New topic
Author

Collection question

cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
Hi all
I am busy going through the java Collection framework.
I just need to know how do you use the data of your objects storing in a Collection.
Say you have object X, and add it to your collection or any other object how do you use that data,or is it only used for Strings and values.I only see examples with Strings.
Or do i miss the point.
Thanks
Cornel
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Cornel -
Are you talking about classes like ArrayList, HashMap, and LinkedList ?
If so, the objects you put in them, come back out as generic 'Objects' from class Object. You have to cast them back to whatever type of object they were when you put them in.
I'm not sure I'm answering your question, are we on the same page here?
-Bert


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
Hi again
That is correct, can you give me an example of how to do it.
Thanks for the reply
Cornel
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8764
    
    5
Cornel - Here is a simple example:
We'll make an ArrayList and add some objects of class 'Stuff' into the ArrayList.
Then (much later on after tons of complicated code ), we'll create an iterator for the ArrayList, iterate thru it, and as we get an Object back out we'll cast it to its original class (in this case 'Stuff'). Then we'll use a method in the Stuff class.


One note: when we retrieve an object out of the ArrayList it is returned with a reference type of class Object - if we don't cast it back to class Stuff we can't call any Stuff methods on it. (without the cast we would only be able to call methods from class Object)
A couple of cool things about these classes:
You can store different (heterogeneous) types of objects in a single collection.
Depending on the collection you use you can create:
key - value maps
sorted lists
sets (with guaranteed - no duplicate entries)
Also you get lots of cool helper methods, like with ArrayList (as opposed to just using an array), you can ask the ArrayList:
how many elements do you contain? (size( ) method)
do you have one of these objects already? (contains( ) method )
remove this object (i don't know where it is - you find it ( remove( ) method )
So, maybe a little extra overhead getting them set up, but then they are 'the cat's pajamas'
-Bert
[ January 05, 2003: Message edited by: Marilyn de Queiroz ]
cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
Hi Bert
Thanks for the great explanation it was here :
Iterator it = aList.iterator();// make an iterator
while(it.hasNext() ) {// iterate thru ArrayList
s = (Stuff) it.next(); // retrieve element from// ArrayList, and cast it
System.out.println("doStuff : " + s.doStuff(++count) );
the book, tutorials did not explained well,but now after i have read your post its now clear and i understand it much better.
Thanks again for your time for helping me understand.
Cornel
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Collection question
 
Similar Threads
Encapsulation and "getting" null references
populating a linked list from text file
Object hashCode ?
Naming convention for Map<Key,Map<Key,Value>>
Casting Object[] into String[]