Link to home
Start Free TrialLog in
Avatar of anumalas
anumalas

asked on

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

Avatar of Mick Barry
Mick Barry
Flag of Australia image

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);
Avatar of anumalas
anumalas

ASKER

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..
Collection c = Arrays.asList(myFuncitonDTO.getMyBlockDTO());
I think
Collection c = myFuncitonDTO.getMyBlockDTO(); will do directly.
Hi Shivaspk

This will not work for me.   Objects solution is some what acceptable for my problem.....
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 () ) ) ;
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??

webArray[] array = webCollection.toArray ( new webArray[] {} ) ;
what are the required imports to use webArray[]. caon you explain more clearly?? thanks in advance
ASKER CERTIFIED SOLUTION
Avatar of Mayank S
Mayank S
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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.




SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial