A few days ago I found a new collection HashSet
I am personally waited for this since I first saw it in JAVA 1.4.2.
HashSet is a combination betwin a Set and A Dictionary
It has some new cool methods like UnionWith, IntersectWith.
Small (and simple) Exmaple:
theSet1.Add(1); // Return true
// theSet1 contains 1,2
HashSet<int> theSet2 = new HashSet<int>();
theSet2.Add(1); // Return true
theSet2.Add(3); // Return true
theSet2.Add(4); // Return true
// theSet2 contains 1,3,4
theSet1.UnionWith(theSet2);
// theSet1 contains 1,2,3,4
theSet1.IntersectWith(theSet2);
// theSet1 contains 2
- Unlike ICollection
.Add : void In HashSet.Add : bool
class OddEvenComparer : IEqualityComparer
public OddEvenComparer() {}
public bool Equals(int x, int y) {
}
public int GetHashCode(int x) {
}
}
// Now we will use the comperator
HashSet<int>
oddEvenSet.Add(1); // Return true
oddEvenSet.Add(3); // Return false (we allready have an odd number)
oddEvenSet.Add(4); // Return true
// oddEvenSet will now hold 1,4 (3 is equal to 1 accurding to our OddEvenComparer)
For more information about HashSet
For Set definition from Wikipedia:
Enjoy
1 comment:
Great post...
Post a Comment