爲什麼要這麼設計 ?
原因有二:
1. 每個對象創建時,都默認生成一個hashCode ,也就是一個經過哈希算法生成的一串數字 。當利用key去取字典中的value時,若是使用遍歷或者二分查找等方法,效率都相對較低 ,於是出現了根據每個key生成的hashCode將該鍵值對放到hashCode對應的數組中的指定位置,這樣當用key去取值時,便不必遍歷去獲取,即可以根據hashCode直接取出。
2. hashCode數字過大,或許會經過取餘生成一個較小的數字,假如是對999取餘,那麼得到的結果始終處於0-999之間。但是,這樣做的弊端在於取餘所得到的值,可能是相同的,這樣可能導致完全不相干的鍵值對被新的鍵值對(取餘後值key相等)所覆蓋。於是出現了數組中套鏈表實現的數組。這樣,key值取餘得到值相等的鍵值對,都將保存在同一個鏈表數組中,當查找key對應的值時,首先獲取到該鏈表數組,然後遍歷該數組,取正確的key所對應的值即可。
具體代碼如下:
鏈表實現的數組
#import <Foundation/Foundation.h>
@interface MYLinkedArray : NSObject
@property (assign , nonatomic) NSUInteger size; //數組長度
- (void)addObject:(NSObject *)obj; //添加元素
- (void)remove:(NSObject *)obj; //移除指定元素
- (void)removeAtIndex:(NSInteger)index; //移除指定索引元素
- (NSObject *)objectAtIndex:(NSInteger)index; //獲取指定位置的值
+ (instancetype)array; //數組初始化
@end
@interface Node : NSObject
@property (strong , nonatomic) Node *previous; //上個節點
@property (strong , nonatomic) Node *next; //下個節點
@property (strong , nonatomic) id content; //當前節點內容
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
. m文件
#import "MYLinkedArray.h"
@interface MYLinkedArray ()
@property (nonatomic, strong) Node *first; //首個節點
@property (nonatomic, strong) Node *last; //最後節點
@end
@implementation MYLinkedArray
//添加元素
- (void)addObject:(NSObject *)obj
{
if (!obj) return;
_size ++ ;
Node *node = [[Node alloc]init];
//首個節點爲空
if (!_first) {
_first = node;
_last = node;
node.previous = nil;
node.next = nil;
node.content = obj;
return;
}
//數組中有值
node.previous = _last;
node.next = nil;
node.content = obj;
_last = node;
_last.next = node;
}
//移除元素
- (void)remove:(NSObject *)obj
{
if (!obj||!_size) return;
Node *tmpNode = _first;
for (NSInteger index = 0; index < _size; index ++) {
if ([tmpNode.content isEqual:obj]) {
[self removeNode:tmpNode]; //移除節點
break;
}
}
}
//根據索引移除元素
- (void)removeAtIndex:(NSInteger)index
{
if (index<0||index>=_size) return;
Node *tmpNode = _first;
for (NSInteger i = 0; i < _size; i ++) {
if (i == index) {
[self removeNode:tmpNode]; //移除節點
break;
}
tmpNode = tmpNode.next;
}
}
//獲取指定索引元素
- (NSObject *)objectAtIndex:(NSInteger)index
{
if (index<0||index>=_size) return nil;
Node *tmpNode = _first;
for (NSInteger i = 0; i < _size; i ++) {
if (i == index) {
return tmpNode.content;
}
tmpNode = tmpNode.next;
}
return nil;
}
//私有
- (void)removeNode:(Node *)node
{
//連接上下節點
Node *preNode = node.previous;
Node *nextNode = node.next;
preNode.next = nextNode;
nextNode.previous = preNode;
node.content = nil; //清空被移除節點內容
_size -- ;//長度更新
}
//構造方法
+ (instancetype)array
{
return [[self alloc]init];
}
@end
@implementation Node
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
自定義的字典類
#import <Foundation/Foundation.h>
@interface MYDictionary : NSObject
+ (instancetype)dictionary;
- (void)setValue:(id)value forkey:(NSString *)key;
- (id)valueForKey:(NSString *)key;
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
. m文件
#import "MYDictionary.h"
#import "MYLinkedArray.h"
@interface KeyValueCache : NSObject
@property (nonatomic, strong) NSString *key;
@property (nonatomic, strong) id value;
@end
@implementation KeyValueCache
+ (instancetype)key:(NSString *)key value:(id)value
{
KeyValueCache *model = [[self alloc]init];
model.key = key;
model.value = value;
return model;
}
@end
@interface MYDictionary ()
@end
@implementation MYDictionary
{
MYLinkedArray *_keyValues[999];
}
//構造方法
+ (instancetype)dictionary
{
return [[self alloc]init];
}
//賦值
- (void)setValue:(id)value forkey:(NSString *)key
{
//獲取hashCode
NSUInteger hash = key.hash;
//默認一個對象佔用8個字節
NSUInteger realCode = hash%(sizeof(_keyValues)/8);
MYLinkedArray *linkArr = _keyValues[realCode];
if (linkArr) { //如果存在鏈表數組
for (NSInteger index = 0; index < linkArr.size; index ++) {
KeyValueCache *keyValue = (KeyValueCache *)[linkArr objectAtIndex:index];
if ([keyValue.key isEqualToString:key]) { //如果存在相同的Key ,更新value
keyValue.value = value; //重新賦值value
return;
}
}
//創建新的鍵值對存儲
KeyValueCache *newKeyValue = [KeyValueCache key:key value:value];
[linkArr addObject:newKeyValue];
}else{ //不存在鏈表數組
//創建新的鏈表數組
MYLinkedArray *newLinkArray = [MYLinkedArray array];
KeyValueCache *newKeyValue = [KeyValueCache key:key value:value];
[newLinkArray addObject:newKeyValue];
_keyValues[realCode] = newLinkArray;
}
}
//根據鍵取值
- (id)valueForKey:(NSString *)key
{
if (!key.length) return nil;
//獲取hashCode
NSUInteger hash = key.hash;
NSUInteger realCode = hash%(sizeof(_keyValues)/8);
MYLinkedArray *linkArr = _keyValues[realCode];
if (linkArr) {
//遍歷鏈表,取出value
for (NSInteger index = 0; index < linkArr.size; index ++) {
KeyValueCache *keyvalue = (KeyValueCache *)[linkArr objectAtIndex:index];
if ([keyvalue.key isEqualToString:key]) {
return keyvalue.value;
}
}
}
return nil;
}
@end
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
最終實驗結果:
- (void)viewDidLoad {
[super viewDidLoad];
MYDictionary *dict = [MYDictionary dictionary];
[dict setValue:@"哈哈" forkey:@"孟"];
[dict setValue:@"你好" forkey:@"孟"];
[dict setValue:@123456 forkey:@"數字"];
NSLog(@"%@",[dict valueForKey:@"孟"]);
NSLog(@"%@",[dict valueForKey:@"數字"]);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
成功自定義了一個字典.