Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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(),

image text in transcribed

(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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application And Administration

Authors: Michael Mannino, Michael V. Mannino

2nd Edition

0072880678, 9780072880670

More Books

Students also viewed these Databases questions