0

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.

  • If you delete an object from the map should both map-entries be deleted? – Flocke Mar 22 '18 at 14:16
  • Could you have a map for each event type? That might speed it up some. Otherwise it looks sensible in the context given. Don't worry about internal memory leaks in HashMap, only your own. – Jan Larsen Mar 22 '18 at 14:16
  • Yes, deleting an object should delete both entries. – Petre247 Mar 22 '18 at 14:19
  • If I have two maps would the overhead of managing both of these decrease the overall performance of the system when the events are called simultaneously? – Petre247 Mar 22 '18 at 14:21

0 Answers0