>  기사  >  백엔드 개발  >  C# 해시세트

C# 해시세트

WBOY
WBOY원래의
2024-09-03 15:18:47202검색

순서가 아닌 고유한 요소 모음을 HashSet C#에서는 Systems.Collections.Generic 네임스페이스에 속하며 컬렉션에 중복 항목이 필요하지 않을 때마다 사용됩니다. 즉, 컬렉션에 중복 항목을 삽입하는 것을 방지하고 HashSet의 성능을 비교하기 위해 HashSet은 비교하여 더 나은 성능을 제공합니다. HashSet에서 제공하는 작업을 나열하고 설정하는 것은 높은 성능을 제공하며 객체가 보유하는 요소 수는 요소 수가 증가함에 따라 HashSet의 용량도 증가합니다.

C#의 HashSet 구문

HashSet<Type_of_hashset> Hashset_name = new HashSet<Type_of_hashset>();

위 구문은 C#의 HashSet을 나타냅니다. 해시세트의 유형은 대문자 T로 표시할 수도 있습니다.

C#에서 Hashset 작업

C#의 Hashset은 순서가 없는 고유한 요소 모음입니다. 이는 Systems.Collections.Generic 네임스페이스에 속하며 컬렉션에 중복이 필요하지 않을 때마다 사용됩니다. 즉, 컬렉션에 중복이 삽입되는 것을 방지하고 HashSet의 성능을 비교합니다. HashSet은 고성능을 제공하는 HashSet에서 제공하는 목록 집합 작업에 비해 더 나은 성능을 제공합니다. 해시셋의 작동 원리를 이해하기 위해 먼저 C#으로 간단한 해시셋 프로그램을 만들어 보겠습니다.

코드:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}

위 프로그램에서는 문자열 유형의 간단한 해시세트가 생성됩니다. 문자열 Shobha, Shivakumar 및 Shardha가 생성된 문자열 유형 해시 세트에 추가됩니다. 위 프로그램의 출력은 아래 스냅샷에 표시됩니다.

C# 해시세트

이미 설명했듯이 해시세트는 컬렉션에 중복된 요소를 추가하는 것을 허용하지 않습니다. 위에서 생성된 문자열 유형의 해시 세트에 중복 요소를 추가하고 프로그램의 출력을 이해해 보겠습니다. 그런 다음 add 메소드를 사용하여 컬렉션에 요소를 추가할 수 있습니다.

코드:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
nameslist.Add("Shobha");
//collection cannot contain duplicate elements.
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}

위 프로그램에서는 문자열 유형의 간단한 해시세트가 생성됩니다. 그런 다음 Shobha, Shivakumar 및 Shardha 문자열이 생성된 문자열 유형 해시 세트에 추가됩니다. add 메소드를 사용하여 생성된 해시셋에 중복 문자열 Shobha를 추가하려고 하면 오류가 표시되지 않지만 중복 문자열이 추가되지 않고 출력에서도 동일한 내용을 볼 수 있습니다. 위 프로그램의 출력은 아래 스냅샷에 표시됩니다.

C# 해시세트

C#에는 해시셋에 존재하는 메소드를 사용하는 Union이라는 메소드가 있습니다. 두 컬렉션에 있는 요소를 호출되는 단일 컬렉션으로 결합하는 데 사용됩니다. 아래 프로그램을 고려해 보세요.

코드:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist.UnionWith(nameslist1);
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}

위 프로그램에서는 문자열 유형의 두 가지 간단한 해시 세트가 생성됩니다. 먼저 Shobha, Shivakumar 및 Shardha 문자열이 유형 문자열의 첫 번째 해시 세트에 추가됩니다. 그런 다음 Shobha, Shivakumar, Shardha, Ravi 및 Nagu 문자열이 유형 문자열의 두 번째 해시 세트에 추가됩니다. 이제 첫 번째 해시 집합과 두 번째 해시 집합의 요소를 결합하기 위해 is hashset 메서드와 결합을 사용합니다. 위 프로그램의 출력은 아래 스냅샷에 표시됩니다.

