Refactor this code snippet [closed]

The common part with list and map working can be moved out from if/else:

Map<String, MyObject> hashMap = new HashMap<>();

List<Value> listOfvalues;
if (!hashMap.containsKey(key)) {
    listOfvalues = new ArrayList<>();
} else {
    listOfvalues = hashMap.get(key).getvalues();
    log.info(String.format("Duplicate key: %s. Previous Value(s): %s", key,  listOfvalues));
}

listOfvalues.add(Value.builder().withValue1(Value1).withValue2(Value2).build());
hashMap.put(key, MyObject.builder().withkey(key).withvalues(listOfvalues).build());

Leave a Comment