// Textbook fragment 06.20 /** Returns the k most accessed elements, for a given k; O(k) time */ public Iterable top(int k) { if (k < 0 || k > size()) throw new IllegalArgumentException("Invalid argument"); PositionList T = new NodePositionList(); // top-k list int i = 0; // counter of the entries added to the list for (Entry e: fList) { if (i++ >= k) break; // all the k entries have been added T.addLast(e.value()); // add one entry to the list } return T; } /** String representation of the favorite list */ public String toString() { return fList.toString(); } /** Helper method that extracts the value of the entry at a given position */ protected E value(Position> p) { return ( p.element()).value(); } /** Helper method that extracts the counter of the entry at a given position * / protected int count(Position> p) { return ( p.element()).count(); }