User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 401,675 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,449 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 5736 | Replies: 6
Reply
Join Date: Oct 2004
Posts: 15
Reputation: Dounia is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Dounia Dounia is offline Offline
Newbie Poster

Help How to use collection in java

  #1  
Nov 13th, 2004

Hi,
I have this homework to do and I have to solve a method called:
public Collection toCollection(). Here is what i solved so far:

final class InventorySet {
private Map _data;
int thesize;
InventorySet() {
// TODO
_data = new HashMap();
}

/**
* Return the number of Records.
*/
public int size() {
// TODO
thesize = _data.size();
return thesize;
}
/**
* Return a copy of the records as a collection.
* Neither the underlying collection, nor the actual records are returned.
*/
public Collection toCollection() {
// Recall that an ArrayList is a Collection.
// TODO
}

Thank you very much for your help
Dounia
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2004
Posts: 6,059
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 419
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: How to use collection in java

  #2  
Nov 13th, 2004
>Here is what i solved so far
Nice, you managed to write a bit of framework, but none of the solution at all. Be more specific about what the homework entails and I might help.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to use collection in java

  #3  
Nov 13th, 2004
Step 1: what do you have?
Step 2: what do you want to have?
Step 3: how do you get from the data in step 1 to the data in step 2?

It's really really simple, you have a set of data and want to transform that into another set containing the same data.

Look at the API docs of Map. You'll find a method there that helps you a lot.
That's all I'll tell you, because it's all the help one can give you without writing the actual code which I'm definitely not going to do.
Reply With Quote  
Join Date: Oct 2004
Posts: 15
Reputation: Dounia is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Dounia Dounia is offline Offline
Newbie Poster

News Re: How to use collection in java

  #4  
Nov 13th, 2004
Thank you both for your suggestions. Here is what i came up with so far. Iam on the right track???
public Collection toCollection() {
// Recall that an ArrayList is a Collection.
// TODO
ArrayList list = new ArrayList();

String val;
Iterator i = _data.values().iterator();
while (i.hasNext())
{
val = (String) i.next();
list.add (val);
}
Collection copyList = list;
ArrayList c = new ArrayList(copyList);

return c;
}
Thank you for your help
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,693
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 195
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: How to use collection in java

  #5  
Nov 14th, 2004
Have you looked at the API docs for List and Map?
You can do what you did in 1 line of code (or 2 if you write it out in a rather verbose syntax which teachers might want).
Reply With Quote  
Join Date: Oct 2004
Posts: 15
Reputation: Dounia is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Dounia Dounia is offline Offline
Newbie Poster

Help Re: How to use collection in java

  #6  
Nov 15th, 2004

Hi,
Thank you Jwenting for the info. I looked at the Map interface doc and I found a method called Collection values() and as a comment it is saying: @return a collection view of the values contained in this map. Is this the method you've been talking about?? Well, it look similar to the method that the teacher is asking to complete but I am still confused on what to do in this method!!
Could please help??
Thank you
Dounia
Reply With Quote  
Join Date: Oct 2004
Posts: 15
Reputation: Dounia is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
Dounia Dounia is offline Offline
Newbie Poster

Solution Re: How to use collection in java

  #7  
Nov 15th, 2004

Hi Jwenting

Thank you very much for your help, never mind my previous message. I went to HashMap and I found the implementation of public Collection values()
Here it is:
public Collection values() {
Collection vs = values;
return (vs != null ? vs : (values = new Values()));
}
this helps a lot. It is almost similar to what i did previously so, I am chaning the code to:
public Collection toCollection() {
// Recall that an ArrayList is a Collection.
// TODO
ArrayList c = new ArrayList(_data);
Collection copyList = c;
return copyList;
}
Thank you for your help and for reminding me to check the Map inteface doc and that reminded me to check Hashmap.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 7:26 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC