Question: How can I use Comparator while using an Iterator? (Java) For example: Consider I created a collection (not any of the defined ones in existing

How can I use Comparator while using an Iterator? (Java)

For example:

Consider I created a collection (not any of the defined ones in existing java libraries like, java.util.ArrayList, java.util.List, etc..). it's known as a SortedArrayCollection col; It holds Book objects, and I want to sort them by "title". Also, I have created a method that returns an Iterator within this collection.

Now consider I created a class that implements the Comparator interface and sorts them by title, lets call that TitleComparator.

In my test Driver lets say that I used an Iterator to iterate

Iterator it = col.iterate();

How do I sort with the comparator? Without using collections.sort() or Arrays.sort(),

How can I use Comparator while using an Iterator? (Java) For example:

(2796) 12:55 PM Eile Edit View Navigate Source Refactor Bun Debug Profile Team Iools Window Help Search (CErt+ p9 C" it = list.iterate() ; while(it.hasNext()) +-, Docker + Task Repositories + Selenium Server System.out.println(it.next()) Thread.sleep (50G) 68 public void displayAuthor() throws InterruptedException 70 lterator Book> it = list.iterate() ; while(it.hasNext()) Navigator x Members 72 73 74 System.out.println(it.next()) Thread.sleep (50G) Bestsellers Bestsellers(SortedArrayCollectionBook boc o addBooks0 O displayAscendingDates0 O displayAuthor0 O displayByPublisher O displayByTitle0 O displayDescendingDates0 O displayFiction0 O displayMonthandYear(int month, int year) O displayNonFiction Output 9:27 INS

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!