Take element of a Set collection depending on the value of another element I had to populate the fields of a Service object using a Set (HashSet) with any configuration parameters -&gt; ConfigurationMap (Class with 2 attributes: key and value).
The problem is that the attribute quantity of Service object can be set from two distinct parameters (&quot;service_quantity&quot; or &quot;publications_amount&quot;) based on the value of another parameter(&quot;type&quot;).
I've gotten it to work with the code below, but I think it's pretty messy and inefficient.
Is there any way to make it more readable considering that the methods signatures are invariable?
@Override
protected void populateServiceConfigurationData(Service theService, Set&lt;ConfigurationMap&gt; configurationParams) {
    boolean publishService = false;
    for (ConfigurationMap configParam : configurationParams) {
        String paramValue = configParam.getValue();
        String paramKey = configParam.getKey();
        setFieldValue(theService, paramKey, paramValue);
        if(&quot;type&quot;.equals(paramKey) &amp;&amp; paramValue.equals(&quot;20&quot;)) {
            publishService = true;
        }
    }

    if(publishService) {
        for (ConfigurationMap configParam : configurationParams) {
            String paramKey = configParam.getKey();
            if(paramKey.equals(&quot;publications_amount&quot;)) {
                String paramValue = configParam.getValue();
                setFieldValue(theService, paramKey, paramValue);
            }
        }
    }
}

@Override
public void setFieldValue(Service service, String paramKey, String paramValue) {
    if (&quot;type&quot;.equals(paramKey)) {
        service.setType(Integer.valueOf(paramValue));
    }
    if (&quot;service_quantity&quot;.equals(paramKey) || &quot;publications_amount&quot;.equals(paramKey)) {
        service.setQuantity(Integer.valueOf(paramValue));
    }
    if (&quot;gateway_id&quot;.equals(paramKey)) {
        service.setGatewayId(paramValue);
    }
    if (&quot;comissions&quot;.equals(paramKey)) {
        service.setCommissions(paramValue);
    }
}
I think it's pretty messy and inefficient

Let's say that it is getting messy, especially if the number of parameters (and hidden rules) keeps growing.
About being inefficient, I am not sure. Even if the input is very large, the method populateServiceConfigurationData runs in \$O(N)\$, so I don't think it will be a big problem.
My suggestions:

Readability: setting the quantity of the service depends on more than one parameter. So the &quot;rule&quot; to set quantity would be more evident if included in a single function, like setServiceQuantity.
Performance: the input set can be converted to a map, to easily access the parameters. The performance gain is minimal, but it should help to make the code more clear.
Design: using setFieldValue in populateServiceConfigurationData seems unnecessary. The method populateServiceConfigurationData can be completely independent to setFieldValue. More on this later.

public void populateServiceConfigurationData(Service service, Set&lt;ConfigurationMap&gt; configurationParams) {
    // Convert the input set to a map
    Map&lt;String, String&gt; params = configurationParams.stream()
            .collect(Collectors.toMap(ConfigurationMap::getKey, ConfigurationMap::getValue));

    if (params.containsKey(&quot;type&quot;)) {
        service.setType(Integer.valueOf(params.get(&quot;type&quot;)));
    }

    if (params.containsKey(&quot;gateway_id&quot;)) {
        service.setGatewayId(params.get(&quot;gateway_id&quot;));
    }

    if (params.containsKey(&quot;comissions&quot;)) {
        service.setCommissions(params.get(&quot;comissions&quot;));
    }

    setServiceQuantity(service, params);
}

private void setServiceQuantity(Service service, Map&lt;String, String&gt; params){
    boolean isPublishService = params.get(&quot;type&quot;).equals(&quot;20&quot;);
    String quantity = isPublishService ? params.get(&quot;publications_amount&quot;) : params.get(&quot;service_quantity&quot;);
    service.setQuantity(Integer.valueOf(quantity));
}

(Note: not tested, it's just to give an idea)
Now the logic for setting each property of the service is clear and can be easily extracted into functions like setServiceQuantity.
One issue is that populateServiceConfigurationData needs to know how to set the properties into the service (for example, parsing type to Integer). If you don't like this approach, the setters can be replaced with setFieldValue (as before) or let service do the parsing in its own methods.
The method setFieldValue looks fine, the only thing I can suggest is to use else if since only one condition will match per invocation.