code: public static void limit (Collection<?> col, int limit)
      {
          int size = col.size();
          if (size > limit) {
              if (col instanceof List<?>) {
                  ((List<?>) col).subList(limit, size).clear();
  
              } else {
                  Iterator<?> itr = col.iterator();
                  for (int ii = 0; ii < limit; ii++) {
                      itr.next();
                  }
                  while (itr.hasNext()) {
                      itr.next();
                      itr.remove();
                  }
              }
          }
      }
docs: Modify the specified Collection so that only the first <code>limit</code> elements
  remain, as determined by iteration order. If the Collection is smaller than limit,
  it is unmodified.