Dictionary 概述
Dictionary<TKey, TValue>
存储的是键值对(Key - Value),通过键(Key)来存储或修改值(Value)
Dictionary<TKey, TValue>
存储的键值对是无序的
Dictionary<TKey, TValue>
存储的键是不可重复的
Dictionary<TKey, TValue>
支持泛型,可以指定存储的键值对的类型
Dictionary<TKey, TValue>
不是线程安全的,在多线程环境中需要谨慎使用
一、Dictionary 存储对象的特性
Dictionary 存储对象或对对象进行检测时(对象作为键或值时),对于没有重写 Equals 和 GetHashCode 方法的对象可能不太方便
internal class Person
{ public String name; public int age; public Person ( ) { } public Person ( string name, int age) { this . name = name; this . age = age; }
}
Dictionary< Person, string > messageDict = new Dictionary< Person, string > ( ) ; messageDict. Add ( new Person ( "Alice" , 30 ) , "Hello World 1" ) ;
messageDict. Add ( new Person ( "Bob" , 25 ) , "Hello World 2" ) ; Console. WriteLine ( messageDict. ContainsKey ( new Person ( "Alice" , 30 ) ) ) ;
Console. WriteLine ( messageDict. ContainsKey ( new Person ( "Alice" , 31 ) ) ) ;
# 输出结果False
False
Dictionary< string , Person> personDict = new Dictionary< string , Person> ( ) ; personDict. Add ( "Alice" , new Person ( "Alice" , 30 ) ) ;
personDict. Add ( "Bob" , new Person ( "Bob" , 25 ) ) ; Console. WriteLine ( personDict. ContainsValue ( new Person ( "Alice" , 30 ) ) ) ;
Console. WriteLine ( personDict. ContainsValue ( new Person ( "Alice" , 31 ) ) ) ;
# 输出结果False
False
Dictionary 存储对象或对对象进行检测时(对象作为键或值时),对于重写 Equals 和 GetHashCode 方法的对象比较方便
internal class Staff
{ public String name; public int age; public Staff ( ) { } public Staff ( string name, int age) { this . name = name; this . age = age; } public override bool Equals ( object obj) { if ( obj == this ) return true ; if ( obj == null ) return false ; if ( obj. GetType ( ) != this . GetType ( ) ) return false ; Staff staff = obj as Staff ; bool agesAreEqual = age == staff. age; bool namesAreEqual = name == null ? null == staff. name : name. Equals ( staff. name) ; return agesAreEqual && namesAreEqual; } public override int GetHashCode ( ) { int hash = 17 ; hash = hash * 23 + name?. GetHashCode ( ) ?? 0 ; hash = hash * 23 + age. GetHashCode ( ) ; return hash; }
}
Dictionary< Staff, string > messageDict = new Dictionary< Staff, string > ( ) ; messageDict. Add ( new Staff ( "Alice" , 30 ) , "Hello World 1" ) ;
messageDict. Add ( new Staff ( "Bob" , 25 ) , "Hello World 2" ) ; Console. WriteLine ( messageDict. ContainsKey ( new Staff ( "Alice" , 30 ) ) ) ;
Console. WriteLine ( messageDict. ContainsKey ( new Staff ( "Alice" , 31 ) ) ) ;
# 输出结果True
False
Dictionary< string , Staff> staffDict = new Dictionary< string , Staff> ( ) ; staffDict. Add ( "Alice" , new Staff ( "Alice" , 30 ) ) ;
staffDict. Add ( "Bob" , new Staff ( "Bob" , 25 ) ) ; Console. WriteLine ( staffDict. ContainsValue ( new Staff ( "Alice" , 30 ) ) ) ;
Console. WriteLine ( staffDict. ContainsValue ( new Staff ( "Alice" , 31 ) ) ) ;
# 输出结果True
False
二、Dictionary 与数组的转换
1、Dictionary 转数组
Dictionary 转键值对数组
Dictionary< string , int > dict = new Dictionary< string , int > ( ) ; dict. Add ( "Alice" , 30 ) ;
dict. Add ( "Bob" , 25 ) ; KeyValuePair< string , int > [ ] array = dict. ToArray ( ) ; foreach ( KeyValuePair< string , int > kvp in array)
{ Console. WriteLine ( $" { kvp. Key } - { kvp. Value } " ) ;
}
# 输出结果Alice - 30
Bob - 25
Dictionary 转键数组
Dictionary< string , int > dict = new Dictionary< string , int > ( ) ; dict. Add ( "Alice" , 30 ) ;
dict. Add ( "Bob" , 25 ) ; KeyValuePair< string , int > [ ] array = dict. ToArray ( ) ; foreach ( KeyValuePair< string , int > kvp in array)
{ Console. WriteLine ( $" { kvp. Key } - { kvp. Value } " ) ;
}
# 输出结果Alice
Bob
Dictionary 转值数组
Dictionary< string , int > dict = new Dictionary< string , int > ( ) ; dict. Add ( "Alice" , 30 ) ;
dict. Add ( "Bob" , 25 ) ; string [ ] array = dict. Keys. ToArray ( ) ; foreach ( string item in array)
{ Console. WriteLine ( item) ;
}
# 输出结果30
25
2、数组转 Dictionary
var array = new ( string , int ) [ ]
{ ( "Alice" , 30 ) , ( "Bob" , 25 )
} ; Dictionary< string , int > dict = array. ToDictionary ( kvp => kvp. Item1, kvp => kvp. Item2) ; foreach ( KeyValuePair< string , int > kvp in dict)
{ Console. WriteLine ( $" { kvp. Key } - { kvp. Value } " ) ;
}
# 输出结果Alice - 30
Bob - 25