Collection of object -> Array of another object??

Q

qazmlp1209

I have a case where it is required to convert the Collection of objects
of a class into an array of objects of an another class.

I have come up with the following sample code so far. I would like to
know whether there is any other better/shorter way of doing such a
conversion.

------------------------
// Object1Coll is of type Collection<Class1>
final int collectionSize = Object1Coll.size() ;
Class2[] class2Arr = new Class2[ collectionSize ] ;

int index = 0 ;

Class1 class1Obj = new Class1() ;

Iterator Object1CollIter = Object1Coll.iterator();
while ( Object1CollIter.hasNext() )
{
class1Obj = Object1CollIter.next() ;

class2Arr[ index ] = new Class2( class1Obj.getMemVal1()
, class1Obj.getMemVal2() ) ;

++index ;
}
 
V

VisionSet

....
I would like to
know whether there is any other better/shorter way of doing such a
conversion. ....
Iterator Object1CollIter = Object1Coll.iterator();
while ( Object1CollIter.hasNext() )
{
class1Obj = Object1CollIter.next() ;

class2Arr[ index ] = new Class2( class1Obj.getMemVal1()
, class1Obj.getMemVal2() ) ;

Any suggestion for improvements in the above code?

If you really do want to convert between 2 polymorphically distinct types,
then no.
Other than using the Java1.5 syntactic short cut for iteration:
Iterator Object1CollIter = Object1Coll.iterator();
while ( Object1CollIter.hasNext() )
{
class1Obj = Object1CollIter.next() ;

is equivalent to:

for (Class1 obj : object1Coll.iterator()) {}

neat huh?!
Class1 class1Obj = new Class1() ;

Oh, and that line is pointless. What are you doing with that object you just
instantiated? nothing!
a/ set it to null, or
b/ better reduce the scope by keeping the reference inside the loop
 
G

Greg R. Broderick

(e-mail address removed) wrote in @e56g2000cwe.googlegroups.com:
I have a case where it is required to convert the Collection of objects
of a class into an array of objects of an another class.

I have come up with the following sample code so far. I would like to
know whether there is any other better/shorter way of doing such a
conversion.

------------------------
// Object1Coll is of type Collection<Class1>
final int collectionSize = Object1Coll.size() ;
Class2[] class2Arr = new Class2[ collectionSize ] ;

int index = 0 ;

Class1 class1Obj = new Class1() ;

Iterator Object1CollIter = Object1Coll.iterator();
while ( Object1CollIter.hasNext() )
{
class1Obj = Object1CollIter.next() ;

class2Arr[ index ] = new Class2( class1Obj.getMemVal1()
, class1Obj.getMemVal2() ) ;

++index ;
}

Implement a method on Class1, that returns an appropriately-constructed
reference to an object of class2. This is more object-oriented, and allows
Class1 to hide more of its implementation-specific details.

Use Collection.toArray(new Class1[collectionSize]), then iterate this array
using a for loop. This should be faster than using an Iterator.

If Class2 is a superclass of Class1, you should be able to directly create
the array using Collection.toArray(new Class2[collectionSize]);

Cheers
GRB

--
---------------------------------------------------------------------
Greg R. Broderick [rot13] (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 
Q

qazmlp1209

Once the Collection -> Array conversion is completed, I have a case
later to do the reverse conversion i.e. converting from an array of a
object to Collection of an another object.
During this conversion, I just noticed that it is not possible to
initialize the Collection to a certain size(equivalent to
array.length). Is that really the case?
 
R

Roedy Green

During this conversion, I just noticed that it is not possible to
initialize the Collection to a certain size(equivalent to
array.length). Is that really the case

does this answer your concern?

public ArrayList(Collection<? extends E> c) {
size = c.size();
// Allow 10% room for growth
int capacity = (int) Math.min((size*110L)/100,
Integer.MAX_VALUE);
elementData = (E[]) c.toArray(new Object[capacity]);

The constructor picks the right size automatically.
 
I

IchBin

Once the Collection -> Array conversion is completed, I have a case
later to do the reverse conversion i.e. converting from an array of a
object to Collection of an another object.
During this conversion, I just noticed that it is not possible to
initialize the Collection to a certain size(equivalent to
array.length). Is that really the case?

I had to take 1.6 off my system because of space. There is an article
"Using the Desktop API in Java SE 6 (Mustang)" that touches on that API
and the author supplies a source link at the end of the article. Maybe
this could help?

http://java.sun.com/developer/technicalArticles/J2SE/Desktop/mustang/desktop_api/

Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
 
Q

qazmlp1209

"object1Coll.iterator()" does not seem to work.
I have a method like this:
private otherClass[] convertFromMineToOthers( Collection<myClass>
myObjColl )
{
for ( myClass myObj : myObjColl.iterator() )
{
// processing
}
}

"Can only iterate over an array or an instance of java.lang.Iterable"
error is reported for "myObjColl.iterator()". Why does it occur? As I
checked Collection<> is an iterable type. So, I expected it to work.
But, unfortunately the above-mentioned error is reported.
 
J

Jacques-Olivier Haenni

Hi,

The correct syntax is:

for (MyClass myObj : myObjColl) {
...
}

Note the '.iterator()' was wrong.

Cheers,

Jacques-Olivier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top