拆分字符串并添加到数组列表

时间:2017-08-08 10:44:09

标签: java android string list tree

我有一个排序的字符串列表,我需要将其拆分并添加到树或某种列表中。例如,如果我有这些字符串:

    List<String> folders = new ArrayList<String>();
    folders.add("Buffet::soups::veg soup");
    folders.add("Buffet::soups::non veg soup");
    folders.add("Buffet::soups::non veg soup::chicken soup");
    folders.add("Vegetables");
    folders.add("Cheese");
    folders.add("Buffet::Starters");
    folders.add("Buffet::Sandwitch");
    folders.add("Buffet::Sandwitch::Cheese Sandwitch");
    folders.add("Buffet::soups::veg soup::tomato soup");

这样我就可以在列表或树中拥有与该类别相关联的类别和子类别。 任何帮助都将非常感谢。

2 个答案:

答案 0 :(得分:1)

使用类来定义类别/子类别,使用另一个类来定义产品。 您的类别是一个包含类别列表和产品列表的对象。

public class Category {

    private String name;
    private List<Category> subCategories;
    private List<Product> products;

    // getter/setters
}

public class Product {
    private String name;
    // any other property defining your product

    // getters/setters
}
用这个你可以建一棵树。使用工厂;一个实用程序类,甚至是类别对象的构建器(但那很脏)来构建你的树。

然后将String传递给Utility类,将其添加到树

public class ProductTreeBuilder {

    public static Category addToTree(Category mainNode, String toAdd) {
        if (mainNode == null) {
            mainNode = new Category("BaseCategory");
        }
        Category currentNode = mainNode;
        String[] path = toAdd.split("::");
        for (int i = 0; i < path.length; i++) {

            String nameCatOrProduct = path[i];
            // if end of path, it's a produc
            // if it's not in product list, add it
            if ((i == path.length - 1) && !currentNode.getProducts().contains(nameCatOrProduct)) {
                currentNode.getProducts().add(new Product(nameCatOrProduct));
            } else {
                // check if currentNode contains subCategory
                Category subCategory = currentNode.getSubCategories().stream().filter(c -> c.getName().equals(
                        nameCatOrProduct)).findFirst().orElse(null);
                if (subCategory == null) {
                    // subCategory doesn't exist yet, create it
                    subCategory = new Category(nameCatOrProduct);
                    currentNode.getSubCategories().add(subCategory);
                }
                // continue with the subCaegory
                currentNode = subCategory;
            }
        }

        return mainNode;

    }
}

然后你可以简单地使用你的工厂来构建你的树:

    Category tree = null;
    tree = ProductTreeBuilder.addToTree(tree, "cat1::cat2::test");
    tree = ProductTreeBuilder.addToTree(tree, "cat2::test");

答案 1 :(得分:1)

定义类别

class Category
{
    private String name = "";
    private List<Category> children = new ArrayList<>();
    private Category parent = null;
    public Category(String name)
    {
        this.name = name;
    }
    public void addChild(Category child)
    {
        child.parent = this;
        children.add(child);
    }
    public Collection<Category> children()
    {
        return new HashSet<>(children);
    }
    public Category parent()
    {
        return parent;
    }
    public String toString(){return name;}
    public String entireHierarchyToString()
    {
        return entireHierarchyToString(0);
    }
    private String entireHierarchyToString(int d)
    {
        String tmp = "";
        for(int i=0;i<d;i++)
            tmp += "\t";
        tmp += name;
        for(Category c : children)
            tmp += "\n" + c.entireHierarchyToString(d+1);
        return tmp;
    }
}

然后我们使用递归方法为我们构建树:

private void process(String[] path, int p, Category root)
{
    if(p >= path.length)
        return;
    String tmp = path[p];
    Category next = null;
    for(Category c : root.children()) {
        if (c.toString().equals(tmp)) {
            next = c;
            break;
        }
    }
    if(next == null) {
        next = new Category(tmp);
        root.addChild(next);
    }
    process(path, p+1, next);
}

在我们的主要方法中,我们可以使用以下逻辑:

 List<String> folders = new ArrayList<String>();
 folders.add("Buffet:soups:veg soup");
 folders.add("Buffet:soups:non veg soup");
 folders.add("Buffet:soups:non veg soup:chicken soup");
 folders.add("Vegetables");
 folders.add("Cheese");
 folders.add("Buffet:Starters");
 folders.add("Buffet:Sandwitch");
 folders.add("Buffet:Sandwitch:Cheese Sandwitch");
 folders.add("Buffet:soups:veg soup:tomato soup");

 Category root = new Category("[ROOT]");
 for(String s : folders)
 {
      process(s.split(":"), 0, root);
 }
 System.out.println(root.entireHierarchyToString());

这应打印出来:

[ROOT]
    Buffet
        soups
            veg soup
                tomato soup
            non veg soup
                chicken soup
        Starters
        Sandwitch
            Cheese Sandwitch
    Vegetables
    Cheese