开发者

adding two objects to a hashMap? [closed]

开发者 https://www.devze.com 2023-01-11 05:08 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.

Have I correctly added an element to a hashTable?

Flows flows = new Flows(sIP,dIP);
FlowStatics开发者_JAVA百科 flowStatics = new FlowStatics(packetLength,timeArrival);

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


Replace this line

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

with this one

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


The code looks ok.

However you should make sure that Flows override equals and hashCode


To avoid compiler warnings, code it like this:

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

If you don't parametrize the myHashMap variable, then you can't add in the second line without getting warnings.


Here is working example on how to 'print' some hashmap statistics:

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 asked for adivice in a comment to another answer)


I would change HashMap myHashMap = new HashMap<Flows, FlowStatics>(); to HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>(); but the addig is completly ok.


If you are not going to use the flows and flowStatics variables again -

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


@Shervin

does it right?

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

精彩评论

暂无评论...
验证码 换一张
取 消