ArrayList to Collection

Hi  

I have two DTO's called MyFunctionDTO.java and MyFunctionDTOImpl.java.

In First DTO we have code like bellow
public class MyFunctionDTO implements Serializable{

private MyBlockDTO[] bolck=null;
private String desc = null;

/** And getter, setters of those */
}

In the Second DTO we have code like this
public MyFucntionDTOImpl implements Serializable{

private Collection MyBlockDTO block = null;
private String desc = null;
/** and getter, setters of those*/
}

My Problem is that , i have to take all values from the MyFucntionDTO and convert arrylist into Collection Object and  like what we have in ImplDTO. For that i  have to write one helper class with one static method , which converts all ArrayList coming form the MyFucntionDTO to Collection Objects. And i have to pass MyFucnitonDTOImpl Objects as a collection to the Action and JSP in UserInterface .. Please Solve this is very urjent

LVL 3
anumalasAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

objectsCommented:
An ArrayList is already a Collectionm so can be passed directly

If you need to convert an array to a Collection use:

Collection c = Arrays.asList(myarray);
0
anumalasAuthor Commented:
but in my case MyBlockDTO[] is another DTO which contatins some getter and setter. I will explain clearly


MyFunctionDTO contains ---> MyBlockTDO Getter and setters, MyBlockDTO contains--> MyRightsDTO getters and setters  

MyFucntionDTOImpl contains pure collection objects.

My problem is that i want to convert MyFucntionDTO objects into pure collection objects. Now we are getting mixed list arraylists form some DTO's and Collections from some DTO's but my UserInterface accepts only collection objects so in my helper class assume like

public static Collection convert(Collection myFuntionDTO)
{
   /** Here how to parse myFuncitonDTO.getMyBlockDTO()//whcih will return MyBlockDTO[].  So how to convert that MyBlockDTO[] into Collection Object.
}
Internally MyBlockDTO contatins MyRolesDTO[] getter and setters,

so how to convert those objects form lower lever and centralize those into set of Collection Objects..
0
objectsCommented:
Collection c = Arrays.asList(myFuncitonDTO.getMyBlockDTO());
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Siva Prasanna KumarPrincipal Solutions ArchitectCommented:
I think
Collection c = myFuncitonDTO.getMyBlockDTO(); will do directly.
0
anumalasAuthor Commented:
Hi Shivaspk

This will not work for me.   Objects solution is some what acceptable for my problem.....
0
Mayank SAssociate Director - Product EngineeringCommented:
You want to store the values from the MyBlockDTO[] array in the MyFunctionDTO class to the MyBlockDTO collection in the MyFucntionDTOImpl class?

MyFunctionDTOImpl obj = new MyFucntionDTOImpl () ;
obj.setMyBlockDTO ( Collections.asList ( myFunctionDTOObject.getMyBlockDTO () ) ) ;
0
anumalasAuthor Commented:
Hi

I have one Collection like follow.

Collection webCollection = blockElements.getWebElements();

How to convert directly that webCollection in to  webArray[].  With out using iterator and loops.

Is there any quick solution??

0
Mayank SAssociate Director - Product EngineeringCommented:
webArray[] array = webCollection.toArray ( new webArray[] {} ) ;
0
anumalasAuthor Commented:
what are the required imports to use webArray[]. caon you explain more clearly?? thanks in advance
0
Mayank SAssociate Director - Product EngineeringCommented:
Well, you asked about webArray[] so I assumed that it is a class that you have defined? Do you have any webArray class??

>> How to convert directly that webCollection in to  webArray[].  

Otherwise you can just use an object array:

Object[] array = webCollection.toArray () ;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anumalasAuthor Commented:
My problem is like

I have 2 classes called A and B of same structure. Class A returns Collection Object. And Class B uses Array. Can I convert Class A's Collection Object into Class B's Array object. Is it Possible? If yes what is the procedure
0
Mayank SAssociate Director - Product EngineeringCommented:
Does the class A's collection hold the same type of objects which class B's array is supposed to hold? If yes, then you can do it.
0
anumalasAuthor Commented:
These are my two classes.

Pubilc class  A implements Serializabel{

private Collection elements = null;

public void setElements(Collection elements)
{
   this.elements = elements;
}

public Collection getElements(){
             return elements;  }
}


public class B implements Serializable{

private Elements[]  elements = null;
public void setElements(Elements[] elements)
{
this.elements = elements;
}
public Elements[] getElements(){
              return elements; }
}

So How to convert Class A's Elements Collection object into Class B's Element[] object.




0
objectsCommented:
b.setElements((Element[])(a.getElements().toArray(new Element[0])));
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.