Class FavoritesList<E>

java.lang.Object
com.zybooks.dsaj.list.FavoritesList<E>
Type Parameters:
E - The element type
Direct Known Subclasses:
FavoritesListMTF

public class FavoritesList<E> extends Object
A list of elements, ordered from most frequently to least frequently accessed.
  • Constructor Details

    • FavoritesList

      public FavoritesList()
      Constructs initially empty favorites list.
  • Method Details

    • value

      protected E value(Position<FavoritesList.Item<E>> p)
      Provides shorthand notation to retrieve user's element stored at Position p.
    • count

      protected int count(Position<FavoritesList.Item<E>> p)
      Provides shorthand notation to retrieve count of item stored at Position p.
    • findPosition

      protected Position<FavoritesList.Item<E>> findPosition(E e)
      Returns Position having element equal to e (or null if not found).
    • moveUp

      protected void moveUp(Position<FavoritesList.Item<E>> p)
      Moves item at Position p earlier in the list based on access count.
    • size

      public int size()
      Returns the number of items in the favorites list.
    • isEmpty

      public boolean isEmpty()
      Returns true if the favorites list is empty.
    • access

      public void access(E e)
      Accesses element e, thereby increasing its access count. If e is new, its count will be 1 after this operation.
    • remove

      public void remove(E e)
      Removes element equal to e from the list of favorites (if found).
    • getFavorites

      public Iterable<E> getFavorites(int k) throws IllegalArgumentException
      Returns an iterable collection of the k most frequently accessed elements.
      Throws:
      IllegalArgumentException
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • test

      protected static void test(FavoritesList<Character> fav)
    • main

      public static void main(String[] args)