| | |
How to use collection in java
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Oct 2004
Posts: 15
Reputation:
Solved Threads: 0
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
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.
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.
•
•
Join Date: Oct 2004
Posts: 15
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Oct 2004
Posts: 15
Reputation:
Solved Threads: 0
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
•
•
Join Date: Oct 2004
Posts: 15
Reputation:
Solved Threads: 0
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.
![]() |
Similar Threads
- book collection Java code (Java)
- How to implement a binary tree Using HashMap Collection in java (Java)
- Begin my first lesson to learn java (Java)
Other Threads in the Java Forum
- Previous Thread: can someone help i need to add a subclass
- Next Thread: Need help with java string.format (prepayment calculation)
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error event exception fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update variablebinding windows working xor






