具有多个原始键的Java Map

时间:2014-06-21 13:26:19

标签: java libgdx

我希望在Java / LibGDX中有一个可以将多个原始值作为键的映射。我的具体情况是我有多个位置(x,y,z)需要附加一个对象。

目前我正在使用带有List作为键的(对象)地图,但是没有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

您问题的简短回答是 no ,因为原始类型不能是Java Map键。问题的答案越长轻松,请创建Position类 -

static class Position {
  int x;
  int y;
  int z;
  Position(int x, int y, int z) {
    this.x = x;
    this.y = y;
    this.z = z;
  }
  public boolean equals(Object b) {
    if (b instanceof Position) {
      Position other = (Position) b;
      return this.x == other.x && this.y == other.y && this.z == other.z;
    }
    return false;
  }
  public int hashCode() {
    return (Integer.valueOf(x).hashCode() ^ Integer.valueOf(y)
        .hashCode()) & Integer.valueOf(z).hashCode();
  }
}

然后

Map<Position, Object> map;
相关问题