C#での2つのHashSetの結合
2つの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);
set1.Add(400);
set1.Add(500);
set1.Add(600);
Console.WriteLine("HashSet1 elements...");
foreach(int ele in set1){
Console.WriteLine(ele);
}
HashSet<int> set2 = new HashSet<int>();
set2.Add(100);
set2.Add(200);
set2.Add(300);
set2.Add(400);
set2.Add(500);
set2.Add(600);
Console.WriteLine("HashSet2 elements...");
foreach(int ele in set2){
Console.WriteLine(ele);
}
Console.WriteLine("Union...");
set1.UnionWith(set2);
foreach(int ele in set1){
Console.WriteLine(ele);
}
}
} 出力
これにより、次の出力が生成されます-
HashSet1 elements... 100 200 300 400 500 600 HashSet2 elements... 100 200 300 400 500 600 Union... 100 200 300 400 500 600
例
別の例を見てみましょう-
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);
set1.Add(400);
set1.Add(500);
set1.Add(600);
Console.WriteLine("HashSet1 elements...");
foreach(int ele in set1){
Console.WriteLine(ele);
}
HashSet<int> set2 = new HashSet<int>();
set2.Add(100);
set2.Add(250);
set2.Add(300);
Console.WriteLine("HashSet2 elements...");
foreach(int ele in set2){
Console.WriteLine(ele);
}
Console.WriteLine("Union...");
set1.UnionWith(set2);
foreach(int ele in set1){
Console.WriteLine(ele);
}
}
} 出力
これにより、次の出力が生成されます-
HashSet1 elements... 100 200 300 400 500 600 HashSet2 elements... 100 250 300 Union... 100 200 300 400 500 600 250
-
2つの辞書をマージするC#プログラム
2つの辞書を設定する- Dictionary < string, int > dict1 = new Dictionary < string, int > (); dict1.Add("laptop", 1); dict1.Add("desktop", 2); Dictionary < string, int > dict2 = new Dictionary < string, int > (); dict2.Add("desktop", 3); dict2.Add("tabl
-
JavaのHashSet
HashSetはAbstractSetを拡張し、Setインターフェイスを実装します。ストレージにハッシュテーブルを使用するコレクションを作成します。 ハッシュテーブルは、ハッシュと呼ばれるメカニズムを使用して情報を格納します。ハッシュでは、キーの情報コンテンツを使用して、ハッシュコードと呼ばれる一意の値を決定します。 ハッシュコードは、キーに関連付けられたデータが格納されるインデックスとして使用されます。キーのハッシュコードへの変換は自動的に実行されます。 例 JavaでHashSetを実装する例を見てみましょう- import java.util.*; public class Dem