需要在java中找出数据结构中的"关键"

pja*_*ain 1 java collections data-structures

我正在寻找一个数据结构,我需要存储值,然后根据key检索它.但在我的情况下,我的键和值将是相同的,所以我不能使用HashMap.HashSet也没用,因为在这种情况下我需要进行顺序搜索才能找到我的密钥.所以这是我正在寻找的例子:

class A{
   int x;
   int y;
   int z;

   equals(){
     //equals is overridden based only on int x and int y; and not on z.
   }
}

Class B{
  Map<A,z>mapA = new HasMap<>(A,z); - option 1 , not useful.
  //store values in mapA. my z is dummy, in fact I need only class A object on some condition match.

  fun(object k){
     if(mapA.containsValue(k))
     {
        /*here I need to get class object A if it matches with "k". There is no function available in map to get the key if it matches key. Also if I use HashSet, in that case I need to retrieve the value by iterating over the set which is not preferable. I wanted to achieve this search in O(1) or O(logn) */
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

任何建议将受到高度赞赏.

Edit1: Hashset只告诉密钥是否存在于集合中?它没有给我钥匙.要获得密钥,我必须遍历集合.如果我错了,请纠正我.

Era*_*ran 5

如果键是值,则HashSet正是您所需要的.搜索密钥将花费O(1)预期时间,与任何哈希表相同.

  • 详细说明,`HashSet`覆盖`contains()`来进行哈希查找,它是分摊的O(1).它与`ArrayList.contains()`的作用不同.:-) (2认同)