我可以覆盖Map< Integer,Integer>使用Map< String,Integer>吗? [英] Can I override a Map<Integer, Integer> with Map<String, Integer>?

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

问题描述

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

 公共类AccountWebappGridRow< Accounts>扩展AccountGridRow实现java.io.Serializable {

AccountGridRow 包含具有此字段:

  private Map< Integer,Integer>余额; 

公开的获取者/设置者:

  public Map< Integer,Integer> getBalances(){
退还余额;
}

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

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

  private Map< Integer,Integer>余额; 

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

  private Map< String,Integer>余额; 


解决方案

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



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



最简单的答案可能是使用

 字符串stringKey = someIntegerKey.toString(); 

resp。

  Integer integerKey = Integer.parseString(stringKey); 

有了这些映射,您现在可以使用内部

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


I have the following class AccountWebappGridRow, which extends AccountGridRow:

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

AccountGridRow contains has this field:

private Map<Integer, Integer> balances;

With public getters/setters:

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;

in my AccountWebappGridRow...with this instead:

private Map<String, Integer> balances;

解决方案

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.

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.

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&lt; Integer,Integer&gt;使用Map&lt; String,Integer&gt;吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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