我有一个对象的问题。实际上,我必须把一个物体转换成另一个物体。在我的Category对象中包含一个类别(子类别)列表。这段代码中的主要问题是我们必须将所有子类别(Category对象)转换为CatgoryUI。
import java.util.ArrayList;
import java.util.List;
public class TestMain {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("In main");
TestMain tm = new TestMain();
List<Category> categories= tm.prepareList();
tm.displayCategories(categories);
List<CategoryUI> categoryList = tm.convertCategories(categories);
System.out.println("------Converted------");
tm.displayConvertedCategories(categoryList);
}
private List<Category> prepareList(){
//Category category = new Category();
List<Category> level3List1 = new ArrayList<Category>();
level3List1.add(new Category(4L, "Sentence Equalence", new ArrayList<Category>()));
level3List1.add(new Category(5L, "Antonyms", new ArrayList<Category>()));
level3List1.add(new Category(6L, "Text Completion", new ArrayList<Category>()));
List<Category> level3List2 = new ArrayList<Category>();
level3List2.add(new Category(7L, "Problem Solving", new ArrayList<Category>()));
level3List2.add(new Category(8L, "Logical Reasoning", new ArrayList<Category>()));
List<Category> level2List = new ArrayList<Category>();
level2List.add(new Category(2L, "Verbal", level3List1));
level2List.add(new Category(3L, "Quantative", level3List2));
List<Category> level1List = new ArrayList<Category>();
level1List.add(new Category(1L, "GRE", level2List));
return level1List;
}
private void displayCategories(List<Category> categories){
System.out.println("<ul>");
for(Category category : categories){
System.out.println("<li>");
System.out.println(category.getName());
System.out.println("</li>");
if(category.getSubCategory().size()>0){
displayCategories(category.getSubCategory());
}
}
System.out.println("</ul>");
}
}
package net.sankhya.debug;
import java.util.List;
public class Category {
private Long id;
private String name;
private List<Category> subCategory;
public Category(Long id, String name, List<Category> subCategory) {
super();
this.id = id;
this.name = name;
this.subCategory = subCategory;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Category> getSubCategory() {
return subCategory;
}
public void setSubCategory(List<Category> subCategory) {
this.subCategory = subCategory;
}
}
package net.sankhya.debug;
import java.util.List;
public class CategoryUI {
private Long id;
private String name;
private List<CategoryUI> subCategory;
public CategoryUI(){
}
public CategoryUI(Long id, String name, List<CategoryUI> subCategory) {
super();
this.id = id;
this.name = name;
this.subCategory = subCategory;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<CategoryUI> getSubCategory() {
return subCategory;
}
public void setSubCategory(List<CategoryUI> subCategory) {
this.subCategory = subCategory;
}
}
当你在html视图中用ul和li显示类别列表时,它将显示如下。并且在转换成CategoryUI对象后,如果打印列表,它也应该以同样的方式播放。所以,请大家就如何将Category转换为CategoryUI给出一些建议。
<ul>
<li>GRE</li>
<ul>
<li>Verbal</li>
<ul>
<li>Sentence Equalence</li>
<li>Antonyms</li>
<li>Text Completion</li>
</ul>
<li>Quantative</li>
<ul>
<li>Problem Solving</li>
<li>Logical Reasoning</li>
</ul>
</ul>
感谢
编写自己的转换方法,如下所示。。。
public List<CategoryUI> convertArray(List<Category> categoryArray){
List<CategoryUI> convertedArray = new ArrayList<CategoryUI>();
for (int i=0;i<categoryArray.size();i++){
convertedArray.add(convertObject(categoryArray.get(i)));
}
return convertedArray;
}
public CategoryUI convertObject(Category category){
return new CategoryUI(category.getId(),category.getName(),convertArray(category.getSubCategory()));
}
只需为每个要转换的对象调用convertObject(category)
。您可以创建一个正确类型的新对象,并用旧对象中的所有数据填充它。它还转换子类别的数组。如果您传入树的顶部节点,那么一个调用将遍历树的所有子类别并转换所有子类别,因此非常简单。
如果需要在应用程序中更频繁地使用Dozer,也可以考虑使用它它将自动执行映射。