C#の別のコレクションからHashSetを作成します
別のコレクションからHashSetを作成するためのコードは、次のとおりです-
例
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<int> set1 = new HashSet<int>();
set1.Add(100);
set1.Add(200);
set1.Add(300);
Console.WriteLine("HashSet created from another collection...");
foreach(int i in set1){
Console.WriteLine(i);
}
HashSet<int> set2 = new HashSet<int>(set1);
Console.WriteLine("HashSet created from another collection...");
foreach(int i in set2){
Console.WriteLine(i);
}
}
} 出力
これにより、次の出力が生成されます-
HashSet created from another collection... 100 200 300 HashSet created from another collection... 100 200 300
例
別の例を見てみましょう-
using System;
using System.Collections.Generic;
public class Demo {
public static void Main(){
HashSet<string> set1 = new HashSet<string>();
set1.Add("Jacob");
set1.Add("Tom");
set1.Add("Harry");
set1.Add("Harry");
set1.Add("Tom");
set1.Add("Harry");
Console.WriteLine("HashSet created from another collection...");
foreach(string i in set1){
Console.WriteLine(i);
}
HashSet<string> set2 = new HashSet<string>(set1);
Console.WriteLine("HashSet created from another collection...");
foreach(string i in set2){
Console.WriteLine(i);
}
}
} 出力
これにより、次の出力が生成されます-
HashSet created from another collection... Jacob Tom Harry HashSet created from another collection... Jacob Tom Harry
-
C#のHashSet、C#Setコレクションとは何ですか?
C#のHashSetは、配列内の重複する文字列または要素を排除します。C#では、最適化されたセットコレクションです。 C#HashSet-を使用して重複する文字列を削除する例を見てみましょう。 例 using System; using System.Collections.Generic; using System.Linq; class Program { static void Main() { string[] arr1 = { "one
-
別の辞書の値からPython辞書を作成するにはどうすればよいですか?
これを行うには、他の辞書を最初の辞書にマージします。 Python 3.5以降では、**演算子を使用して辞書を解凍し、次の構文を使用して複数の辞書を組み合わせることができます- 構文 a = {'foo': 125} b = {'bar': "hello"} c = {**a, **b} print(c) 出力 これにより出力が得られます- {'foo': 125, 'bar': 'hello'} これは古いバージョンではサポートされていません。ただし、次の同様の構文を使用して置き換えることがで