Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向hashMap添加两个对象?_Java - Fatal编程技术网

Java 向hashMap添加两个对象?

Java 向hashMap添加两个对象?,java,Java,是否已将元素正确添加到哈希表中 Flows flows = new Flows(sIP,dIP); FlowStatics flowStatics = new FlowStatics(packetLength,timeArrival); HashMap myHashMap = new HashMap<Flows, FlowStatics>(); myHashMap.put(flows, flowStatics); 流量=新流量(sIP、dIP); FlowStatics Flow

是否已将元素正确添加到哈希表中

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

HashMap myHashMap = new HashMap<Flows, FlowStatics>();
myHashMap.put(flows, flowStatics);
流量=新流量(sIP、dIP);
FlowStatics FlowStatics=新的FlowStatics(包裹长度、到达时间);
HashMap myHashMap=新HashMap();
put(流、流静态);
代码看起来正常

但是,您应该确保
覆盖
等于
并且
哈希代码
代码看起来正常


但是,您应该确保
覆盖
等于
hashCode
我将更改
HashMap myHashMap=newhashmap()

to
HashMap myHashMap=newhashmap()但是addig是完全正常的。

我将更改
HashMap myHashMap=newhashmap()
to
HashMap myHashMap=newhashmap()但addig完全正常。

替换此行

HashMap myHashMap = new HashMap<Flows, FlowStatics>();
HashMap myHashMap=newhashmap();
用这个

Map<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>();
Map myHashMap=newhashmap();
更换该行

HashMap myHashMap = new HashMap<Flows, FlowStatics>();
HashMap myHashMap=newhashmap();
用这个

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

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

HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>();
myHashMap.put(flows, flowStatics);
HashMap myHashMap=newhashmap();
put(流、流静态);
如果没有参数化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());
HashMap myHashMap=newhashmap();
对于(int i=0;i<10;i++){
//OP评论说,映射填充在一个循环中
myHashMap.put(createNewFlow(),createNewFlowStatistics());//填充映射
}
System.out.printf(“映射中的项目数:%s%n”,myHashMap.keyset().size());

(OP在对另一个答案的评论中询问了advice)

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

HashMap<Flows, FlowStatics> myHashMap = new HashMap<Flows, FlowStatics>();
myHashMap.put(flows, flowStatics);
HashMap myHashMap=newhashmap();
put(流、流静态);
如果没有参数化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());
HashMap myHashMap=newhashmap();
对于(int i=0;i<10;i++){
//OP评论说,映射填充在一个循环中
myHashMap.put(createNewFlow(),createNewFlowStatistics());//填充映射
}
System.out.printf(“映射中的项目数:%s%n”,myHashMap.keyset().size());
(OP在对另一个答案的评论中要求与之告别)

@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;
    }

}
package-myclassifier;
公共类流实现了可比性{
字符串srcAddr,dstAddr,srcPort,dstPort,protocol;
公共流(串sIP、串dIP){
this.srcadr=sIP;
此.dstAddr=倾角;
}
公共int比较(其他流量){
int res=this.srcadr.compareTo(other.srcadr);
如果(res!=0){
返回res;
}
res=此.dstAddr.compareTo(其他.dstAddr);
如果(res!=0){
返回res;
}
res=this.srcPort.compareTo(other.srcPort);
如果(res!=0){
返回res;
}
res=此.dstPort.compareTo(其他.dstPort);
如果(res!=0){
返回res;
}
返回此.protocol.compareTo(其他.protocol);
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((dstAddr==null)?0:dstAddr.hashCode();
result=prime*result+((dsport==null)?0:dsport.hashCode();
result=prime*result+((srcadr==null)?0:srcadr.hashCode();
result=prime*result+((srcPort==null)?0:srcPort.hashCode();
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
其他流量=(流量)obj;
if(dstAddr==null){
if(other.dstAddr!=null)
返回false;
}如果(!dstAddr.equals(other.dstAddr))
返回false;
if(dstPort==null){
if(other.dstPort!=null)
返回false;
}如果(!dstPort.equals(other.dstPort))
返回false;
if(srcadr==null){
if(other.srcadr!=null)
返回false;
}else如果(!srcadr.equals(other.srcadr))
返回false;
如果(srcPort==null){
if(other.srcPort!=null)
返回false;
}如果(!srcPort.equals(other.srcPort))
返回false;
返回true;
}
}
@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;
    }

}
package-myclassifier;
公共类流实现了可比性{
字符串srcAddr,dstAddr,srcPort,dstPort,protocol;
公共流(串sIP、串dIP){
this.srcadr=sIP;
此.dstAddr=倾角;
}
公共int比较(其他流量){
int res=this.srcadr.compareTo(other.srcadr);
如果(res!=0){
返回res;
}
res=此.dstAddr.compareTo(其他.dstAddr);
如果(res!=0){
返回res;
}
res=this.srcPort.compareTo(other.srcPort);
如果(res!=0){
返回res;
}
res=此.dstPort.compareTo(其他.dstPort);
如果(res!=0){
返回res;
}
返回此.protocol.compareTo(其他.protocol);
}
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((dstAddr==null)?0:dstAddr.hashCode();
result=prime*result+((dsport==null)?0:dsport.hashCode();
R