title: Avoiding code duplication for methods with different return types
body: <p>I have two builder classes that return a <code>Tree</code> and a <code>Set</code> respectively. The execution of the second builder is just the execution of the first builder with an extra step and a different return type. I really don't want to duplicate the same steps in two different classes, because if there is a change in the <code>Tree</code>-building process, I have to change code in two classes. Here are the two builders:</p>
  
  <pre><code>public class TreeBuilder {
  
      private NodeHelper nodeHelper;
  
      public TreeBuilder(NodeHelper nodeHelper) {
          this.nodeHelper = nodeHelper;
      }
  
      public Tree&lt;EquipmentDetails&gt; buildTree() {
  
          Tree&lt;EquipmentDetails&gt; tree = new Tree&lt;&gt;(nodeHelper.getEquipmentDetails(nodeHelper.getModelName()));
  
          return buildTree(tree);
      }
  
      private Tree&lt;EquipmentDetails&gt; buildTree(Tree&lt;EquipmentDetails&gt; parentTree) {
  
          Map&lt;String, NativeSlotDisplayLabel&gt; childSubTypeMap = nodeHelper.getChildSubTypeMap(parentTree);
  
          if (CommonUtils.nullOrEmpty(childSubTypeMap))
              return parentTree;
  
          for (String holderCommonName : childSubTypeMap.keySet()) {
  
              NativeSlotDisplayLabel nativeSlotDisplayLabel = childSubTypeMap.get(holderCommonName);
  
              EquipmentDetails equipmentDetails = nodeHelper.constructEqptDetails(holderCommonName, nativeSlotDisplayLabel, parentTree);
  
              Tree&lt;EquipmentDetails&gt; childTree = new Tree&lt;&gt;(equipmentDetails);
  
              parentTree.addChild(childTree);
  
              String equipmentCommonName = equipmentDetails.getModelEquipType();
  
              if (equipmentCommonName == null)
                  continue;
  
              buildTree(childTree);
          }
  
          return parentTree;
      }
  }
  </code></pre>
  
  <p>The second builder uses one more dependency and returns a <code>Set</code> instead of a <code>Tree</code>:</p>
  
  <pre><code>public class FreePortsBuilder {
  
      private NodeHelper nodeHelper;
  
      private PortLabelGenerator portLabelGenerator;
  
      public FreePortsBuilder(NodeHelper nodeHelper, PortLabelGenerator portLabelGenerator) {
          this.nodeHelper = nodeHelper;
          this.portLabelGenerator = portLabelGenerator;
      }
  
      public Set&lt;PortNameAndLabel&gt; collectFreePorts(){
  
          Tree&lt;EquipmentDetails&gt; tree = new Tree&lt;&gt;(nodeHelper.getEquipmentDetails(nodeHelper.getModelName()));
  
          Set&lt;PortNameAndLabel&gt; portNameAndLabels = new HashSet&lt;&gt;();
  
          return collectFreePorts(tree, portNameAndLabels);
      }
  
      private Set&lt;PortNameAndLabel&gt; collectFreePorts(Tree&lt;EquipmentDetails&gt; parentTree, Set&lt;PortNameAndLabel&gt; portNameAndLabels) {
  
          Map&lt;String, NativeSlotDisplayLabel&gt; childSubTypeMap = nodeHelper.getChildSubTypeMap(parentTree);
  
          if (CommonUtils.nullOrEmpty(childSubTypeMap))
              return portNameAndLabels;
  
          Set&lt;PortNameAndLabel&gt; generatedPortNameAndLabel = portLabelGenerator.getPortNameAndLabel(childSubTypeMap, parentTree.getUserObject());
  
          portNameAndLabels.addAll(generatedPortNameAndLabel);
  
          for (String holderCommonName : childSubTypeMap.keySet()) {
  
              NativeSlotDisplayLabel nativeSlotDisplayLabel = childSubTypeMap.get(holderCommonName);
  
              EquipmentDetails equipmentDetails = nodeHelper.constructEqptDetails(holderCommonName, nativeSlotDisplayLabel, parentTree);
  
              Tree&lt;EquipmentDetails&gt; childTree = new Tree&lt;&gt;(equipmentDetails);
  
              parentTree.addChild(childTree);
  
              String equipmentCommonName = equipmentDetails.getModelEquipType();
  
              if (equipmentCommonName == null)
                  continue;
  
              collectFreePorts(childTree, portNameAndLabels);
          }
  
          return portNameAndLabels;
      }
  }
  </code></pre>
  
  <p>I am looking for a way to avoid the duplication. Any suggestions?</p>