对象如何从HashMap中获取其键?
-
20-12-2019 - |
题
假设我有一个用HashMap定义的棋盘
HashMap <Position,Field> chessboard = new HashMap <Position,Field>();
我声明 Position
作为
class Position{
int x;
int y;
}
当我想上一堂课的时候。 Field
对象我遇到问题:该 Field
应包含一个 Position
因为它是由它定义的。前。
class Field {
Position pos;
int color;
void draw(){
// draw Field using pos
}
}
但是Position对象将用于HashMap。我怎样才能避免这种冗余?
解决方案
成本实际上并不是你想象的那样:你只持有一个单一的引用(在大多数系统上是四个字节)到相同的 Position
内存中的对象。开销是最小的;别担心。
不隶属于 StackOverflow