[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ArrayList  to Collection

Posted on 2006-04-17
16
Medium Priority
?
4,724 Views
Last Modified: 2007-12-19
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

0
Comment
Question by:anumalas
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16475184
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
 
LVL 3

Author Comment

by:anumalas
ID: 16475242
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
 
LVL 92

Expert Comment

by:objects
ID: 16475290
Collection c = Arrays.asList(myFuncitonDTO.getMyBlockDTO());
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 16475357
I think
Collection c = myFuncitonDTO.getMyBlockDTO(); will do directly.
0
 
LVL 3

Author Comment

by:anumalas
ID: 16475522
Hi Shivaspk

This will not work for me.   Objects solution is some what acceptable for my problem.....
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16476613
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
 
LVL 3

Author Comment

by:anumalas
ID: 16484757
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16484803
webArray[] array = webCollection.toArray ( new webArray[] {} ) ;
0
 
LVL 3

Author Comment

by:anumalas
ID: 16484807
what are the required imports to use webArray[]. caon you explain more clearly?? thanks in advance
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 1000 total points
ID: 16484835
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
 
LVL 3

Author Comment

by:anumalas
ID: 16484865
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
 
LVL 30

Expert Comment

by:Mayank S
ID: 16484893
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
 
LVL 3

Author Comment

by:anumalas
ID: 16485007
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
 
LVL 92

Assisted Solution

by:objects
objects earned 1000 total points
ID: 16485027
b.setElements((Element[])(a.getElements().toArray(new Element[0])));
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question