code: private Collection<? extends String> recursiveCollect(String refId,
        Map<String, SortedSet<String>> relationKeyMap, int maxRecursion) {
      Set<String> list = new HashSet<>();
      if (maxRecursion > 0 && relationKeyMap.containsKey(refId)) {
        SortedSet<String> subList = relationKeyMap.get(refId);
        for (String subRefId : subList) {
          list.add(subRefId);
          list.addAll(
              recursiveCollect(subRefId, relationKeyMap, maxRecursion - 1));
        }
      }
      return list;
    }
docs: Recursive collect.
  
  @param refId the ref id
  @param relationKeyMap the relation key map
  @param maxRecursion the max recursion
  @return the collection<? extends string>