Tuesday, July 5, 2016

Working with HashSet in c-sharp

A HashSet is an unordered collection of unique elements.It was introduced in .net 3.5 and is found in System.Collections.Generic namespace.It is used in a situation where we want to prevent duplicates from being inserted in collection. Performancewise it is better in comparison to List.In this article i will begin by starting with creating a simple HashSet,hen will continue to perform various operations on HashSet.At the end of article ,i will show how to create a HashSet of Custom type and how to prevent duplicates from being inserted in HashSet. So lets begin our example.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSetDemo { class Program { static void Main(string[] args) { HashSet<string> names = new HashSet<string> { "Rajeev", "Akash", "Amit" }; foreach (var name in names) { Console.WriteLine(name); } Console.ReadKey(); } } }


In the above code we are creating a simple HashSet of string type and adding strings to it.We can also add string using Add Method .We will see how we can use Add method in the below snippet.We will now try to add the duplicate string and see what happens.
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSetDemo { class Program { static void Main(string[] args) { HashSet<string> names = new HashSet<string> { "Rajeev", "Akash", "Amit" }; names.Add("Rajeev"); //duplicates are not added into collection. foreach (var name in names) { Console.WriteLine(name); } Console.ReadKey(); } } }


In he above snippet even though we try to add a duplicate string,we will not get any error but when we iterate he collection we could not find the string.This shows that we cannot add duplicate elements to a HashSet. Now we will look into some of the important methods of HashSet.
1)UnionWith:-This method combines the elements present in both the collections into collection on which it is called.

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSetDemo { class Program { static void Main(string[] args) { HashSet<string> names = new HashSet<string> { "Rajeev", "Akash", "Amit" }; HashSet<string> names1 = new HashSet<string> { "Rajeev", "Akash", "Amit", "Deepak", "Mohit" }; names.UnionWith(names1); foreach (var name in names) { Console.WriteLine(name); } Console.ReadKey(); } } }


2)IntersectWith:-This method combines the elements that are common to both collections.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSetDemo { class Program { static void Main(string[] args) { HashSet<string> names = new HashSet<string> { "Rajeev", "Akash", "Amit" }; HashSet<string> names1 = new HashSet<string> { "Rajeev", "Akash", "Amit", "Deepak", "Mohit" }; names.IntersectWith(names1); foreach (var name in names) { Console.WriteLine(name); } Console.ReadKey(); } } }



2)ExceptWith:-This method removes all the elements that are present in other collections from the collection on which it is called.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSetDemo { class Program { static void Main(string[] args) { HashSet<string> names = new HashSet<string> { "Rajeev", "Akash", "Amit" }; HashSet<string> names1 = new HashSet<string> { "Rajeev", "Akash", "Amit", "Deepak", "Mohit" }; names1.ExceptWith(names); foreach (var name in names1) { Console.WriteLine(name); } Console.ReadKey(); } } }


Now lets go a step further and create a class and try to create a HashSet of class type and add try to add duplicates to it.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSetDemo { class Program { static void Main(string[] args) { Console.WriteLine("-----Custom HashSet With Duplicates----"); HashSet<Employee> employees = new HashSet<Employee> { {new Employee{Emp_Id=1,Emp_name="Rajeev",Dept_name="IT"}}, {new Employee{Emp_Id=1,Emp_name="Rajeev",Dept_name="IT"}}, {new Employee{Emp_Id=3,Emp_name="Akash",Dept_name="IT"}}, {new Employee{Emp_Id=4,Emp_name="Amit",Dept_name="IT"}} }; Console.WriteLine("{0,-6}{1,10}{2,-8}", "Emp_Id", "Emp_name", "Dept_name"); Console.WriteLine("=============================="); foreach (var employee in employees) { Console.WriteLine("{0,-8}{1,-10}{2,5}", employee.Emp_Id, employee.Emp_name, employee.Dept_name); } Console.WriteLine("=============================="); Console.ReadKey(); } } public class Employee { public int Emp_Id { get; set; } public string Emp_name { get; set; } public string Dept_name { get; set; } } }


We know that HashSet will not allow duplicates to collection but still in out output we are having duplicate records.To overcome this drawback we need to implement IEquatable interface and override Equals and GetHashCode methods.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HashSetDemo { class Program { static void Main(string[] args) { Console.WriteLine("-----Custom HashSet With Duplicates----"); HashSet<Employee> employees = new HashSet<Employee> { {new Employee{Emp_Id=1,Emp_name="Rajeev",Dept_name="IT"}}, {new Employee{Emp_Id=1,Emp_name="Rajeev",Dept_name="IT"}}, {new Employee{Emp_Id=3,Emp_name="Akash",Dept_name="IT"}}, {new Employee{Emp_Id=4,Emp_name="Amit",Dept_name="IT"}} }; Console.WriteLine("{0,-6}{1,10}{2,-8}", "Emp_Id", "Emp_name", "Dept_name"); Console.WriteLine("=============================="); foreach (var employee in employees) { Console.WriteLine("{0,-8}{1,-10}{2,5}", employee.Emp_Id, employee.Emp_name, employee.Dept_name); } Console.WriteLine("=============================="); Console.ReadKey(); } } public class Employee : IEquatable<Employee> { public int Emp_Id { get; set; } public string Emp_name { get; set; } public string Dept_name { get; set; } public bool Equals(Employee other) { return this.Emp_Id.Equals(other.Emp_Id); } public override int GetHashCode() { return this.Emp_Id.GetHashCode(); } } }


So HashSet is a generic collection that does not allow duplicates.We can use HashSet to remove duplicates from any collection like List using HashSet.

Reading & Writing File in NodeJs

There are 2 ways to read files in node.js
1)Read synchronously:Used in the cases where file need to be read before any further processing . eg-any config file.
2)Read Asynchronously(Default):-read file in a separate thread. 


Read AsyncSynchronously
First we need to include file system object using require. Now we will call readFile method of file system.This has 2 parameters 

1)location of the file. 
2) callback function :This will notify us when we successfully read it.
console.log("--------Async Reading------------------"); console.log("Started Reading File..."); console.log("Reading File Asynchronously"); var content=fs.readFile('read.js',function(error,data) { if(error) { console.log(error); } else { console.log("Content \n "+data); } } ); console.log("-------- End Async Reading-------------");
First we need to include file system object using require. Now we will call readFileSync method of file system.This has one parameter -location of the file.
var fs=require('fs'); console.log("--------Synchronously Reading------------------"); console.log("Started Reading File..."); console.log("Reading File Synchronously"); var content=fs.readFileSync('read.js'); console.log("Content Printed First:-- \n "+content); console.log("--------End Synchronously Reading--------------");