Java 我应该使用HashMap、列表还是其他什么?
我正在写一个程序Java 我应该使用HashMap、列表还是其他什么?,java,list,arraylist,linked-list,hashmap,Java,List,Arraylist,Linked List,Hashmap,我正在写一个程序 存储键值对列表(场景、结果列表) 随机模拟新场景,查找结果 检查场景是否在键值对列表中 如果是,则更新该键值对,以便将新结果添加到列表中 否则,将场景结果对添加到键值对列表中 重复步骤2-5 目前我正在使用HashMap,因为它的键值系统是有意义的。然而,计算似乎需要很长时间,我正在考虑是否采用不同的数据结构更合适 模拟100个场景花费了8.623秒,留下4600个键值对的HashMap 模拟200个场景花费了42.690秒,在HashMap中留下了9431个键值对 看起来,键
@Override
public int hashCode() {
int result = 31 + legalBoard;
result = result*31 + playerToMove;
result = result*31 + Arrays.hashCode(getSmallestFieldConfiguration());
//System.out.println("Hashcode: " + result + " ---------- " + Arrays.toString(field));
return result;
}
legalBoard
是介于-1和8之间的整数playerToMove
为-1或1<代码>字段是值为-1、0和1的整数[81]。getSmallestConfiguration()
方法从数组的每个可能反射/旋转中查找最小的数组,如下所示:
public int[] getSmallestFieldConfiguration(){
int[] smallestConfig = field;
for(int[] config : getAllOtherFieldConfigurations()){
if(isGreater(smallestConfig, config)){
smallestConfig = config;
}
}
return smallestConfig;
}
public int[][] getAllOtherFieldConfigurations(){
int[][] configs = new int[7][];
int[][] twoDimensionalField = new int[][]{
{field[0],field[1],field[2],field[3],field[4],field[5],field[6],field[7],field[8]},
{field[9],field[10],field[11],field[12],field[13],field[14],field[15],field[16],field[17]},
{field[18],field[19],field[20],field[21],field[22],field[23],field[24],field[25],field[26]},
{field[27],field[28],field[29],field[30],field[31],field[32],field[33],field[34],field[35]},
{field[36],field[37],field[38],field[39],field[40],field[41],field[42],field[43],field[44]},
{field[45],field[46],field[47],field[48],field[49],field[50],field[51],field[52],field[53]},
{field[54],field[55],field[56],field[57],field[58],field[59],field[60],field[61],field[62]},
{field[63],field[64],field[65],field[66],field[67],field[68],field[69],field[70],field[71]},
{field[72],field[73],field[74],field[75],field[76],field[77],field[78],field[79],field[80]},
};
/*for(int i=0; i<81; i++){
twoDimensionalField[i%9][i/9] = field[i];
}*/
//Reflections
configs[0] = getFieldFromMatrix(MatrixTransformations.reflectVertical(twoDimensionalField));
configs[1] = getFieldFromMatrix(MatrixTransformations.reflectHorizontal(twoDimensionalField));
//Rotations
int[][] singleRotation = MatrixTransformations.rotate(twoDimensionalField);
configs[2] = getFieldFromMatrix(singleRotation);
int[][] doubleRotation = MatrixTransformations.rotate(twoDimensionalField);
configs[3] = getFieldFromMatrix(doubleRotation);
configs[4] = getFieldFromMatrix(MatrixTransformations.rotate(doubleRotation));
//Transpositions
configs[5] = getFieldFromMatrix(MatrixTransformations.transpose(twoDimensionalField));
configs[6] = getFieldFromMatrix(MatrixTransformations.transpose(doubleRotation));
return configs;
}
也许你应该坚持用地图。除非你的钥匙是按顺序排列的1,2,3,否则使用9K的随机数可能会导致你内存不足的问题 我们怎么知道?你没有发布任何代码。代码很重要。您应该发布包含长执行时间的代码。问题可能不在于地图,而在于它的使用方式或代码中的其他内容。同意davidxxx——根据(非常模糊的!)描述,猜测第4步很昂贵。“更新”列表听起来可能很昂贵,如果它涉及
list.contains(某物)
。其他的操作应该是O(1)(至少是你列出的那些,如果执行得当的话)@Marco13这也是我的怀疑。有没有快速“更新”键值的方法?我目前的方法似乎很慢;它类似于map.put(key,map.get(key).getUpdatedValue(myResult));只要你不发布getUpdatedValue
的代码,每个人都只能猜测。谢谢。我怀疑列表中的第4步(替换函数)是最耗时的。没有map.put(key,map.get(key),getUpdatedKey(thinghavetoadd))之类的东西,有没有更快的方法来“更新”给定键的值?正如前面所说的,更多的代码会更好,但您的瓶颈已经基本确定,你不应该花太多的时间去检查可用的结构,并测量什么可以使痛苦最小化。在做任何事情之前,我会使用一个分析器:VisualVM等。最好从硬数字开始。这会告诉你流血的地方。它可能是调用hashCode(…)后的特定部分
public static int[][] reflectVertical(int[][] arr){
for(int j=0; j<9; j++){
for(int k=0; k<4; k++){
int temp = arr[j][k];
arr[j][k] = arr[j][3-k];
arr[j][8-k] = temp;
}
}
return arr;
}
public static int[][] reflectHorizontal(int[][] arr){
for(int j=0; j<4; j++){
for(int k=0; k<9; k++){
int temp = arr[j][k];
arr[j][k] = arr[8-j][k];
arr[8-j][k] = temp;
}
}
return arr;
}
public static int[][] transpose (int[][] array) {
if (array == null || array.length == 0)//empty or unset array, nothing do to here
return array;
int width = array.length;
int height = array[0].length;
int[][] array_new = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
array_new[y][x] = array[x][y];
}
}
return array_new;
}
public static int[][] rotate(int[][] arr){
int[][] newArr = new int[9][9];
for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) {
newArr[i][j] = arr[8 - j][i];
}
}
return newArr;
}
}