refactoring and optimization the class There is repetition here and would appreciate help in improving this code.  Suggestions? Here's the framework for it:

Class Author {
  private Integer id;

  public static List&lt;Book&gt; sortBooksAlphabetically( Author a ) {
    Integer id = a.id;
    if (id == null) { return null; }
    List&lt;Book&gt; list = getAllBooksForAuthor( id );
    // do some sorting and return list
  }

  public static List&lt;Book&gt; sortBooksReverseAlphabetically( Integer id ) {
    if (id == null ) { return null; }
    List&lt;Book&gt; list = getAllBooksForMusician( id );
    // do some sorting and return list
  }

  public static List&lt;Book&gt; sortBooksByPopularity( Integer id ) {
    if (id == null ) { return null; }
    List&lt;Book&gt; list = getAllBooksForAuthor( id );
    // do some sorting and return list
  }

  public static List&lt;Book&gt; getAllBooksForAuthor( Integer id ) {
     if (id == null ) throw new NullPointerException();
     ...
     return listOfBooks;
  }
}
Tentative answer...

First of all, it looks like you didn't include the source of the full Author class. Where is the list of Books for that author, for instance?

Second: you seem to want methods to sort this inner list of books for a given author; why, then, pass the id of the author as an argument? You should have a lookup method for an Author given its id -- but this lookup method should not be a method of one Author instance; at worst, it should be a static method in the Author class. What is your backend like? How do you look up an author by its id eventually?

Third: the JDK has Collections.sort(); and given that you want to have a sorted list of books according to several criteria, you want to write several Comparators for that book. See below for a solution to that.

Fourth, and not the least: you should not operate on the list in the Author class itself, but on a copy of that list.

Given these four points above, here are some code extracts...

First: how to return a List&lt;Book&gt; from an author -- don't return the list itself but at least an unmodifiable version of it:

// in class Author; it is supposed that the list has name "bookList" in the class
public List&lt;Book&gt; getBookList()
{
    return new Collections.unmodifiableList(bookList); // not modifiable!
}


Second: you want book lists sorted in various ways; one solution is to create an enum:

public enum BookSort
{
    BY_NAME (new Comparator&lt;Book&gt;()
    {
        @Override
        public int compare(final Book a, final Book b)
        {
            return a.getName().compareTo(b.getName());
        }
    }),
    // other values

    private final Comparator&lt;Book&gt; comparator;

    BookSort(final Comparator&lt;Book&gt; comparator)
    {
        this.comparator = comparator;
    }

    // Return a sorted copy of books
    public List&lt;Book&gt; sort(final List&lt;Book&gt; list)
    {
        final List&lt;Book&gt; ret = new ArrayList&lt;Book&gt;(list);
        Collections.sort(ret, comparator);
        return ret;
    }
}


Then, in your code:

final List&lt;Book&gt; sorted = BookSort.BY_NAME.sort(original);


But all this code is moot to begin with if you do not provide the full code of both the Author and Book class; along with the existing mechanism to grab instances of these two classes.