HashSet<T> Constructor (IEnumerable<T>^, IEqualityComparer<T>^)
Initializes a new instance of the HashSet<T> class that uses the specified equality comparer for the set type, contains elements copied from the specified collection, and has sufficient capacity to accommodate the number of elements copied.
Assembly: System.Core (in System.Core.dll)
Parameters
- collection
-
Type:
System.Collections.Generic::IEnumerable<T>^
The collection whose elements are copied to the new set.
- comparer
-
Type:
System.Collections.Generic::IEqualityComparer<T>^
The IEqualityComparer<T> implementation to use when comparing values in the set, or null to use the default EqualityComparer<T> implementation for the set type.
| Exception | Condition |
|---|---|
| ArgumentNullException | collection is null. |
The capacity of a HashSet<T> object is the number of elements that the object can hold. A HashSet<T> object's capacity automatically increases as elements are added to the object.
If collection contains duplicates, the set will contain one of each unique element. No exception will be thrown. Therefore, the size of the resulting set is not identical to the size of collection.
This constructor is an O(n) operation, where n is the number of elements in the collection parameter.
The following example uses a supplied IEqualityComparer<T> to allow case-insensitive comparisons on the elements of a HashSet<T> collection of vehicle types.
#using <System.Core.dll> using namespace System; using namespace System::Collections::Generic; ref class SameVehicleComparer : public EqualityComparer<String^> { public: virtual bool Equals(String^ s1, String^ s2) override { return s1->Equals(s2, StringComparison::CurrentCultureIgnoreCase); } virtual int GetHashCode(String^ s) override { return this->GetHashCode(); } }; ref class Program { public: static void Main() { SameVehicleComparer^ compareVehicles = gcnew SameVehicleComparer(); HashSet<String^> ^allVehicles = gcnew HashSet<String^>(compareVehicles); List<String^>^ someVehicles = gcnew List<String^>(); someVehicles->Add("Planes"); someVehicles->Add("Trains"); someVehicles->Add("Automobiles"); // Add in the vehicles contained in the someVehicles list. allVehicles->UnionWith(someVehicles); Console::WriteLine("The current HashSet contains:\n"); for each (String^ vehicle in allVehicles) { Console::WriteLine(vehicle); } allVehicles->Add("Ships"); allVehicles->Add("Motorcycles"); allVehicles->Add("Rockets"); allVehicles->Add("Helicopters"); allVehicles->Add("Submarines"); Console::WriteLine("\nThe updated HashSet contains:\n"); for each (String^ vehicle in allVehicles) { Console::WriteLine(vehicle); } // Verify that the 'All Vehicles' set contains at least the vehicles in // the 'Some Vehicles' list. if (allVehicles->IsSupersetOf(someVehicles)) { Console::Write("\nThe 'All' vehicles set contains everything in "); Console::WriteLine("'Some' vechicles list."); } // Check for Rockets. Here the SameVehicleComparer will compare // true for the mixed-case vehicle type. if (allVehicles->Contains("roCKeTs")) { Console::WriteLine("\nThe 'All' vehicles set contains 'roCKeTs'"); } allVehicles->ExceptWith(someVehicles); Console::WriteLine("\nThe excepted HashSet contains:\n"); for each (String^ vehicle in allVehicles) { Console::WriteLine(vehicle); } // Remove all the vehicles that are not 'super cool'. allVehicles->RemoveWhere(gcnew Predicate<String^>(&isNotSuperCool)); Console::WriteLine("\nThe super cool vehicles are:\n"); for each (String^ vehicle in allVehicles) { Console::WriteLine(vehicle); } } private: // Predicate to determine vehicle 'coolness'. static bool isNotSuperCool(String^ vehicle) { bool superCool = (vehicle == "Helicopters") || (vehicle == "Motorcycles"); return !superCool; } }; int main() { Program::Main(); } // The program writes the following output to the console:: // // The current HashSet contains: // // Planes // Trains // Automobiles // // The updated HashSet contains: // // Planes // Trains // Automobiles // Ships // Motorcycles // Rockets // Helicopters // Submarines // // The 'All' vehicles set contains everything in 'Some' vechicles list. // // The 'All' vehicles set contains 'roCKeTs' // // The excepted HashSet contains: // // Ships // Motorcycles // Rockets // Helicopters // Submarines // // The super cool vehicles are: // // Motorcycles // Helicopters
Available since 8
.NET Framework
Available since 3.5
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 4.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1