次の方法で共有


Hashtable.Contains メソッド

Hashtable に特定のキーが格納されているかどうかを判断します。

Public Overridable Function Contains( _
   ByVal key As Object _) As Boolean Implements IDictionary.Contains
[C#]
public virtual bool Contains(objectkey);
[C++]
public: virtual bool Contains(Object* key);
[JScript]
public function Contains(
   key : Object) : Boolean;

パラメータ

  • key
    Hashtable 内で検索されるキー。

戻り値

指定したキーを持つ要素が Hashtable に格納されている場合は true 。それ以外の場合は false

実装

IDictionary.Contains

例外

例外の種類 条件
ArgumentNullException key が null 参照 (Visual Basic では Nothing) です。

解説

ほとんどの場合、この実装は O(1) に近くなります。

Contains は、 IDictionary.Contains を実装します。これは、 ContainsKey とまったく同じ動作をします。

使用例

Hashtable に特定の要素が格納されているかどうかを判断する方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesHashtable    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Hashtable.
        Dim myHT As New Hashtable()
        myHT.Add(0, "zero")
        myHT.Add(1, "one")
        myHT.Add(2, "two")
        myHT.Add(3, "three")
        myHT.Add(4, "four")
        
        ' Displays the values of the Hashtable.
        Console.WriteLine("The Hashtable contains the following values:")
        PrintIndexAndKeysAndValues(myHT)
        
        ' Searches for a specific key.
        Dim msg As String
        Dim myKey As Integer = 2
        If myHT.ContainsKey(myKey) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The key ""{0}"" is {1}.", myKey, msg)
        myKey = 6
        If myHT.ContainsKey(myKey) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The key ""{0}"" is {1}.", myKey, msg)
        
        ' Searches for a specific value.
        Dim myValue As String = "three"
        If myHT.ContainsValue(myValue) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The value ""{0}"" is {1}.", myValue, msg)
        myValue = "nine"
        If myHT.ContainsValue(myValue) Then
            msg = "in the Hashtable"
        Else
            msg = "NOT in the Hashtable"
        End If
        Console.WriteLine("The value ""{0}"" is {1}.", myValue, msg)
    End Sub
    
        
    Public Shared Sub PrintIndexAndKeysAndValues(myList As Hashtable)
        Dim myEnumerator As IDictionaryEnumerator = myList.GetEnumerator()
        Dim i As Integer = 0
        Console.WriteLine(ControlChars.Tab + "-INDEX-" + ControlChars.Tab _
           + "-KEY-" + ControlChars.Tab + "-VALUE-")
        While myEnumerator.MoveNext()
            i = i + 1
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}" + ControlChars.Tab + "{2}", i, myEnumerator.Key, _
               myEnumerator.Value)
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The Hashtable contains the following values:
'     -INDEX-    -KEY-    -VALUE-
'     [0]:    4    four
'     [1]:    3    three
'     [2]:    2    two
'     [3]:    1    one
'     [4]:    0    zero
' 
' The key "2" is in the Hashtable.
' The key "6" is NOT in the Hashtable.
' The value "three" is in the Hashtable.
' The value "nine" is NOT in the Hashtable. 

