将包/类名称列表转换为父/子数据结构

时间:2013-05-29 10:50:28

标签: java algorithm data-structures collections tree

我有一个类名列表如下:

   String s1 = "com.mycompany.project.dao.hibernate.BaseDAOHibernate";
    String s2 = "com.mycompany.project.domain.Product";
    String s3 = "com.mycompany.project.domain.ProductCategory";
    String s4 = "com.mycompany.project.service.impl.ProductServiceImpl";
    String s5 = "com.mycompany.project.domain.User";
    String s6 = "com.mycompany.project.service.impl.ProductCategoryServiceImpl";
    String s7 = "com.mycompany.project.dao.hibernate.ProductCategoryDAOHibernate";
    String s8 = "com.mycompany.project.dao.hibernate.ProductDAOHibernate";

    String[] strings = { s1, s2, s3, s4, s5, s6, s7, s8};

我想使用以下类定义将此数组转换为树结构:

public class Item  {
    private String itemName;
    private List<Item> subItems;

}

该方法将获取上面的数组并生成以下对象。

item.ItemName = "com";
item.suItems = {"mycompany"};

item2.itemName = "mycompany";
item2.subItems = {"project");

item3.itemName = {"project"};
item3.subItems = {"dao", "domain", "service"}

......等等。

请告知如何执行此操作,因为我知道可能有数百个类的列表作为输入。

由于

4 个答案:

答案 0 :(得分:1)

这里有一些代码/伪代码可以帮助您处理递归方法:

public void add(Item node, String name)
{
    String prefix = the part of the name before the first '.'
    String suffix = the part of the name after the first '.'
    if (there is no suffix)
    {
        subItems.add(new Node(prefix));
    }
    else 
    {
        Item subItem = null;
        if (subItems contains an Item whose itemName is prefix)
        {
            subItem = that item
        }
        else 
        {
            subItem = new Node(prefix);
            subItems.add(subItem);
        }
        add(subItem, suffix);
    }
}

答案 1 :(得分:1)

请参阅我在C#上的实现,对于Java,您可以将其用作伪代码。

public class Item
{
    private String itemName;
    private List<Item> subItems = new List<Item>();

    public void Push(string[] namespaces, int index)
    {
        if (index >= namespaces.Length)
            return;

        foreach (Item child in subItems)
        {
            if (child.itemName == namespaces[index])
            {
                child.Push(namespaces, index + 1);
                return;
            }
        }

        Item newChild = new Item();
        newChild.itemName = namespaces[index];
        newChild.Push(namespaces, index + 1);
        subItems.Add(newChild);
    }
}

private static void Namespaces()
{
    String s1 = "com.mycompany.project.dao.hibernate.BaseDAOHibernate";
    String s2 = "com.mycompany.project.domain.Product";
    String s3 = "com.mycompany.project.domain.ProductCategory";
    String s4 = "com.mycompany.project.service.impl.ProductServiceImpl";
    String s5 = "com.mycompany.project.domain.User";
    String s6 = "com.mycompany.project.service.impl.ProductCategoryServiceImpl";
    String s7 = "com.mycompany.project.dao.hibernate.ProductCategoryDAOHibernate";
    String s8 = "com.mycompany.project.dao.hibernate.ProductDAOHibernate";

    String[] strings = { s1, s2, s3, s4, s5, s6, s7, s8 };

    Item root = new Item();
    foreach (string s in strings)
    {
        root.Push(s.Split('.'), 0);
    }
    // Do something with root variable.
}

我还建议使用HashMap而不是list。

答案 2 :(得分:0)

您可以在每个String上使用split()方法并在所有结果数组上循环:

for (String s: strings)
{
    for (String anotherS: s.split(".");
    {
        //assign to itam classes
    }
}

答案 3 :(得分:0)

根据定义,Parent/Child结构将是某种Tree。请考虑以下内容......

public class Item  {
   private String itemName;
   private Map<String, Item> subItems;
}

Map<String, Item> rootMap;

使用其他地方建议的split方法,然后遍历包中的元素,向下钻取rootMap的相应分支。

这是对List的改进,因为当你有共同的父包时,你必须通过List进行搜索,找到适当的Item添加新的封装