Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.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中的性能问题_Java - Fatal编程技术网

Java中的性能问题

Java中的性能问题,java,Java,我在Java代码中面临一个性能问题。我必须计算一个进程创建的每个对象的频率。对象的类型如下所示。 目前,我已经使用下面在test1中给出的HashMap类型技术解决了这个问题。然而,这个过程非常缓慢,因为我的模拟生成了100万个对象。此外,我必须在每次迭代期间拆分字符串,然后重新连接,等等。所以我想应用下面测试2中给出的技术。但是HashMap将每个新对象视为一个新条目,而不考虑对象中的内容。 我想知道是否有人知道如何有效地解决这个问题 public class NewMain { /**

我在Java代码中面临一个性能问题。我必须计算一个进程创建的每个对象的频率。对象的类型如下所示。 目前,我已经使用下面在test1中给出的HashMap类型技术解决了这个问题。然而,这个过程非常缓慢,因为我的模拟生成了100万个对象。此外,我必须在每次迭代期间拆分字符串,然后重新连接,等等。所以我想应用下面测试2中给出的技术。但是HashMap将每个新对象视为一个新条目,而不考虑对象中的内容。 我想知道是否有人知道如何有效地解决这个问题

public class NewMain {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
         String[] a={"c","d"};
         int[] b={1,2};

    Map<String, Integer> test1=new HashMap<>();
    test1.put(new Node(a, b).toString(), 1);
    test1.put(new Node(a, b).toString(), 3);
    System.out.println(test1.size()); // size is 1

         //////////////////////////
         Map<Node, Integer> test2=new HashMap<>();
         test2.put(new Node(a, b), 1);
         test2.put(new Node(a, b), 3);
         System.out.println(test2.size()); // size is 2

}
}
    class Node{
    String[] a;
    int[] b;

public Node(String[] a, int[] b) {
    this.a = a;
    this.b = b;
}
public String toString(){
 String result=null;
    for(String e:a)
        result+=e+"|";
    for(int e:b)
        result+=e+"|";
    return result;   
}

}
在节点类中尝试以下操作:

这告诉Java应该通过比较对象的哈希代码来比较对象——默认情况下只是比较指针,并确保哈希代码是基于类的内容计算的。这将为test2.size调用提供1的结果

或者,根据@bjlee72上面的建议,Eclipse给了我:

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Arrays.hashCode(a);
    result = prime * result + Arrays.hashCode(b);
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Node other = (Node) obj;
    if (!Arrays.equals(a, other.a))
        return false;
    if (!Arrays.equals(b, other.b))
        return false;
    return true;
}

重写你的节点类的hashCode?你使用String而不是Node作为键的任何特殊原因?在test2中,我使用Node作为键,但每次我将其插入HashMap时,它都被视为新键。因此,在最后,我将在HashMap中得到数百万个条目。我如何覆盖hashCode?任何建议如果您使用eclipse,您可以使用source->generate hashCode and equals弹出菜单自动生成hashCode and equals方法的定义。只需右键单击类名。太好了!只是一个简单的问题。我只想使用字符串数组a。所以我删除了hashCode中的第二个循环。但是,即使避免第一个循环,只使用公共int hashCode{returna.hasCode;}也会有问题。虽然它给出了正确的结果,但我想知道大量内容是否会发生冲突。现在您指出,我的equals代码确实存在冲突的风险,但Eclipse版本没有。猜测Map的实现,它可能首先执行一个hashCode进行快速比较,然后等于检查它是否只是冲突或实际匹配。请选择Eclipse版本而不是我的,因为它会更可靠。
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + Arrays.hashCode(a);
    result = prime * result + Arrays.hashCode(b);
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Node other = (Node) obj;
    if (!Arrays.equals(a, other.a))
        return false;
    if (!Arrays.equals(b, other.b))
        return false;
    return true;
}