I am working on creating a very performance-focused event-driven system. In this program, I have one object that needs to be linked to two different unique keys. The object contains parameters of what to do when one of two different events is triggered.
public class MonthConfiguration implements Comparable<MonthConfiguration>, Serializable {
final String monthID;
public final String displayID;
private final Double dte;
public boolean enabled;
...
public MonthConfiguration(String monthID, String displayID, boolean enabled, double dte) {
this.monthID = monthID;
this.displayID = displayID;
this.enabled = enabled;
this.dte = dte;
}
...
@Override
public int compareTo(MonthConfiguration o) {
return this.dte.compareTo(o.dte);
}
}
I currently need to quickly recall one of these objects in two different call backs that are triggered with unique keys
HashMap<String, MonthConfiguration> monthMap = new HashMap<>()
@Override
public void onEventOne(String key1) {
MonthConfiguration mon1 = monthMap.get(key1)
...
}
@Override
public void onEventTwo(String key2) {
MonthConfiguration mon2= monthMap.get(key2)
...
}
In the above example key1 != key2, however mon1 and mon2 are the same.
Currently I am using the code
MonthConfiguration mon = new MonthConfiguration (monthID, displayID,enabled, dte);
monthMap.put(key1, mon);
monthMap.put(key2, mon);
Is there a better way to do this? The number of MonthConfiguration objects is rather large and I am worried about the efficiency of this and possible memory leaks as objects are deleted/added to the map.