Translations keys into a Tree datastructure java

Viewed 23

I've got a list of unique translation keys like:

  • filtergrid.filter.cleared
  • filtergrid.item.added
  • filtergrid.item.removed
  • filtertoolbar.placeholder.text
  • footer.disclaimer
  • footer.disclaimer.loders
  • footer.policy
  • etc.

I have to seperate them by the dots and put it into a tree datastructure. I already have a node class for the tree:

public class KeyNode implements Serializable {

    private static final long serialVersionUID = 1L;
    
    private String name;
    private KeyNode parent;
    private List<KeyNode> children = new ArrayList<KeyNode>();
    
    public KeyNode(String name) {
        this.name = name;
    }
    
    public KeyNode(String name, KeyNode parent) {
        this.name = name;
        this.parent = parent;
        this.parent.children.add(this);
    }
    
    public KeyNode getParent() {
        return parent;
    }
    
    public void setParent(KeyNode parent) {
        this.parent = parent;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }

    public List<KeyNode> getChildren() {
        return children;
    }
    
    public void setChildren(List<KeyNode> children) {
        this.children = children;
    }
    
    @Override
    public String toString() {
        return name;
    }
}

How can I loop through all those translation keys and efficiently put them in a tree datastructure?

List<String> keys = translationService.findDistinctKeys(); //List with all translationskeys
for (String key : keys) {
    String[] splittedKey = key.split("."); 
    //TODO put in the tree datastructure
}
0 Answers
Related