次の機能を実装する必要があります。私は最高のクラスを使用することにオープンですが、SortedSet と TreeSet のどちらを使用すべきかわかりません (Set myEmailsAscending = new TreeSet(new DateAscendingComparator()); // メールは常に昇順です)。
public void addEmail(Email email)//The prototype isn't to be altered.
{
Comparator comp;
if(getCurrentSortingMethod()=="DateDescending") comp=DateDescendingComparator;
else if(getCurrentSortingMethod()=="DateAscending") comp=DateAscendingComparator;
...//other comparators
int index = Collections.binarySearch(getEmails(), email, new comp());// Search where to insert according to the current sorting method.
getEmails().add(-index-1, email);}// Add the item to the list
}
それはコンパレータを選択するための適切な構文ですか? 複数の Comparator クラスを作成したくないので、これを行う方法はありますか?