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.

Dani AI

Generated

A simple, reliable way to convert the tree of Category objects to CategoryUI objects is to walk the tree and create a new CategoryUI for each Category, recursively converting its children. This preserves the exact nesting and keeps UI-specific state separate from the domain model (useful if Category or CategoryUI will diverge later). The following shows the conversion pattern concisely.

private List<CategoryUI> convertCategories(List<Category> categories) {
    if (categories == null) return Collections.emptyList();
    return categories.stream()
        .map(this::mapCategory)
        .collect(Collectors.toList());
}

private CategoryUI mapCategory(Category c) {
    List<CategoryUI> children = (c.getSubCategory() == null || c.getSubCategory().isEmpty())
        ? Collections.emptyList()
        : convertCategories(c.getSubCategory());
    CategoryUI ui = new CategoryUI();
    ui.setId(c.getId());
    ui.setName(c.getName());
    ui.setSubCategory(children);
    return ui;
}

If you prefer less allocation and want the UI to reflect changes to the original object instantly, ’s suggestion to wrap a Category inside a lightweight CategoryUI (composition) is valid: have CategoryUI hold a reference to the original Category and build the view from that. Trade-offs: wrapping is quicker and simpler, but coupling the UI to the domain can cause surprises if Category mutates.

Practical tips: always null-check the subcategory list and return Collections.emptyList() instead of null to avoid NPEs; watch for cycles (trees normally don’t have them, but defensive checks help); and use the same traversal to render CategoryUI (displayConvertedCategories) so the HTML output matches the original. If the tree is very deep, consider an iterative conversion using a stack to avoid recursion limits.

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...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.