将两个对象添加到hashMap?

时间:2010-08-13 12:00:33

标签: java

我是否正确地将一个元素添加到hashTable?

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

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

6 个答案:

答案 0 :(得分:3)

代码看起来不错。

但是,您应该确保Flows覆盖equalshashCode

答案 1 :(得分:3)

替换此行

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

这一个

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

答案 2 :(得分:1)

要避免编译器警告,请按以下方式编写代码:

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在另一个答案的评论中要求提供建议)

答案 3 :(得分:0)

我会改变HashMap myHashMap = new HashMap<Flows, FlowStatics>();HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>();但是addig完全没问题。

答案 4 :(得分: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;
    }

}

答案 5 :(得分:0)

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

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