我在Java中的代码中遇到性能问题。我必须计算一个进程创建的每个对象的频率。对象的类型是下面给出的“节点”。 目前我已经在test1中使用下面给出的HashMap类型技术解决了这个问题。然而,这个过程非常缓慢,因为我的模拟生成了数百万个对象。此外,我必须在每次迭代期间拆分字符串,然后重新连接,等等。所以我正在考虑像下面在test2中给出的那样应用技术。但是HashMap会将每个新对象视为新条目,而不管对象内的内容如何。 我想知道如果有人知道如何有效地解决这个问题。您Node
类中Java中的性能问题
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;
}
}
覆盖'hashCode'你'Node'类? – 2014-09-11 00:28:51
@ C.B。和'equals' – resueman 2014-09-11 00:30:18
您使用'String'而不是'Node'作为关键字的任何特定原因? – Amadan 2014-09-11 00:31:21