2010-08-13 88 views
1

我是否正确地添加了一个元素到一个hashTable?将两个对象添加到哈希映射?

Flows flows = new Flows(sIP,dIP); 
FlowStatics flowStatics = new FlowStatics(packetLength,timeArrival); 

HashMap myHashMap = new HashMap<Flows, FlowStatics>(); 
myHashMap.put(flows, flowStatics); 
+2

这个问题是无法回答的,因为无论是“正道”上,完全取决于你的计划的目的是。有一件事要注意:你用作键的对象必须正确实现'hashCode()'和'equals()'。 – Jesper 2010-08-13 12:02:45

+0

@红狮:是的,这是正确的(与@Jesper提到的警告)。记住,虽然你不只是“添加两个对象”,但是你正在创建一个('flows')和另一个('flowStatics')之间的映射,这样你可以使用'flows'来查找'flowStatics'关键。 – David 2010-08-13 12:02:55

+0

发布你的Flows类,我们可以告诉你它是否可以用作HashMap键 – 2010-08-13 12:04:43

回答

1

为了避免编译器警告,代码像这样替换该行

HashMap myHashMap = new HashMap<Flows, FlowStatics>(); 

HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>(); 
myHashMap.put(flows, flowStatics); 

如果不参数化的myHashMap变量,那么你不能在没有得到警告的情况下在第二行添加。


这里正在研究如何 '打印' 一些HashMap的统计例如:

HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>(); 
for (int i = 0; i < 10; i++) { 
    // OP commented that the map is populated in a loop 
    myHashMap.put(createNewFlow(), createNewFlowStatistics()); // populate map 
} 
System.out.printf("Number of items in Map: %s%n", myHashMap.keyset().size()); 

(OP要求adivice到另一个答案评论)

+0

谢谢我纠正它。 – 2010-08-13 12:14:49

3

代码看起来没问题。

然而,你应该确保Flows覆盖equalshashCode

+2

'hashCode',而不是'hashcode'。案例很重要! – Jesper 2010-08-13 12:04:29

0

我会改变HashMap myHashMap = new HashMap<Flows, FlowStatics>();HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>();但addig是完全地确定。

+0

但我无法打印出地图。 – 2010-08-13 12:15:38

+0

@红狮 - 你期望什么样的印刷效果? – 2010-08-13 12:25:38

+0

至少要检查键集的数量和它们的值 – 2010-08-13 12:33:37

3

与这一个

Map<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>(); 
0

@Shervin

对不对?

package myclassifier; 


public class Flows implements Comparable<Flows> { 

    String srcAddr, dstAddr, srcPort, dstPort, protocol; 

    public Flows(String sIP, String dIP){ 
     this.srcAddr = sIP; 
     this.dstAddr = dIP; 
    } 

    public int compareTo(Flows other) { 
      int res = this.srcAddr.compareTo(other.srcAddr); 
      if (res != 0) { 
       return res; 
      } 
      res = this.dstAddr.compareTo(other.dstAddr); 
      if (res != 0) { 
       return res; 
      } 
      res = this.srcPort.compareTo(other.srcPort); 
      if (res != 0) { 
       return res; 
      } 
      res = this.dstPort.compareTo(other.dstPort); 
      if (res != 0) { 
       return res; 
      } 
      return this.protocol.compareTo(other.protocol); 
} 

    @Override 
    public int hashCode() { 

     final int prime = 31; 
     int result = 1; 
     result = prime * result + ((dstAddr == null) ? 0 : dstAddr.hashCode()); 
     result = prime * result + ((dstPort == null) ? 0 : dstPort.hashCode()); 
     result = prime * result + ((srcAddr == null) ? 0 : srcAddr.hashCode()); 
     result = prime * result + ((srcPort == null) ? 0 : srcPort.hashCode()); 
     return result; 

    } 


    @Override 
    public boolean equals(Object obj) { 
     if (this == obj) 
      return true; 
     if (obj == null) 
      return false; 

     if (getClass() != obj.getClass()) 
      return false; 

     Flows other = (Flows) obj; 

     if (dstAddr == null) { 
      if (other.dstAddr != null) 
       return false; 
     } else if (!dstAddr.equals(other.dstAddr)) 
      return false; 

     if (dstPort == null) { 
      if (other.dstPort != null) 
       return false; 
     } else if (!dstPort.equals(other.dstPort)) 
      return false; 

     if (srcAddr == null) { 
      if (other.srcAddr != null) 
       return false; 
     } else if (!srcAddr.equals(other.srcAddr)) 
      return false; 

     if (srcPort == null) { 
      if (other.srcPort != null) 
       return false; 
     } else if (!srcPort.equals(other.srcPort)) 
      return false; 

     return true; 
    } 

} 
+0

是的,但您应该使用泛型,以便您不必手动投射。 您可以做的最安全的事情是请您的编辑为您生成它。 你也可以使用Lombok(www.projectlombok.org)并注释你的Class,它会为你生成。 – 2010-08-13 12:17:24

+0

我不能将LomBok添加到我的ide中,因为它不兼容。我不能这样做,因为我定制了我的netbeans以便与jnetpcap库一起工作,而不是仅添加一个库。 我不知道如何使用泛型。 但无论如何。 – 2010-08-13 12:38:36

+0

看起来很完美。我为你添加了“implements Comparable ”,以便compareTo实际上可以使用。 – 2010-08-13 12:41:26

0

如果你不打算再次使用flowsflowStatics变量 -

HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>(); 
myHashMap.put(new Flows(sIP,dIP), new FlowStatics(packetLength,timeArrival));