Recipe 5.7. Retrieving All Instances of a Specific Item in a List<T>ProblemYou need to retrieve every object contained in a List<T> that matches a search criterion. The List<T> contains the BinarySearch method to find a single itemessentially, there is no find all functionality. If you want to find all items duplicated in a List<T>, you must write your own routine. SolutionUse the ListEx<T> class shown in Example 5-3, which inherits from the List<T> class in order to extend its functionality. Two methodsGetAll and BinarySearchGetAllare added to return an array of all the matching objects found in this sorted or unsorted List<T>. Example 5-3. Retrieving all instances of a specific item in a List<T>
DiscussionThe GetAll and BinarySearchGetAll methods used in this recipe are very similar to those used in Recipe 5.6. The main difference is that these methods return the actual items found in an object array instead of a count of the number of times an item was found. The main thing to keep in mind when choosing a method is whether you are going to be searching a List<T> that is sorted. Choose the GetAll method to obtain an array of all found items from an unsorted List<T>, and choose the BinarySearchGetAll method to get all items in a sorted List<T>. The following code exercises these two new methods of the ListEx<T> class: class Test { static void Main( ) { ListEx<int> arrayExt = new ListEx<int>( ); arrayExt.Add(-1); arrayExt.Add(-1); arrayExt.Add(1); arrayExt.Add(2); arrayExt.Add(2); arrayExt.Add(2); arrayExt.Add(2); arrayExt.Add(3); arrayExt.Add(100); arrayExt.Add(4); arrayExt.Add(5); Console.WriteLine("--GET All--"); int[] objects = arrayExt.GetAll(2); foreach (object o in objects) { Console.WriteLine("obj2: " + o); } Console.WriteLine( ); objects = arrayExt.GetAll(-2); foreach (object o in objects) { Console.WriteLine("obj-2: " + o); } Console.WriteLine( ); objects = arrayExt.GetAll(5); foreach (object o in objects) { Console.WriteLine("obj5: " + o); } Console.WriteLine("\r\n--BINARY SEARCH GET ALL--"); arrayExt.Sort( ); objects = arrayExt.BinarySearchGetAll(-2); foreach (object o in objects) { Console.WriteLine("obj-2: " + o); } Console.WriteLine( ); objects = arrayExt.BinarySearchGetAll(2); foreach (object o in objects) { Console.WriteLine("obj2: " + o); } Console.WriteLine( ); objects = arrayExt.BinarySearchGetAll(5); foreach (object o in objects) { Console.WriteLine("obj5: " + o); } } } This code outputs the following: --GET All-- obj2: 2 obj2: 2 obj2: 2 obj2: 2 obj5: 5 --BINARY SEARCH GET ALL-- obj2: 2 obj2: 2 obj2: 2 obj2: 2 obj5: 5 The BinarySearchGetAll method is faster than the GetAll method, especially if the array has already been sorted. If a BinarySearch is used on an unsorted List<T>, it is highly likely that the results returned by the search will be incorrect. See AlsoSee Recipe 5.6; see the "List<T> Class" topic in the MSDN documentation. |