将Object转换为其他类型,包括Object的列表

时间:2013-06-28 07:27:19

标签: java type-conversion

我遇到了一个对象问题。实际上我必须将一个对象转换为另一个对象。这里的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对象后,如果打印列表,它也应该以相同的方式播放。所以,请提供任何有关如何将类别转换为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>

谢谢

2 个答案:

答案 0 :(得分: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)即可。您可以有效地创建一个正确类型的新对象,并使用旧对象中的所有数据填充它。它还转换子类别数组。如果你传入树的顶部节点,那么一个调用将遍历树的所有子类并转换所有子类,因此它很简单。

答案 1 :(得分:1)

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