HashMap :如何将Object本身替换为String

时间:2021-11-26 16:59:01
A a = new A();     //classA {   }

HashMap<String, Object> hm = new Hashmap<String,Object>();

hm.put("A", a);

My question is, How can i put the Object itself instead of "A" in same declaration?

我的问题是,我怎样才能将Object本身而不是“A”放在同一个声明中?

hm.put(`a??`, a);

5 个解决方案

#1


3  

You simply cannot do that, the language prohibits it. It would only be possible if your class A is a subclass of String which is not possible, since String is declared as final in Java.

你根本做不到,语言禁止它。只有当你的类A是String的子类是不可能的时候才有可能,因为String在Java中被声明为final。

With respect to you interview question: It's not possible due to the generic type parameter that was chosen for the declaration. You can read more about that in Bounded Type Parameters.

关于你的面试问题:由于为声明选择的泛型类型参数,这是不可能的。您可以在有界类型参数中阅读更多相关信息。

#2


0  

If the class held a non-changing decent String field, you could use that.

如果该类持有一个不变的体面字符串字段,您可以使用它。

// the id property must be a String, immutable and unique for each instance!
myMap.put(a.getId(), a);

#3


0  

If you want to make any object as a key in your HashMap, then that object has to be immutable.. Because, you don't want anyone to change your key, after you add them to your HashMap..

如果你想在HashMap中将任何对象作为键,那么该对象必须是不可变的。因为,在将它们添加到HashMap之后,你不希望任何人更改你的键。

Just imagine, if your keys are changed after insertion, you won't ever be able to find your inserted value..

试想一下,如果您的密钥在插入后被更改,您将无法找到插入的值。

But if your key is immutable, then if anyone tries to change your keys, he will actually create a new one for himself, but you will still have yours..

但如果你的密钥是不可改变的,那么如果有人试图改变你的密钥,他实际上会为自己创建一个新密钥,但你仍然会拥有你的密钥。

That is what happens in case you use String as your key in HashMap(They can't be changed).. So, if you want your object to be a key, either you make your class a subclass of String (that you can't do), or, just make your class immutable..

如果你在HashMap中使用String作为你的键(它们不能被改变),那就会发生这种情况。所以,如果你想让你的对象成为一个键,你要么让你的类成为String的子类(你可以' t do),或者,只是让你的类不可变..

#4


0  

A a = new A();     //classA {   }

Map<A, A> hm = new Hashmap<A, A>();

hm.put(a, a);

But I do not see any point of putting a->a

但我认为没有任何意义 - a

#5


0  

This is actually possible using a raw type, like this:

这实际上可以使用原始类型,如下所示:

Object key = ...;
Object value = ...;
Map<String, Integer> map = new HashMap<>();//a normal map
Map rawMap = map; // here is the raw type
rawMap.put(key, value); // it works!

This runs fine, but problems arise when you try to use the generic map later:

运行正常,但是当您稍后尝试使用通用映射时会出现问题:

Integer value = map.get(key);// ClassCastException (unless value actually is an Integer)

That's why you were told that it's a "dirty trick". You shouldn't use it.

这就是为什么你被告知这是一个“肮脏的把戏”。你不应该使用它。

#1


3  

You simply cannot do that, the language prohibits it. It would only be possible if your class A is a subclass of String which is not possible, since String is declared as final in Java.

你根本做不到,语言禁止它。只有当你的类A是String的子类是不可能的时候才有可能,因为String在Java中被声明为final。

With respect to you interview question: It's not possible due to the generic type parameter that was chosen for the declaration. You can read more about that in Bounded Type Parameters.

关于你的面试问题:由于为声明选择的泛型类型参数,这是不可能的。您可以在有界类型参数中阅读更多相关信息。

#2


0  

If the class held a non-changing decent String field, you could use that.

如果该类持有一个不变的体面字符串字段,您可以使用它。

// the id property must be a String, immutable and unique for each instance!
myMap.put(a.getId(), a);

#3


0  

If you want to make any object as a key in your HashMap, then that object has to be immutable.. Because, you don't want anyone to change your key, after you add them to your HashMap..

如果你想在HashMap中将任何对象作为键,那么该对象必须是不可变的。因为,在将它们添加到HashMap之后,你不希望任何人更改你的键。

Just imagine, if your keys are changed after insertion, you won't ever be able to find your inserted value..

试想一下,如果您的密钥在插入后被更改,您将无法找到插入的值。

But if your key is immutable, then if anyone tries to change your keys, he will actually create a new one for himself, but you will still have yours..

但如果你的密钥是不可改变的,那么如果有人试图改变你的密钥,他实际上会为自己创建一个新密钥,但你仍然会拥有你的密钥。

That is what happens in case you use String as your key in HashMap(They can't be changed).. So, if you want your object to be a key, either you make your class a subclass of String (that you can't do), or, just make your class immutable..

如果你在HashMap中使用String作为你的键(它们不能被改变),那就会发生这种情况。所以,如果你想让你的对象成为一个键,你要么让你的类成为String的子类(你可以' t do),或者,只是让你的类不可变..

#4


0  

A a = new A();     //classA {   }

Map<A, A> hm = new Hashmap<A, A>();

hm.put(a, a);

But I do not see any point of putting a->a

但我认为没有任何意义 - a

#5


0  

This is actually possible using a raw type, like this:

这实际上可以使用原始类型,如下所示:

Object key = ...;
Object value = ...;
Map<String, Integer> map = new HashMap<>();//a normal map
Map rawMap = map; // here is the raw type
rawMap.put(key, value); // it works!

This runs fine, but problems arise when you try to use the generic map later:

运行正常,但是当您稍后尝试使用通用映射时会出现问题:

Integer value = map.get(key);// ClassCastException (unless value actually is an Integer)

That's why you were told that it's a "dirty trick". You shouldn't use it.

这就是为什么你被告知这是一个“肮脏的把戏”。你不应该使用它。