我可以覆盖Map& lt; Integer,Integer& gt;与Map& lt; String,Integer& gt;一起使用? [英] Can I override a Map<Integer, Integer> with Map<String, Integer>?

查看:29
本文介绍了我可以覆盖Map& lt; Integer,Integer& gt;与Map& lt; String,Integer& gt;一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类 AccountWebappGridRow ,该类扩展了 AccountGridRow :

I have the following class AccountWebappGridRow, which extends AccountGridRow:

public class AccountWebappGridRow<Accounts> extends AccountGridRow implements java.io.Serializable {

AccountGridRow 包含具有以下字段:

private Map<Integer, Integer> balances;

使用公开获取器/设置器:

public Map<Integer, Integer> getBalances() {
  return balances;
}

public void setBalances(Map<Integer, Integer> balances) {
   this.balances = balances;
}

有没有一种方法可以某种方式覆盖/替换继承的

Is there a way that I can somehow override/replace the inherited

private Map<Integer, Integer> balances;

在我的 AccountWebappGridRow 中...与此相反:

in my AccountWebappGridRow...with this instead:

private Map<String, Integer> balances;

推荐答案

好的.您可以执行此操作……可以提供一个映射,该映射将允许您将 Integer 键转换为String键.

Sure. You can do that ... as soon as you can provide a mapping that will allow you to turn an Integer key into a String key.

换句话说:您的班级了解整数键和值.当然,您现在可以添加另一个使用相同值的地图.唯一需要的是创建一个有意义的映射功能.含义含义:符合您的要求的功能.我们不知道 Map< String,Integer> 应该包含什么内容.所以我们不能告诉您如何在此处正确映射键!

In other words: your class knows about Integer keys and values. Of course you can now add another map that uses the same values. The only thing required is that you create a meaningful mapping function. Meaningful meaning: a function that fits your requirements. We don't know what content the Map<String, Integer> is supposed to hold; so we can't tell you how to properly map keys here!

最简单的答案可能是使用

The most simple answer could be to use

String stringKey = someIntegerKey.toString();

resp.

Integer integerKey = Integer.parseString(stringKey);

有了这些映射,您现在可以获取内部映射,并创建一个再次使用整数的结果"映射.

With those mappings you can now take the internal map and create a "result" map that uses Integers again.

换句话说:您可以将该新地图添加到您的课程中;或者您可以完全重做您的课程并更改 balances 字段以使用其他键.所有这些都只是工作"-所有这些都依赖于 you 来定义如何从Integer转换为String键;反之亦然.

In other words: you can add that new map to your class; or you can completely rework your class and change that balances field to use a different key. All of that is just "work" - and all of that relies on you defining how you get from Integer to String keys; and vice versa.

这篇关于我可以覆盖Map&amp; lt; Integer,Integer&amp; gt;与Map&amp; lt; String,Integer&amp; gt;一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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