C# 해시세트

C#에는 hashset에 존재하는 메소드와 Intersect라는 메소드가 있습니다. 두 컬렉션에 공통적으로 존재하는 요소를 호출되는 단일 컬렉션으로 결합하는 데 사용됩니다. 아래 프로그램을 고려해 보세요.

코드:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist.IntersectWith(nameslist1);
foreach(var nam in nameslist) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}

위 프로그램에서는 문자열 유형의 두 가지 간단한 해시 세트가 생성됩니다. 먼저, 문자열 Shobha, Shivakumar 및 Shardha가 유형 문자열의 첫 번째 해시 세트에 추가됩니다. 다음으로 Shobha, Shivakumar, Shardha, Ravi 및 Nagu 문자열이 유형 문자열의 두 번째 해시 세트에 추가됩니다. 이제 첫 번째 해시 집합과 두 번째 해시 집합 모두의 공통 요소를 결합하기 위해 is hashset 메서드와 교차를 사용합니다. 위 프로그램의 출력은 아래 스냅샷에 표시됩니다.

C# 해시세트

C#에는 Except with method가 hashset에 존재한다는 메서드가 있습니다. 호출된 컬렉션을 제외하고 두 컬렉션 모두에 존재하는 모든 요소를 ​​제거하는 데 사용됩니다. 아래 프로그램을 고려해 보세요.

코드:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HashSet {
class Programcheck {
static void Main(string[] args) {
HashSet < string > nameslist = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha"
};
HashSet < string > nameslist1 = new HashSet < string > {
"Shobha",
"Shivakumar",
"Shardha",
"Ravi",
"Nagu"
};
nameslist1.ExceptWith(nameslist);
foreach(var nam in nameslist1) {
Console.WriteLine(nam);
}
Console.ReadKey();
}
}
}

위 프로그램에서는 문자열 유형의 두 가지 간단한 해시 세트가 생성됩니다. 먼저, 문자열 Shobha, Shivakumar 및 Shardha가 유형 문자열의 첫 번째 해시 세트에 추가됩니다. 다음으로 Shobha, Shivakumar, Shardha, Ravi 및 Nagu 문자열이 유형 문자열의 두 번째 해시 세트에 추가됩니다. 이제 호출된 컬렉션을 제외한 모든 컬렉션에 존재하는 모든 요소를 ​​제거하기 위해 hashset의 메서드와 함께 Except를 사용합니다.  위 프로그램의 출력은 아래 스냅샷에 표시됩니다.

C# 해시세트

Examples of C# HashSet

Given below are the examples of C# HashSet:

Example #1

C# program to illustrate the creation of a hashset and adding elements to the hashset.

Code:

using System;
using System.Collections.Generic;
class Hashset {
// Calling the main method
public static void Main()
{
// An hashset of even numbers is created
HashSet<int> even = new HashSet<int>();
// Adding the elements in to the hashset
for (int i = 0; i < 5; i++) {
even.Add(2 * i);
}
// Printing the elements in the hashset
foreach(int j in even)
{
Console.WriteLine(j);
}
}
}

Output:

C# 해시세트

Example #2

C# program to illustrate contains method in a hashset.

Code:

using System;
using System.Collections.Generic;
class hashset {
// Calling the main method
public static void Main()
{
// An hashset consisting of strings is created
HashSet<string> cl = new HashSet<string>();
// Elements are inserted into the hashset
cl.Add("Shobha");
cl.Add("Shivakumar");
cl.Add("Shardha");
cl.Add("Ravi");
// Using the contains method to check if an element is present in the collection
if (cl.Contains("Shobha"))
Console.WriteLine("The element specified is present");
else
Console.WriteLine("The element specified is not present");
}
}

Output:

C# 해시세트

위 내용은 C# 해시세트의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:C#의 상속다음 기사:C#의 상속