| Visual Basic サンプル集 |
|
|
Visual Basic 中学校 > Visual Basic サンプル集 > Visual Basic サンプル集目次 >
Dictionaryの検索1 (キーが数値など基本的なデータ型の場合)
数値型のキーのDictionaryで、キーを使って検索する例
キーが文字列型や日付型などの場合も同じように記述できます。
1.キー 555444 が存在しているか検索する例
ContainsKeyメソッドでキーを存在するか確認できます。
| Public Class
Person Public Property Name As String Public Property Age As Integer End Class Private Sub DictionarySample1() Dim dic As New Dictionary(Of Integer, Person) dic.Add(123456, New Person With {.Name = "徳川家康", .Age = 20}) dic.Add(555444, New Person With {.Name = "豊臣秀吉", .Age = 22}) dic.Add(369369, New Person With {.Name = "織田信長", .Age = 30}) dic.Add(987654, New Person With {.Name = "足利義政", .Age = 45}) If dic.ContainsKey(555444) Then Console.WriteLine("キー555444は存在します。") Else Console.WriteLine("キー555444は存在しません。") End If End Sub |
VB2005
でも同じ方法でキーを検索できますが、この例で使用しているWith
によるオブジェクトの初期化がVB2015には導入されていないためこの例をそのまま実行することはできません。
2.キー 555444 の値を取得する例
| Public Class
Person Public Property Name As String Public Property Age As Integer End Class Private Sub DictionarySample1() Dim dic As New Dictionary(Of Integer, Person) dic.Add(123456, New Person With {.Name = "徳川家康", .Age = 20}) dic.Add(555444, New Person With {.Name = "豊臣秀吉", .Age = 22}) dic.Add(369369, New Person With {.Name = "織田信長", .Age = 30}) dic.Add(987654, New Person With {.Name = "足利義政", .Age = 45}) Dim target As Person = dic(555444) If target Is Nothing Then Console.WriteLine("キー555444は存在しません。") Else 'VB2015以降ではこのように簡潔に出力できる。 'Console.WriteLine($"名前:{target.Name} 年齢:{target.Age}") 'VB2013以前では文字列補間の$が使用できないので代わりに下記のように記述する。 Console.WriteLine(String.Format("名前:{0} 年齢:{1}", target.Name, target.Age)) End If End Sub |
VB2005
でも同じ方法で値を取得できますが、この例で使用しているWith
によるオブジェクトの初期化がVB2015には導入されていないためこの例をそのまま実行することはできません。また、$による文字列補間についても下記の制限も当てはまります。
3.最後が 4 で終わるキーをすべて取得する例
Whereメソッドで条件に該当する要素を抽出できます。この例ではキー(Key)自体を抽出しています。値(Value)の方を抽出する方法は後述の例で説明しています。
| Public Class
Person Public Property Name As String Public Property Age As Integer End Class Private Sub DictionarySample1() Dim dic As New Dictionary(Of Integer, Person) dic.Add(123456, New Person With {.Name = "徳川家康", .Age = 20}) dic.Add(555444, New Person With {.Name = "豊臣秀吉", .Age = 22}) dic.Add(369369, New Person With {.Name = "織田信長", .Age = 30}) dic.Add(987654, New Person With {.Name = "足利義政", .Age = 45}) Dim keys = dic.Keys.Where(Function(key) key.ToString.EndsWith("4")) Console.WriteLine("最後が 4 で終わるキーは、" & String.Join(" ", keys)) '列挙するならこのように書いても良い。(VB2008では不可) dic.Keys.ToList.ForEach(Sub(key) If key.ToString.EndsWith("4") Then Console.WriteLine("最後が4で終わるキー発見 " & key.ToString) End If End Sub) End Sub |
VB2005にはWhereメソッドがないので、条件に該当するキーを検索するにはFor Eachなどのループを使用してください。その方法は、下記の「4.最後が
4 で終わるキーの値をすべて取得する例1:伝統的な方法」を参考にしてください。
VB2008でもWhereメソッドを使った抽出はできますが、この例の最後で列挙しながらConsole.WriteLineを実行する部分は動作しません。
4.最後が 4 で終わるキーの値をすべて取得する例1:伝統的な方法
For Eachループを使って条件に該当する要素を抽出できます。
| Public Class
Person Public Property Name As String Public Property Age As Integer End Class Private Sub DictionarySample1() Dim dic As New Dictionary(Of Integer, Person) dic.Add(123456, New Person With {.Name = "徳川家康", .Age = 20}) dic.Add(555444, New Person With {.Name = "豊臣秀吉", .Age = 22}) dic.Add(369369, New Person With {.Name = "織田信長", .Age = 30}) dic.Add(987654, New Person With {.Name = "足利義政", .Age = 45}) Dim values As New List(Of Person) For Each item In dic If (item.Key.ToString.EndsWith("4")) Then values.Add(item.Value) End If Next '結果 values.ForEach(Sub(p) Console.WriteLine("伝統的なロジック:" & p.Name)) End Sub |
VB2005でもFor
Eachループを使って結果を抽出できますが、この例のその他の部分ではVB2005では対応していない手法をつかっているのでこの例をそのまま実行することはできません。Withによるオブジェクトの初期化と、For
Eachループでのitemの宣言で型を推論していること、結果表示にForEachメソッドが対応していない点です。
5.最後が 4 で終わるキーの値をすべて取得する例2:LINQのクエリ式による方法
LINQのクエリ式を使って条件に該当する要素を抽出できます。
| Public Class
Person Public Property Name As String Public Property Age As Integer End Class Private Sub DictionarySample1() Dim dic As New Dictionary(Of Integer, Person) dic.Add(123456, New Person With {.Name = "徳川家康", .Age = 20}) dic.Add(555444, New Person With {.Name = "豊臣秀吉", .Age = 22}) dic.Add(369369, New Person With {.Name = "織田信長", .Age = 30}) dic.Add(987654, New Person With {.Name = "足利義政", .Age = 45}) Dim values = From item In dic Where item.Key.ToString.EndsWith("4") Select item.Value '結果 values.ToList.ForEach(Sub(p) Console.WriteLine("LINQクエリ構文:" & p.Name)) End Sub |
VB2008でもLINQのクエリ式を使った抽出はできますが、プログラム内で改行するためには行継続文字
_ を使用する必要があるため、この例をそのまま実行することはできません。
6.最後が 4 で終わるキーの値をすべて取得する例3:メソッドチェーンによる方法
LINQのメソッドベースの構文を使って条件に該当する要素を抽出できます。
| Public Class
Person Public Property Name As String Public Property Age As Integer End Class Private Sub DictionarySample1() Dim dic As New Dictionary(Of Integer, Person) dic.Add(123456, New Person With {.Name = "徳川家康", .Age = 20}) dic.Add(555444, New Person With {.Name = "豊臣秀吉", .Age = 22}) dic.Add(369369, New Person With {.Name = "織田信長", .Age = 30}) dic.Add(987654, New Person With {.Name = "足利義政", .Age = 45}) Dim values = dic. Where(Function(item) item.Key.ToString.EndsWith("4")). Select(Function(item) item.Value) '結果 values.ToList.ForEach(Sub(p) Console.WriteLine("メソッドチェーン:" & p.Name)) End Sub |
VB2008でもメソッドベースの構文を使った抽出はできますが、プログラム内で改行するためには行継続文字
_ を使用する必要があるため、この例をそのまま実行することはできません。