[C#] 
using System;
using System.Collections;
public class SamplesHashtable  {

   public static void Main()  {

      // Creates and initializes a new Hashtable.
      Hashtable myHT = new Hashtable();
      myHT.Add( 0, "zero" );
      myHT.Add( 1, "one" );
      myHT.Add( 2, "two" );
      myHT.Add( 3, "three" );
      myHT.Add( 4, "four" );

      // Displays the values of the Hashtable.
      Console.WriteLine( "The Hashtable contains the following values:" );
      PrintIndexAndKeysAndValues( myHT );

      // Searches for a specific key.
      int myKey = 2;
      Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );
      myKey = 6;
      Console.WriteLine( "The key \"{0}\" is {1}.", myKey, myHT.ContainsKey( myKey ) ? "in the Hashtable" : "NOT in the Hashtable" );

      // Searches for a specific value.
      String myValue = "three";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
      myValue = "nine";
      Console.WriteLine( "The value \"{0}\" is {1}.", myValue, myHT.ContainsValue( myValue ) ? "in the Hashtable" : "NOT in the Hashtable" );
   }


   public static void PrintIndexAndKeysAndValues( Hashtable myList )  {
      IDictionaryEnumerator myEnumerator = myList.GetEnumerator();
      int i = 0;
      Console.WriteLine( "\t-INDEX-\t-KEY-\t-VALUE-" );
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t[{0}]:\t{1}\t{2}", i++, myEnumerator.Key, myEnumerator.Value );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The Hashtable contains the following values:
    -INDEX-    -KEY-    -VALUE-
    [0]:    4    four
    [1]:    3    three
    [2]:    2    two
    [3]:    1    one
    [4]:    0    zero

The key "2" is in the Hashtable.
The key "6" is NOT in the Hashtable.
The value "three" is in the Hashtable.
The value "nine" is NOT in the Hashtable.
*/ 

[C++] 
#using <mscorlib.dll>
#using <system.dll>

using namespace System;
using namespace System::Collections;

void PrintIndexAndKeysAndValues( Hashtable* myList )  {
   IDictionaryEnumerator* myEnumerator = myList->GetEnumerator();
   int i = 0;
   Console::WriteLine( S"\t-INDEX-\t-KEY-\t-VALUE-" );
   while ( myEnumerator->MoveNext() )
      Console::WriteLine( S"\t[{0}]:\t{1}\t{2}", __box(i++), myEnumerator->Key, myEnumerator->Value );
   Console::WriteLine();
}

int main()  {
   // Creates and initializes a new Hashtable.
   Hashtable* myHT = new Hashtable();
   myHT->Add( __box(0), S"zero" );
   myHT->Add( __box(1), S"one" );
   myHT->Add( __box(2), S"two" );
   myHT->Add( __box(3), S"three" );
   myHT->Add( __box(4), S"four" );

   // Displays the values of the Hashtable.
   Console::WriteLine( S"The Hashtable contains the following values:" );
   PrintIndexAndKeysAndValues( myHT );

   // Searches for a specific key.
   int myKey = 2;
   Console::WriteLine( S"The key \"{0}\" is {1}.", __box(myKey),
      myHT->ContainsKey( __box(myKey) ) ? S"in the Hashtable" : S"NOT in the Hashtable" );
   myKey = 6;
   Console::WriteLine( S"The key \"{0}\" is {1}.", __box(myKey),
      myHT->ContainsKey( __box(myKey) ) ? S"in the Hashtable" : S"NOT in the Hashtable" );

   // Searches for a specific value.
   String* myValue = S"three";
   Console::WriteLine( S"The value \"{0}\" is {1}.", myValue,
      myHT->ContainsValue( myValue ) ? S"in the Hashtable" : S"NOT in the Hashtable" );
   myValue = S"nine";
   Console::WriteLine( S"The value \"{0}\" is {1}.", myValue, 
      myHT->ContainsValue( myValue ) ? S"in the Hashtable" : S"NOT in the Hashtable" );
}

 /*
 This code produces the following output.

 The Hashtable contains the following values:
        -INDEX- -KEY-   -VALUE-
        [0]:    4       four
        [1]:    3       three
        [2]:    2       two
        [3]:    1       one
        [4]:    0       zero

The key "2" is in the Hashtable.
The key "6" is NOT in the Hashtable.
The value "three" is in the Hashtable.
The value "nine" is NOT in the Hashtable.
 */

[JScript] 
import System
import System.Collections
import Microsoft.VisualBasic

// Creates and initializes a new Hashtable.
var myHT : Hashtable = new Hashtable()
myHT.Add(0, "zero")
myHT.Add(1, "one")
myHT.Add(2, "two")
myHT.Add(3, "three")
myHT.Add(4, "four")

// Displays the values of the Hashtable.
Console.WriteLine("The Hashtable contains the following values:")
PrintIndexAndKeysAndValues(myHT)

// Searches for a specific key.
var msg : String
var myKey : int = 2
if(myHT.ContainsKey(myKey))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The key \"{0}\" is {1}.", myKey, msg)
myKey = 6
if(myHT.ContainsKey(myKey))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The key \"{0}\" is {1}.", myKey, msg)

// Searches for a specific value.
var myValue : String = "three"
if(myHT.ContainsValue(myValue))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The value \"{0}\" is {1}.", myValue, msg)
myValue = "nine"
if(myHT.ContainsValue(myValue))
    msg = "in the Hashtable"
else
    msg = "NOT in the Hashtable"
Console.WriteLine("The value \"{0}\" is {1}.", myValue, msg)

function PrintIndexAndKeysAndValues(myList : Hashtable){
    var myEnumerator : IDictionaryEnumerator = myList.GetEnumerator()
    var i : int = 0
    Console.WriteLine("\t-INDEX-\t-KEY-\t-VALUE-")
    while(myEnumerator.MoveNext()){
        Console.WriteLine("\t[{0}]:\t{1}\t{2}", i++, myEnumerator.Key,
            myEnumerator.Value)
    }
    Console.WriteLine()
}

// This code produces the following output.
// 
// The Hashtable contains the following values:
//     -INDEX-    -KEY-    -VALUE-
//     [0]:       4        four
//     [1]:       3        three
//     [2]:       2        two
//     [3]:       1        one
//     [4]:       0        zero
// 
// The key "2" is in the Hashtable.
// The key "6" is NOT in the Hashtable.
// The value "three" is in the Hashtable.
// The value "nine" is NOT in the Hashtable. 

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Hashtable クラス | Hashtable メンバ | System.Collections 名前空間 | ContainsKey | IDictionary