比较链接对象 [英] Comparing Linked-objects

查看:130
本文介绍了比较链接对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些数据从服务器看起来像这样。每行都是一个数组,所以数据就是一个数组:

 
net Person age
net Person height

net地址街名

org公司名称
org公司位置

com学校颜色

com学校号

从左到右我循环使用两个for循环遍历数组,并构建每行的树状结构(每个元素是其追随者)如下所示。在每个内循环之后,我将特定的树(行树状)添加到ArrayList。所以ArrayList中的每个对象就像一棵树。如下所示。

 
+ net
个人
年龄

+网
个人


+净
地址
streetname

+ org
公司
名称

+ org
公司
位置

+ com
学校
颜色

+ com
学校



这是我的主要问题



将第一个对象添加到ArrayList之后,我想比较后续对象,以防止重复。正如你可以看到个人和地址具有相同的父净,所以我希望两者都在同一个父母下,以便有一个净。你也可以看到年龄和身高也有同样的父母人,我想要两个都在人下。 公司将属于单位,其子女姓名和地点将在公司下。
我如何比较它们来实现这个行为?



我已经像已经发现的链接列表一样实现了树状结构。 / p>

  // SUPER CLASS 
public class Model {

protected String name;
protected Model parent = null;
protected ArrayList&Model>孩子

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();
}
}

行中的每个元素都属于这些子类之一。我们主要的问题是,如何有效地比较它们,并将所需的对象放在适当的父级下?



/ p>

请我需要你的想法。如果还有一个代码,我也会感谢你把它添加到你的建议或指向我。



谢谢大家。

解决方案

如果您覆盖了.equals和.hashCode,则可以使用Set(HashSet实现)来执行O(1)查找。我会推荐,像mmyers,你看一些数据结构 - Java有许多是为更专门的东西而设计的。


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.

解决方案

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.

这篇关于比较链接对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