Hi All,
This is Bhanu Teja M. I have a problem with an object. Actually i have to convert an object into another object. Here in my Category object contains a list of categories (subcategories). The main issues in this code is we have to convert the all subcategories (Category objects) into 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);
    }

    //Prepareing the object list
    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;

    }

    //Display the Category 
    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>");
    }

    //Convert Category into CategoryUI
    private List<CategoryUI> convertCategories(List<Category> categories){
        //Here i need to convert Category Object into CategoryUI Object.
    }
}





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;
    }
}






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;
    }
}

When you display the category list with in ul and li in html view it will display like below. And after converting into CategoryUI object if print the list it should also this play in the same way. So, can any please give any suggestions that how to convert the Category into 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>
</ul>

Thanks & Regards
Bhanu Teja M.

Once you create a Java object you can never change or convert it to another type of object.
Maybe what you need here is to create a list of new CatUI's, and have each CatUI contain a referemce to the Cat that it will display.
Something along these lines...

class Cat {.....

class CatUI {
   private Cat the cat;
   public CatUI(Cat c) {
      cat = c;
   }
   public void display() {
      System.out.println(cat.getName() + cat.getXxx...
This article has been dead for over six months. Start a new discussion instead.