从Java中的HashMap中获取关键对象 [英] Get key object out of a HashMap in Java

查看:55
本文介绍了从Java中的HashMap中获取关键对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Java中的HashMap中检索键的原始对象,什么是最好的方法?

I would like to retrieve the original object of a key in a HashMap in Java, what is the best way to do it?

例如

HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
Integer keyObj = new Integer(10);
Integer valueObj = new Integer(100);

// And add maybe 1 million other key value pairs here
//... later in the code, if I want to retrieve the valueObj, given the value of a key to be 10
Integer retrievedValueObj = map.get(10);

//is there a way to retrieve the original keyObj object with value 10 from map?

基本上,用户可以在此处仅查询key对象的key的任何值,10只是示例.一些评论说:您已经有了x对象,为什么要得到它?"嗯,这就像说您已经拥有价值对象,为什么要得到它?"一样.这就是HashMap数据结构,存储和检索的目的.

Basically, the user can query any value of key here just for the key object, 10 is just an example. Some comment say, "you already have the x object, why do you want to get it?" Well, this is the same as saying "you already have the value object, why do you want to get it?" That is the purpose for the HashMap data structure, store and retrieval.

检索值对象很容易,但是似乎没有多少人知道如何检索键对象.似乎很多人不明白为什么我要达到10的目标并问为什么?为什么不只值10.这只是一个大大简化的模型.

Retrieving a value object is easy but it seems no many people know how to retrieve the key object. It seems like many people don't get why I want to achieve the object of 10 and ask why? why not just value 10. This is just a greatly simplified model.

好吧,让我给出一点背景.keyObj是另一个数据结构中的数据,我需要此原始键对象的确切引用.说,有一个所有键值的链表,如果我想删除链表中的特定节点.

Well, let me give a little bit context. The keyObj is data in another data structure and I need the exact reference of this original key object. Say, there is a linked list of all the key values, and if I want to remove a particular node in the linked list.

我不仅对值"10"感兴趣,而且对存储位置(即Java中对该"10"的引用)感兴趣.目的.存储器中可能有许多"10".但是那个确切的对象正是我想要检索的.

I am not only interested in the value "10", but also the memory location, i.e. the reference in Java of that "10" object. There could be many "10"'s in memory. But that exact object is what I want to retrieve.

下面的迭代器方法答案给出了O(n)方法.但是我正在寻找的是给定键值的键对象的O(1)检索.

The iterator approach answer below give an O(n) approach. But what I am looking for is an O(1) retrieval of the key OBJECT given the key value.

我能想到的一种方法是将关键对象也存储在值中,例如

One way I can think of is to store the key object in value as well, like

class KeyAndValue {
     public Integer key;
     public Integer value;
     public KeyAndValue(Integer key, Integer value) {
         this.key = key;
         this.value = value;
     }
}

map<Integer, keyAndValueL> map = new map<Integer, keyAndValueL>();
Integer x = new Integer(10);
map.add(x, new KeyAndValue(x, 100));

//then I can retrieve the reference of x, given value of key 10
Integer newKeyObj = map.get(10).key;

但是这种方法使用了更多的内存,对我来说似乎是一个hack.我想知道Java中是否有更优雅的方法.

but this approach uses more memory and looks like a hack to me. I am wondering if there is a more elegant way in Java.

推荐答案

一个类似的方法,但更通用的方法是将键+值"作为条目存储,而不是将其封装在另一个类中.示例:

A similar aproach but more generic is to store the "key + value" as an Entry instead of encapsule in another class. Example:

    Map<Integer, Entry<Integer, Integer>> map = new HashMap<Integer, Entry<Integer, Integer>>();
    Integer x = new Integer(10);
    map.put(x, new AbstractMap.SimpleEntry<Integer, Integer>(x, 100));

    //then I can retrieve the reference of x, given value of key 10
    Entry<Integer, Integer> keyObj = map.get(10);

这篇关于从Java中的HashMap中获取关键对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