code: public void trim(int ntrees) {
          if (ntrees < 1) {
              throw new IllegalArgumentException("Invalid new model size: " + ntrees);
          }
  
          if (k == 2) {
              if (ntrees > trees.length) {
                  throw new IllegalArgumentException("The new model size is larger than the current size.");
              }
  
              if (ntrees < trees.length) {
                  trees = Arrays.copyOf(trees, ntrees);
                  this.ntrees = ntrees;
              }
          } else {
              if (ntrees > forest[0].length) {
                  throw new IllegalArgumentException("The new model size is larger than the current one.");
              }
  
              if (ntrees < forest[0].length) {
                  for (int i = 0; i < forest.length; i++) {
                      forest[i] = Arrays.copyOf(forest[i], ntrees);
                  }
                  this.ntrees = ntrees;
              }
          }
      }
docs: Trims the tree model set to a smaller size in case of over-fitting.
  Or if extra decision trees in the model don't improve the performance,
  we may remove them to reduce the model size and also improve the speed of
  prediction.
  
  @param ntrees the new (smaller) size of tree model set.