Quick Navigate

August 09, 2006  ·  3 min read

Find and FindAll in generic collections

In .NET 2.0, searching through generic collections such as Arrays and Lists are easier with the new Find and FindAll methods. Both methods accepts a Predicate parameter, which is a delegate to a method. According to MSDN, these methods run in O(n). In situations like searching for item(s) in a collection, I used to write utility functions with simple bubble sort algorithm. This causes code duplication and O(n^2) is just too slow.

Therefore, I wrote couple of test cases to explore these two new methods. Check it out!

TestFixtureSetUp

[TestFixture]
public class TestFindInICollection
{
private List _numbers;
private const int TOTAL_NUMBER = 20;
private const int MAX = 100;
private DomainModel[] _models;

[TestFixtureSetUp]
public void TestFixtureSetUp()
{
_numbers = new List();
_models = new DomainModel[TOTAL_NUMBER];

Random r = new Random();
Console.WriteLine("Orignal Collection");
Console.WriteLine("Size = " + TOTAL_NUMBER);

//populate the number list with pseudo-random numbers
//populate the model list
for (int i = 0; i < TOTAL_NUMBER; i++)
{
int radNum = r.Next(MAX);
_numbers.Add(radNum);
Console.WriteLine(radNum);
_models[i] = new DomainModel(i, DateTime.Now.ToLongTimeString());
}
}

Find numbers smaller then 50 using anonymous delegate

[Test]
public void CanFindAllNumbersUsingAnonymousMethod()
{
//_number is prepopulated with 20 random numbers from 0 to 50
//find numbers smaller then 50
List smallerPartition = _numbers.FindAll(delegate(int testInt) { return testInt < 50; });
Console.WriteLine("Numbers smaller then 50");
Console.WriteLine("Size = " + smallerPartition.Count);

foreach (int n in smallerPartition)
{
Assert.IsTrue(n < 50);
Console.WriteLine(n);
}
}


Find the domain model with id equals to 10

[Test]
public void CanFindModelUsingAnonymousMethod()
{
int id = 10;
DomainModel model = Array.Find(_models, delegate(DomainModel testModel) { return testModel.Id == id; });
Assert.AreEqual(10, model.Id);
Console.WriteLine(model);
}

Find numbers larger then 50 by calling a helper function that returns a Predicate

[Test]
public void CanFindAllNumbersUsingFunction()
{
//find numbers bigger then 50
List biggerPartition = _numbers.FindAll(BiggerThen(50));
Console.WriteLine("Numbers bigger then 50");
Console.WriteLine("Size = " + biggerPartition.Count);

foreach (int n in biggerPartition)
{
Assert.IsTrue(n > 50);
Console.WriteLine(n);
}
}

private Predicate BiggerThen(int p)
{
return delegate(int testInt) { return testInt > p; };
}


Simple DomainModel Class

class DomainModel
{
private int _id;
private string _label;

public DomainModel(int id, string label)
{
_id = id;
_label = label;
}

public int Id
{
get { return _id; }
set { _id = value; }
}


public string Label
{
get { return _label; }
set { _label = value; }
}

public override string ToString()
{
return _id + " " + _label;
}
}
Picture of Weijie Lin

Thanks for reading!

Follow me on Twitter to stay up to date on my latest posts