この記事は会員限定です。会員登録(無料)すると全てご覧いただけます。
何らかの要素(オブジェクト)を、重複を省いて一覧として保持したい場合がある。例えば、項目を重複なく処理するために、すでに処理済みの項目一覧を保持しておき、次に処理すべき項目がすでに一覧にあれば何もせず、なければ一覧に加えて処理を行うといった場合である。
上記のような処理は.NET Framework 2.0であれば、Dictionaryクラス(System.Collections.Generic名前空間)を使用するのが一般的であった(Dictionaryクラスについては「TIPS:ハッシュテーブル(連想配列)を使うには?(Dictionaryクラス編)」を参照)。
次のコードは、英文中に現れる単語を重複せずに表示するサンプル・プログラムである。すでに表示した英単語をDictionaryクラスで管理している。
// dictionary.cs
using System;
using System.Collections.Generic;
class Program {
static void Main() {
string lincoln =
"government of the people by the people for the people";
// Dictionaryクラスによる単語テーブル
Dictionary<string, object> dictTable
= new Dictionary<string, object>();
foreach (string word in lincoln.Split(' ')) {
// 単語が未登録なら追加してから表示
if (!dictTable.ContainsKey(word)) {
dictTable.Add(word, null);
Console.WriteLine(word);
}
}
// 出力:
// government
// of
// the
// people
// by
// for
}
}
// コンパイル方法:csc dictionary.cs
' dictionary.vb
Imports System
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim lincoln As String = _
"government of the people by the people for the people"
' Dictionaryクラスによる単語テーブル
Dim dictTable As New Dictionary(Of String, Object)
For Each word As String In lincoln.Split(" ")
' 単語が未登録なら追加してから表示
If Not dictTable.ContainsKey(word) Then
dictTable.Add(word, Nothing)
Console.WriteLine(word)
End If
Next
' 出力:
' government
' of
' the
' people
' by
' for
End Sub
End Class
' コンパイル方法:vbc dictionary.vb
Dictionaryクラスはキーと値のペアを、キーが重複しないように保持するが、本稿のような用途ではキーのみで用が足りるため、値の方は未使用となる(上記のコードではnull/Nothingを取りあえず入れている)。
.NET Framework 3.5では、要素を重複なく格納するためのHashSetクラス(System.Collections.Generic名前空間)が新しく追加されている。いうなれば、このクラスはキーだけを保持するDictionaryクラスのようなものだ。
HashSetクラスでも新しい要素を追加するのにAddメソッドを使用するが、HashSetクラスのAddメソッドは少し便利になっており、すでに要素が存在していた場合にはfalseを返し、要素が追加できた場合にtrueを返すようになっている(DictionaryクラスのAddメソッドは、すでに要素が存在していると例外を発生する)。
HashSetクラスを使用して上記のサンプル・プログラムを書き換えると次のようになる。
// hashset.cs
using System;
using System.Collections.Generic;
class Program {
static void Main() {
string lincoln =
"government of the people by the people for the people";
// HashSetクラスによる単語テーブル
HashSet<string> hsTable = new HashSet<string>();
foreach (string word in lincoln.Split(' ')) {
// 単語を追加。未登録なら表示
if (hsTable.Add(word)) {
Console.WriteLine(word);
}
}
// 出力:
// government
// of
// the
// people
// by
// for
}
}
// コンパイル方法:csc hashset.cs
' hashset.vb
Imports System
Imports System.Collections.Generic
Class Program
Shared Sub Main()
Dim lincoln As String = _
"government of the people by the people for the people"
' HashSetクラスによる単語テーブル
Dim hsTable As New HashSet(Of String)
For Each word As String In lincoln.Split(" ")
' 単語を追加。未登録なら表示
If hsTable.Add(word) Then
Console.WriteLine(word)
End If
Next
' 出力:
' government
' of
' the
' people
' by
' for
End Sub
End Class
' コンパイル方法:vbc hashset.vb
HashSetクラスには、2つのHashSetオブジェクトの和集合や差集合、積集合といった集合演算を行うメソッド(Unionメソッド、Exceptメソッド、Intersectメソッド)なども用意されている。
利用可能バージョン:.NET Framework 3.5以降
カテゴリ:クラス・ライブラリ 処理対象:コレクション
使用ライブラリ:Dictionaryクラス(System.Collections.Generic名前空間)
使用ライブラリ:HashSetクラス(System.Collections.Generic名前空間)
関連TIPS:ハッシュテーブル(連想配列)を使うには?(Dictionaryクラス編)
Copyright© Digital Advantage Corp. All Rights Reserved.