views:

118

answers:

1

I have some data from server which looks like this. Each row is an array so the data comes as an array of arrays:

net      Person       age
net      Person       height

net      Address      streetname

org      Company      name
org      Company      location

com      School       color

com      School       number

From left to right I loop through the array with two for loops and build a tree-like structure of each row(each element is a parent of its follower) like below. After each inner loop i add that particular tree(row tree-like) to the an ArrayList. So each object in the ArrayList is like a tree. As you can see below.

+net
  Person
       age

+net
 Person
     height

+net
   Address
      streetname

+org
  Company
     name

+org
  Company
     location

+com
   School
       color

+com
  School
     number

This is my main question

After I have added the first object to the ArrayList, I would like to compare the subsequent objects in order to prevent duplicates. As you can see "Person" and "Address" has the same parent "net" so I would like both to be under the same parent so that there will be a single "net". You can also see that "age" and "height" also has the same parent "Person", I want both to go under "Person". "Company" will be under a single "org" and their children "name" and "location" will be under "Company". How can I compare them to achieve this behaviour?

I implemented the tree-like structure in a form like a linked list as you have spotted already.

//SUPER CLASS
public class Model {

    protected String name;
    protected Model parent = null;
    protected ArrayList<Model> children;

    public Model(String name ){
     this.setName(name);
     children = new ArrayList<Model>();
    }

    public void addChild(Model node) {
        children.add(node);       
    }

    public ArrayList<Model> getChildren() {
        return children;       
    }

}


// SUBCLASSES

public class cPackage extends Model{    
    public cPackage() {
     super();
    }
}

public class cClass extends Model{
    public cClass () {
     super();
    }
}

public class cMethod extends Model{
    public cMethod () {
     super();
    }
}

Each element in a row belongs to one of these subclasses. Each level of a the tree belongs to the same class.

My main question now is, how can I compare them efficiently and bring the required objects under their appropriate parent?

Please I need your ideas. If there is a code also I will appreciate that you add it to your suggestions or point me there.

Thank you all.

A: 

If you override .equals and .hashCode, you could use a Set (HashSet implementation) to do an O(1) lookup. I would recommend, like mmyers, that you look into some data structures - Java has many of them that are designed for more specialied things.

aperkins
Do you honestly think that OP knows what O(1) means? :-)
ChssPly76
don't forget Google collections:http://code.google.com/p/google-collections/
akf
@ChssPly I have to try - maybe the learning will come
aperkins