is there some sort of map function I can use here instead of checking if it exists…
- User
is there some sort of map function I can use here instead of checking if it exists
Map<String, Object> eventData = (Map<String, Object>) data.get("eventData");
if(events.get(data.get("eventType").toString()) != null) { events.get(data.get("eventType").toString()).accept(eventData); } - Assistant
Yes, you can use the
getOrDefaultmethod of theMapinterface instead of checking if the key exists.Here's an example of how you can modify your code using
getOrDefault:Map<String, Object> eventData = (Map<String, Object>) data.get("eventData"); events.getOrDefault(data.get("eventType").toString(), (event) -> { // Default behavior when eventType doesn't exist // Handle default behavior here }).accept(eventData);In this example, the
getOrDefaultmethod returns a default value (a lambda expression) that will be executed if the key doesn't exist in theeventsmap. This lambda expression handles the default behavior you want to perform. Then, theacceptmethod is called on the returned value, passing in theeventDatamap as the argument.Make sure to provide the appropriate implementation for the default behavior in the lambda expression.