74989

Can I override a Map with Map?

Question:

I have the following class <strong>AccountWebappGridRow</strong>, which extends <strong>AccountGridRow</strong>:

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

<strong>AccountGridRow</strong> contains has this field:

private Map<Integer, Integer> balances;

With <strong>public</strong> 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 <strong>AccountWebappGridRow</strong>...with this instead:

private Map<String, Integer> balances;

Answer1:

Sure. You can do that ... as soon as <strong>you</strong> can provide a mapping that will allow you to turn an <strong>Integer</strong> 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 <strong>you</strong> create a <em>meaningful</em> mapping function. <em>Meaningful</em> meaning: a function that fits <em>your</em> 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 <em>mappings</em> you can now take the <em>internal</em> 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 <em>you</em> defining how you get from Integer to String keys; and vice versa.

Answer2:

you can define a method that using stream turn the Map<Integer, Integer> into a Map<String, Integer>

public Map<String, Integer> getBalancesStringString() { return balances.entrySet() .stream() .collect(Collectors.toMap(e -> e.toString(), e -> e.getValue())); }

Recommend

  • Data Cleanup, post conversion from ALLCAPS to Title Case
  • init method of a Serlvet without ServletException
  • TCP HL7 message has .(period) as segment terminator
  • Serialize JSON array from a web request
  • ES6 imports in Node with --experimental-modules
  • Plot: color all larger than different color
  • Django Admin wont allow me to allocate permissions to Users or Groups
  • python regex [:alpha:]
  • Model to match this JSON for deserialization, field names with dashes
  • RegularExpressionValidator With DropDownList(asp.net)
  • Filter Values of Current Week with XQuery
  • C++ mySQL connector LINKER errors (Windows)
  • dojo 1.9: what annotation does declare.safeMixin add?
  • Can't close mysql connections in R
  • Visual Studio not stopping on an exception being thrown
  • Entity Framework Core: Include many-to-many related objects in WebAPI
  • Multicolor tooltip in Qt
  • SAXReader not re-ecape characters
  • Silverlight DependencyProperty.SetCurrentValue Equivalent
  • Zurb Foundation _global.scss meta styles for js?
  • error importing numpy
  • Checking free space on FTP server
  • Update CALayer sublayers immediately
  • JFileChooser in front of fullscreen Swing application
  • Join two tables and save into third-sql
  • How to set my toolbar fixed while scrolling android
  • How to make Safari send if-modified-since header?
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • unknown Exception android
  • Checking variable from a different class in C#
  • Django query for large number of relationships
  • Sorting a 2D array using the second column C++
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • How to push additional view controllers onto NavigationController but keep the TabBar?
  • How can I use threading to 'tick' a timer to be accessed by other threads?