C#에서 HashSet은 고유 요소의 순서가 지정되지 않은 컬렉션입니다. 이 컬렉션은 다음에서 소개됩니다. .NET 3.5 . 집합 구현을 지원하고 저장을 위해 해시 테이블을 사용합니다. 이 컬렉션은 일반 유형 컬렉션에 속하며 다음에서 정의됩니다. 시스템.컬렉션.일반 네임스페이스. 일반적으로 컬렉션에 중복된 요소가 배치되는 것을 방지하려는 경우에 사용됩니다. HashSet의 성능은 목록에 비해 훨씬 좋습니다.
C#의 HashSet 관련 중요 사항
- HashSet 클래스는 다음을 구현합니다. ICollection , IEnumerable , IReadOnlyCollection , 씨족 , IEnumerable , IDeserialization콜백 , 그리고 IS직렬화 가능 인터페이스.
- HashSet에서는 요소의 순서가 정의되지 않습니다. HashSet의 요소는 정렬할 수 없습니다.
- HashSet에서 요소는 고유해야 합니다.
- HashSet에서는 중복 요소가 허용되지 않습니다.
- 교집합, 합집합, 차이 등 다양한 수학적 집합 연산을 제공합니다.
- HashSet의 용량은 보유할 수 있는 요소의 수입니다.
- HashSet은 동적 컬렉션이므로 새 요소가 추가되면 HashSet의 크기가 자동으로 증가합니다.
- HashSet에서는 동일한 유형의 요소만 저장할 수 있습니다.
HashSet을 만드는 방법은 무엇입니까?
HashSet 클래스는 다음을 제공합니다. 7가지 유형의 생성자 HashSet을 생성하는 데 사용됩니다. 여기서는 해시세트() , 생성자. HashSet의 생성자에 대한 자세한 내용을 보려면 다음을 참조하세요. C# | HashSet 클래스 .
해시세트(): 이는 비어 있고 집합 유형에 대한 기본 동등 비교자를 사용하는 HashSet 클래스의 인스턴스를 만드는 데 사용됩니다.
문자열 대체 자바
1 단계: 포함하다 시스템.컬렉션.일반 프로그램의 네임스페이스를 사용하여 사용하여 예어:
using System.Collections.Generic;>
2 단계: 아래와 같이 HashSet 클래스를 사용하여 HashSet을 생성합니다.
HashSet Hashset_name = new HashSet();>
3단계: HashSet에 요소를 추가하려면 다음을 사용하십시오. 추가하다() HashSet에 요소를 추가하는 방법입니다. 또한 컬렉션 초기화 프로그램을 사용하여 HashSet에 요소를 저장할 수도 있습니다.
4단계: HashSet의 요소는 다음을 사용하여 액세스됩니다. 각각 고리. 아래 예와 같습니다.
예:
씨#
// C# program to illustrate how to> // create hashset> using> System;> using> System.Collections.Generic;> class> GFG {> >// Main Method> >static> public> void> Main()> >{> >// Creating HashSet> >// Using HashSet class> >HashSet<>string>>myhash1 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash1.Add(>'C'>);> >myhash1.Add(>'C++'>);> >myhash1.Add(>'C#'>);> >myhash1.Add(>'Java'>);> >myhash1.Add(>'Ruby'>);> >Console.WriteLine(>'Elements of myhash1:'>);> >// Accessing elements of HashSet> >// Using foreach loop> >foreach>(>var> val>in> myhash1)> >{> >Console.WriteLine(val);> >}> >// Creating another HashSet> >// using collection initializer> >// to initialize HashSet> >HashSet<>int>>myhash2 =>new> HashSet<>int>>() {10,> >100,1000,10000,100000};> > >// Display elements of myhash2> >Console.WriteLine(>'Elements of myhash2:'>);> >foreach>(>var> value>in> myhash2)> >{> >Console.WriteLine(value);> >}> >}> }> |
>
>산출
Elements of myhash1: C C++ C# Java Ruby Elements of myhash2: 10 100 1000 10000 100000>
HashSet에서 요소를 제거하는 방법은 무엇입니까?
HashSet에서는 HashSet의 요소를 제거할 수 있습니다. HashSet 클래스는 요소를 제거하는 세 가지 방법을 제공하며 해당 방법은 다음과 같습니다.
자바의 수학적 방법
- 제거(T) : 이 메소드는 HashSet 객체에서 지정된 요소를 제거하는 데 사용됩니다.
- RemoveWhere(술어) : 이 메서드는 HashSet 컬렉션에서 지정된 조건자에 의해 정의된 조건과 일치하는 모든 요소를 제거하는 데 사용됩니다.
- 분명한 : 이 메소드는 HashSet 객체에서 모든 요소를 제거하는 데 사용됩니다.
예시 1:
씨#
// C# program to illustrate how to> // remove elements of HashSet> using> System;> using> System.Collections.Generic;> class> GFG {> >// Main Method> >static> public> void> Main()> >{> >// Creating HashSet> >// Using HashSet class> >HashSet<>string>>마이해시 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash.Add(>'C'>);> >myhash.Add(>'C++'>);> >myhash.Add(>'C#'>);> >myhash.Add(>'Java'>);> >myhash.Add(>'Ruby'>);> >// Before using Remove method> >Console.WriteLine(>'Total number of elements present (Before Removal)'>+> >' in myhash: {0}'>, myhash.Count);> >// Remove element from HashSet> >// Using Remove method> >myhash.Remove(>'Ruby'>);> >// After using Remove method> >Console.WriteLine(>'Total number of elements present (After Removal)'>+> >' in myhash: {0}'>, myhash.Count);> >// Remove all elements from HashSet> >// Using Clear method> >myhash.Clear();> >Console.WriteLine(>'Total number of elements present'>+> >' in myhash:{0}'>, myhash.Count);> >}> }> |
>
>산출
Total number of elements present in myhash: 5 Total number of elements present in myhash: 4 Total number of elements present in myhash:0>
연산 집합
HashSet 클래스는 또한 세트에 대해 다양한 작업을 수행하는 데 사용되는 몇 가지 메서드를 제공하며 메서드는 다음과 같습니다.
- UnionWith(IEnumerable) : 이 메서드는 자체, 지정된 컬렉션 또는 둘 모두에 존재하는 모든 요소를 포함하도록 현재 HashSet 개체를 수정하는 데 사용됩니다.
예:
씨#
파이썬의 eol
// C# program to illustrate set operations> using> System;> using> System.Collections.Generic;> class> GFG {> >static> public> void> Main()> >{> >// Creating HashSet> >// Using HashSet class> >HashSet<>string>>myhash1 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash1.Add(>'C'>);> >myhash1.Add(>'C++'>);> >myhash1.Add(>'C#'>);> >myhash1.Add(>'Java'>);> >myhash1.Add(>'Ruby'>);> >// Creating another HashSet> >// Using HashSet class> >HashSet<>string>>myhash2 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash2.Add(>'PHP'>);> >myhash2.Add(>'C++'>);> >myhash2.Add(>'Perl'>);> >myhash2.Add(>'Java'>);> >// Using UnionWith method> >myhash1.UnionWith(myhash2);> >foreach>(>var> ele>in> myhash1)> >{> >Console.WriteLine(ele);> >}> >}> }> |
>
>산출
C C++ C# Java Ruby PHP Perl>
- IntersectWith(IEnumerable) : 이 메서드는 해당 개체와 지정된 컬렉션에 있는 요소만 포함하도록 현재 HashSet 개체를 수정하는 데 사용됩니다.
예:
씨#
자바 클래스 예
// C# program to illustrate set operations> using> System;> using> System.Collections.Generic;> class> GFG {> >// Main Method> >static> public> void> Main()> >{> >// Creating HashSet> >// Using HashSet class> >HashSet<>string>>myhash1 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash1.Add(>'C'>);> >myhash1.Add(>'C++'>);> >myhash1.Add(>'C#'>);> >myhash1.Add(>'Java'>);> >myhash1.Add(>'Ruby'>);> >// Creating another HashSet> >// Using HashSet class> >HashSet<>string>>myhash2 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash2.Add(>'PHP'>);> >myhash2.Add(>'C++'>);> >myhash2.Add(>'Perl'>);> >myhash2.Add(>'Java'>);> >// Using IntersectWith method> >myhash1.IntersectWith(myhash2);> >foreach>(>var> ele>in> myhash1)> >{> >Console.WriteLine(ele);> >}> >}> }> |
>
>산출
C++ Java>
- ExceptWith(IEnumerable) : 이 메서드는 현재 HashSet 개체에서 지정된 컬렉션의 모든 요소를 제거하는 데 사용됩니다.
예:
씨#
VLC 미디어 플레이어 다운로드 유튜브
// C# program to illustrate set operations> using> System;> using> System.Collections.Generic;> class> GFG {> >// Main Method> >static> public> void> Main()> >{> >// Creating HashSet> >// Using HashSet class> >HashSet<>string>>myhash1 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash1.Add(>'C'>);> >myhash1.Add(>'C++'>);> >myhash1.Add(>'C#'>);> >myhash1.Add(>'Java'>);> >myhash1.Add(>'Ruby'>);> >// Creating another HashSet> >// Using HashSet class> >HashSet<>string>>myhash2 =>new> HashSet<>string>>();> >// Add the elements in HashSet> >// Using Add method> >myhash2.Add(>'PHP'>);> >myhash2.Add(>'C++'>);> >myhash2.Add(>'Perl'>);> >myhash2.Add(>'Java'>);> >// Using ExceptWith method> >myhash1.ExceptWith(myhash2);> >foreach>(>var> ele>in> myhash1)> >{> >Console.WriteLine(ele);> >}> >}> }> |
>
>산출
C C# Ruby>