Source:
private void updateExistingAndNewOptions(final GlusterVolumeEntity existingVolume, Collection<GlusterVolumeOptionEntity> fetchedOptions) { Map<String, GlusterVolumeOptionEntity> existingOptions = new HashMap<String, GlusterVolumeOptionEntity>(); Map<String, GlusterVolumeOptionEntity> newOptions = new HashMap<String, GlusterVolumeOptionEntity>(); for (final GlusterVolumeOptionEntity fetchedOption : fetchedOptions) { final GlusterVolumeOptionEntity existingOption = existingVolume.getOption(fetchedOption.getKey()); if (existingOption == null) { newOptions.put(fetchedOption.getKey(), fetchedOption); } else if (!existingOption.getValue().equals(fetchedOption.getValue())) { fetchedOption.setId(existingOption.getId()); existingOptions.put(fetchedOption.getKey(), fetchedOption); } } final List<GlusterVolumeOptionEntity> newOptionsSortedList = new ArrayList<GlusterVolumeOptionEntity>(newOptions.values()); final List<GlusterVolumeOptionEntity> existingOptionsSortedList = new ArrayList<GlusterVolumeOptionEntity>(existingOptions.values()); Collections.sort(newOptionsSortedList); Collections.sort(existingOptionsSortedList); if (!newOptionsSortedList.isEmpty()) { TransactionSupport.executeInScope(TransactionScopeOption.Required, new TransactionMethod<Void>() { @Override public Void runInTransaction() { saveNewOptions(existingVolume, newOptionsSortedList); return null; } }); } if (!existingOptionsSortedList.isEmpty()) { TransactionSupport.executeInScope(TransactionScopeOption.Required, new TransactionMethod<Void>() { @Override public Void runInTransaction() { updateExistingOptions(existingVolume, existingOptionsSortedList); return null; } }); } }

Target:
private void updateExistingAndNewOptions(final GlusterVolumeEntity existingVolume, Collection<GlusterVolumeOptionEntity> fetchedOptions) { Map<String, GlusterVolumeOptionEntity> existingOptions = new HashMap<>(); Map<String, GlusterVolumeOptionEntity> newOptions = new HashMap<>(); for (final GlusterVolumeOptionEntity fetchedOption : fetchedOptions) { final GlusterVolumeOptionEntity existingOption = existingVolume.getOption(fetchedOption.getKey()); if (existingOption == null) { newOptions.put(fetchedOption.getKey(), fetchedOption); } else if (!existingOption.getValue().equals(fetchedOption.getValue())) { fetchedOption.setId(existingOption.getId()); existingOptions.put(fetchedOption.getKey(), fetchedOption); } } final List<GlusterVolumeOptionEntity> newOptionsSortedList = new ArrayList<GlusterVolumeOptionEntity>(newOptions.values()); final List<GlusterVolumeOptionEntity> existingOptionsSortedList = new ArrayList<GlusterVolumeOptionEntity>(existingOptions.values()); Collections.sort(newOptionsSortedList); Collections.sort(existingOptionsSortedList); if (!newOptionsSortedList.isEmpty()) { TransactionSupport.executeInScope(TransactionScopeOption.Required, new TransactionMethod<Void>() { @Override public Void runInTransaction() { saveNewOptions(existingVolume, newOptionsSortedList); return null; } }); } if (!existingOptionsSortedList.isEmpty()) { TransactionSupport.executeInScope(TransactionScopeOption.Required, new TransactionMethod<Void>() { @Override public Void runInTransaction() { updateExistingOptions(existingVolume, existingOptionsSortedList); return null; } }); } }