-2

我有一个对象的问题。实际上我必须将一个对象转换为另一个对象。在我的 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>

谢谢

4

2 回答 2

1

编写自己的转换方法,像这样......

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)您要转换的每个对象。您有效地创建了一个正确类型的新对象,并用旧对象中的所有数据填充它。它还转换子类别数组。如果您传入树的顶部节点,则一次调用将遍历树的所有子类别并转换所有子类别,因此它既好又简单。

于 2013-06-28T07:38:37.660 回答
1

如果您需要在应用程序中更频繁地执行此操作,您也可以考虑使用Dozer ,它会自动执行映射。

于 2013-06-28T07:45:57.823 回答