310 chapter 9 collection classes both hashsett and

Info icon This preview shows pages 334–337. Sign up to view the full content.

View Full Document Right Arrow Icon
310 | Chapter 9: Collection Classes
Image of page 334

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Both HashSet<T> and SortedSet<T> offer various handy set-based methods. You can determine whether an IEnumerable<T> is a subset of (i.e., all its elements are also found in) a set with the IsSubsetOf , for example. The available methods are defined by the common ISet<T> interface, reproduced in Example 9-12 . Example 9-12. ISet<T> namespace System.Collections.Generic { public interface ISet<T> : ICollection<T>, IEnumerable<T>, IEnumerable { bool Add(T item); void ExceptWith(IEnumerable<T> other); void IntersectWith(IEnumerable<T> other); bool IsProperSubsetOf(IEnumerable<T> other); bool IsProperSupersetOf(IEnumerable<T> other); bool IsSubsetOf(IEnumerable<T> other); bool IsSupersetOf(IEnumerable<T> other); bool Overlaps(IEnumerable<T> other); bool SetEquals(IEnumerable<T> other); void SymmetricExceptWith(IEnumerable<T> other); void UnionWith(IEnumerable<T> other); } } Queues Queue<T> is a handy collection type for processing entities on a first come, first served basis. For example, some doctors’ general practice surgeries operate an appointment- free system. Since the time taken to see each patient in these scenarios can vary wildly depending on the problem at hand, seeing patients in turn can end up being a lot more efficient than allocating fixed-length appointment slots. We could model this with a Queue<Patient> (where Patient is some class defined by our application). When a patient arrives, she would be added to a queue by calling its Enqueue method: private Queue<Patient> waitingPatients = new Queue<Patient>(); ... public void AddPatientToQueue(Patient newlyArrivedPatient) { waitingPatients.Enqueue(newlyArrivedPatient); } When a doctor has finished seeing one patient and is ready to see the next, the Dequeue method will return the patient who has been in the queue longest, and will then remove that patient from the queue: Patient nextPatientToSee = waitingPatients.Dequeue(); Queues | 311
Image of page 335
While this example perfectly matches how Queue<T> works, you prob- ably wouldn’t use a Queue<T> here in practice. You’d want to handle crashes and power failures gracefully in this application, which means that in practice, you’d probably store the list of waiting patients in a database, along with something such as a ticket number indicating their place in the queue. In-memory queues tend to show up more often in multithreaded servers for keeping track of outstanding work. But since we haven’t gotten to either the networking or the threading chapters yet, an example along those lines would be premature. Queue<T> implements IEnumerable<T> , so you can use LINQ queries across items in the whole queue. It also implements ICollection<T> , so you can discover whether the queue is currently empty by inspecting its Count property. Queue<T> operates in strict first in, first out (FIFO) order, which is to say that Dequeue will return items in exactly the same order in which they were added with Enqueue . That might be fine for general practice, but it wouldn’t work so well for the emergency room.
Image of page 336

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 337
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern