Note. This lesson is learned from Scott Allen C# Generic videos in Pluralsight.
HashSet type is supposed to identify duplicates and guarantee the uniqueness of stored objects, while the uniqueness/equality comparison shall be defined by the code.
HashSet has one construction that accepts a IEqualityComparer interface.
var departments = new SortedDictionary<string, HashSet<Employee>>();
Departments.Add("Sales", new HashSet<Employee>(new EmployeeComparer());
departments["Sales"].Add(new Employee{Name = "Joy" });
departments["Sales"].Add(new Employee{Name = "Joy" });
departments["Sales"].Add(new Employee{Name = "Joy" });
new SortedSet<Employee>(new EmployeeComparer());
public class EmployeeComparer : IEqualityComparer<Employee>, IComparer<Employee>
{
Equals ();
GetHashCode ();
}
1) Hashset (how to remove object duplicates)
HashSet type is supposed to identify duplicates and guarantee the uniqueness of stored objects, while the uniqueness/equality comparison shall be defined by the code.
HashSet has one construction that accepts a IEqualityComparer interface.
var departments = new SortedDictionary<string, HashSet<Employee>>();
Departments.Add("Sales", new HashSet<Employee>(new EmployeeComparer());
departments["Sales"].Add(new Employee{Name = "Joy" });
departments["Sales"].Add(new Employee{Name = "Joy" });
departments["Sales"].Add(new Employee{Name = "Joy" });
2) SortedSet (how to sort objects)
//SortedSet is similar, needs to implement IComparer<> interfacenew SortedSet<Employee>(new EmployeeComparer());
public class EmployeeComparer : IEqualityComparer<Employee>, IComparer<Employee>
{
Equals ();
GetHashCode ();
}
3) Cleaner code - hide those fussy "new", "generic" keywords by create a new class to inherit
namespace Employees
{
public class Employee
{
public string Name { get; set; }
}
public class EmployeeComparer : IEqualityComparer<Employee>, IComparer<Employee>
{
public bool Equals(Employee x, Employee y)
{
return string.Equals(x.Name, y.Name);
}
public int GetHashCode(Employee obj)
{
return obj.Name.GetHashCode();
}
public int Compare(Employee x, Employee y)
{
return string.Compare(x.Name, y.Name);
}
}
public class DepartmentCollection : SortedDictionary<string, HashSet<Employee>>
{
public DepartmentCollection Add(string departmentName, Employee employee)
{
if (!ContainsKey(departmentName)) {
this.Add(departmentName, new HashSet<Employee>(new EmployeeComparer()));
}
this[departmentName].Add(employee);
return this;
}
}
class Program
{
static void Main(string[] args)
{
var departments = new DepartmentCollection();
departments.Add("Sales", new Employee { Name = "Xi" })
.Add("Sales", new Employee { Name = "Xi" })
.Add("Sales", new Employee { Name = "Liu" });
departments.Add("Engineers", new Employee { Name = "Jin" })
.Add("Engineers", new Employee { Name = "Jin" })
.Add("Engineers", new Employee { Name = "Jin" });
foreach (var item in departments)
{
Console.WriteLine(item.Key);
foreach (var employee in item.Value)
{
Console.WriteLine("\t" + employee.Name);
}
}
Console.ReadLine();
}
}
}
No comments:
Post a Comment