HashSet<T> содержит неповторяющюся коллекцию элементов.
HashSet<T> содержит элементы неупорядоченно (без сортировки).
HashSet<T> позволяет быстро определить, есть такой элемент или нет (быстро потому что, использует индекс, который вычисляется из хэш-кода элемента).
HashSet<T> - имеет методы
Add,
Remove,
Contains, но поскольку он использует хэш-реализацию, эти операции занимают 1 действие (методы
Contains и
Remove в
List<T> занимает n-действий.)
HashSet<T> имеет методы:
UnionWith (
объединение элементов с другим HashSet<T>)
IntersectWith (
пересечение элементов с другим HashSet<T>)
ExceptWith (
разность элементов с другим HashSet<T>)
SymmetricExceptWith (
симетрическая разность элементов с другим HashSet<T>)
HashSet<T> относится к
типизированной коллекции... то есть все элементы одного типа.
C#
Создаем новое C# консольное приложение... и напишем код
using System;
using System.Collections.Generic; // подключаем HashSet<T>
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// создаем HashSet
HashSet<string> myHash = new HashSet<string>();
// добавляем элементы
myHash.Add("a");
myHash.Add("b");
myHash.Add("c");
// в myHash элементы: "a", "b", "c"
// Перебираем все элементы с помощью foreach и выводим на экран
foreach (string value in myHash)
Console.WriteLine(value);
// узнать есть ли такой элемент?
bool isFound = myHash.Contains("a");
// isFound = true
// удаляем элемент
myHash.Remove("a");
// в myHash элементы: "b", "c"
// удаляем элементы "b" и "c"
myHash.RemoveWhere(item => item=="b" || item=="c");
// в myHash пусто
// Метод ExceptWith (Разность)
HashSet<string> hashSet1 = new HashSet<string>() { "a", "b", "c"};
HashSet<string> hashSet2 = new HashSet<string>() { "a", "e", "h"};
hashSet1.ExceptWith(hashSet2);
// (ПОМЕНЯЛИСЬ) в hashSet1 элементы: "b", "c"
// в hashSet2 элементы: "a", "e", "h"
// Метод SymmetricExceptWith (Симетрическая разность)
hashSet1 = new HashSet<string>() { "a", "b", "c" };
hashSet2 = new HashSet<string>() { "a", "e", "h" };
hashSet1.SymmetricExceptWith(hashSet2);
// (ПОМЕНЯЛИСЬ) в hashSet1 элементы: "e", "b", "c", "h",
// в hashSet2 элементы: "a", "e", "h"
// Метод UnionWith (Объединение)
hashSet1 = new HashSet<string>() { "a", "b", "c" };
hashSet2 = new HashSet<string>() { "a", "e", "h" };
hashSet1.UnionWith(hashSet2);
// (ПОМЕНЯЛИСЬ) в hashSet1 элементы: "a", "b", "c", "e", "h",
// в hashSet2 элементы: "a", "e", "h"
// Метод IntersectWith (Пересечение)
hashSet1 = new HashSet<string>() { "a", "b", "c"};
hashSet2 = new HashSet<string>() { "a", "e", "h"};
hashSet1.IntersectWith(hashSet2);
// в hashSet1 элементы: "a"
// в hashSet2 элементы: "a", "e", "h"
// Метод Overlaps (есть ли Пересечение?)
hashSet1 = new HashSet<string>() { "a", "b", "c" };
hashSet2 = new HashSet<string>() { "a", "e", "h" };
bool flag = hashSet1.Overlaps(hashSet2);
// flag = true
// в hashSet1 элементы: "a", "b", "c"
// в hashSet2 элементы: "a", "e", "h"
// Метод IsProperSubsetOf (является ли hashSet1 строгим подмножеством параметра HashSet)
hashSet1 = new HashSet<string>() { "a", "b"};
flag = hashSet1.IsProperSubsetOf(new HashSet<string>() { "a"}); // flag = false
flag = hashSet1.IsProperSubsetOf(new HashSet<string>() { "a", "b"}); // flag = false
flag = hashSet1.IsProperSubsetOf(new HashSet<string>() { "a", "b", "c" }); // flag = TRUE
// Метод IsSubsetOf (является ли hashSet1 подмножеством параметра HashSet)
hashSet1 = new HashSet<string>() { "a", "b" };
flag = hashSet1.IsSubsetOf(new HashSet<string>() { "a" }); // flag = false
flag = hashSet1.IsSubsetOf(new HashSet<string>() { "a", "b" }); // flag = TRUE
flag = hashSet1.IsSubsetOf(new HashSet<string>() { "a", "b", "c" }); // flag = TRUE
// Метод IsProperSupersetOf (является ли параметр HashSet строгим подмножеством hashSet1)
hashSet1 = new HashSet<string>() { "a", "b" };
flag = hashSet1.IsProperSupersetOf(new HashSet<string>() { "a" }); // flag = TRUE
flag = hashSet1.IsProperSupersetOf(new HashSet<string>() { "a", "b" }); // flag = false
flag = hashSet1.IsProperSupersetOf(new HashSet<string>() { "a", "b", "c" }); // flag = false
// Метод IsSupersetOf (является ли параметр HashSet подмножеством hashSet1)
hashSet1 = new HashSet<string>() { "a", "b" };
flag = hashSet1.IsSupersetOf(new HashSet<string>() { "a" }); // flag = TRUE
flag = hashSet1.IsSupersetOf(new HashSet<string>() { "a", "b" }); // flag = TRUE
flag = hashSet1.IsSupersetOf(new HashSet<string>() { "a", "b", "c" }); // flag = false
}
}
}