ruby的数据结构--哈希(Hash)

1、哈希(Hash)

哈希也是存储对象的一个集合,哈希里面的元素是以"key" => “value”(键值对)这样的形式存在的,元素是没有顺序的,哈希的键可以是任意对象,键必须的唯一的,键通常用符号(Symbol)表示。

哈希的创建有两种形式,两种形式都是一样的,最常使用第二种:

irb(main):108:0> hash_1 = {:name => "Mary",:age => 12}

=> {:name=>"Mary", :age=>12}

irb(main):109:0> hash_2 = {name:"Mary",age:12}

=> {:name=>"Mary", :age=>12}

1

2

3

4

2、类型转换

(1)哈希转字符串to_s

irb(main):110:0> hash = {name:"Mary",age:12}

=> {:name=>"Mary", :age=>12}

irb(main):111:0> hash_str = hash.to_s

=> "{:name=>\"Mary\", :age=>12}"

1

2

3

4

(2)字符串转哈希eval,字符串必须是将hash转成字符串的那种格式,不是任意字符串都可以转成hash。通常用于从文件中读取hash的情况。

#定义一个字符串,字符串是将hash转成字符串的格式

irb(main):111:0> hash_str = "{:name=>\"Mary\", :age=>12}"

irb(main):112:0> eval(hash_str)

=> {:name=>"Mary", :age=>12}

#随便定义一个字符串

irb(main):113:0> str = "hello"

=> "hello"

#eval会报错,eval转换的字符串必须是将hash转成字符串的格式

irb(main):114:0> eval(str)

NameError: undefined local variable or method `hello' for main:Object

Did you mean?  help

from (irb):114:in `eval'

from (irb):114:in `eval'

from (irb):114

1

2

3

4

5

6

7

8

9

10

11

12

13

14

3、哈希操作

(1)== 判断两个哈希是否相等,要求每个键值对的类型和内容都相等,不要求顺序也一样,因为哈希中的元素是没有顺序的

irb(main):116:0> h1 = {7 => 35, "c" => 2, "a" => 1}

=> {7=>35, "c"=>2, "a"=>1}

irb(main):117:0> h2 = {"c" => 2, "a" => 1, 7 => 35}

=> {"c"=>2, "a"=>1, 7=>35}

irb(main):118:0> h3 = {:c => 2, :a => 1, 7 => 35}

=> {:c=>2, :a=>1, 7=>35}

irb(main):119:0> h1 == h2

=> true

#h2、和h3中元素的区别是键的类型,:c为符号类型、"c"为字符串类型,所以两个hash不相等

irb(main):120:0> h2 == h3

=> false

1

2

3

4

5

6

7

8

9

10

11

(2)hash[key] 通过key取哈希的值

irb(main):121:0> hash = {7 => 35, "c" => 2, "a" => 1}

=> {7=>35, "c"=>2, "a"=>1}

irb(main):122:0> hash[7]

=> 35

irb(main):123:0> hash["c"]

=> 2

#hash中不包含key值为:c的键,返回nil

irb(main):124:0> hash[:c]

=> nil

1

2

3

4

5

6

7

8

9

(3)hash[key] = value 如果key值已存在,则将key对应的值替换为value。如果key值不存在,则新建key => value 键值对

irb(main):125:0> hash = {7 => 35, "c" => 2, "a" => 1}

=> {7=>35, "c"=>2, "a"=>1}

#key为7已存在,7所对应的值被替换成888

irb(main):126:0> hash[7] = 888

=> 888

#key为:c不存在,新建:c=>3键值对

irb(main):127:0> hash[:c] = 3

=> 3

irb(main):128:0> hash

=> {7=>888, "c"=>2, "a"=>1, :c=>3}

1

2

3

4

5

6

7

8

9

10

(4)hash.delete(key) 删除key所在的键值对,并返回key所对应的value值

irb(main):129:0> hash = {7 => 35, "c" => 2, "a" => 1}

=> {7=>35, "c"=>2, "a"=>1}

irb(main):130:0> hash.delete("c")

=> 2

#hash中不包含key值为:c的键,返回nil

irb(main):131:0> hash.delete(:c)

=> nil

1

2

3

4

5

6

7

(5)hash.delete_if {| key,value | block } 删除满足block条件的键值对

irb(main):132:0> hash = {"1" => 100, "2" => 200, "3" => 300}

=> {"1"=>100, "2"=>200, "3"=>300}

#删除掉value的值大于280的键值对

irb(main):133:0> hash.delete_if {|key,value| value > 280}

=> {"1"=>100, "2"=>200}

#现在hash中的元素

irb(main):134:0> hash

=> {"1"=>100, "2"=>200}

1

2

3

4

5

6

7

8

(6)each 遍历哈希

hash.each {| key,value | block } 遍历每个键值对

hash.each_key {| key | block } 遍历每个键

hash.each_value {| value | block } 遍历每个值

#用第二种形式定义hash,是不是比第一种的简单

irb(main):140:0> hash = {a:100,b:200,c:300}

=> {:a=>100, :b=>200, :c=>300}

#遍历键值对,块中有两个变量,第一个变量是键,第二个变量是值

irb(main):141:0> hash.each {|key,value| puts "键为#{key},值为#{value}"}

键为a,值为100

键为b,值为200

键为c,值为300

=> {:a=>100, :b=>200, :c=>300}

#只遍历键,块中的变量是键

irb(main):142:0> hash.each_key {|key| puts "键为#{key}"}

键为a

键为b

键为c

=> {:a=>100, :b=>200, :c=>300}

#只遍历值,块中的变量是值

irb(main):143:0> hash.each_value {|value| puts "值为#{value}"}

值为100

值为200

值为300

=> {:a=>100, :b=>200, :c=>300}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

(7)hash.empty? 判断哈希是否为空,为空返回true,否则返回false

irb(main):001:0> h_1 = {}

=> {}

irb(main):002:0> h_1.blank?

=> true

1

2

3

4

(8)include 判断哈希中是否有某个值

hash.include?(key) 判断哈希中是否有键key,有返回true,没有返回false

hash.has_key?(key) 与include?方法同样用法

hash.has_value?(value) 判断哈希中是否有值value,有返回true,没有返回false

irb(main):003:0> hash = {a:100,b:200,c:300}

=> {:a=>100, :b=>200, :c=>300}

irb(main):004:0> hash.include?(100)

=> false

irb(main):005:0> hash.include?(:a)

=> true

irb(main):006:0> hash.has_value?(100)

=> true

1

2

3

4

5

6

7

8

(9)数组形式返回键、值

hash.keys 以数组的形式返回哈希所有的key

hash.values 以数组的形式返回哈希所有的value

irb(main):007:0> hash = {a:100,b:200,c:300}

=> {:a=>100, :b=>200, :c=>300}

irb(main):008:0> hash.keys

=> [:a, :b, :c]

irb(main):009:0> hash.values

=> [100, 200, 300]

1

2

3

4

5

6

(10)hash.length 哈希的长度

irb(main):010:0> hash = {a:100,b:200,c:300}

=> {:a=>100, :b=>200, :c=>300}

irb(main):011:0> hash.length

=> 3

1

2

3

4

(11)hash_1.merge(hash_2) 将hash_1和hash_2两个哈希里面的元素合起来,返回一个新的哈希,如果hash_1和hash_2两个哈希里面有相同的key值,则key对应的值以后面的哈希(即hash_2)中的值为准。

hash_1.merge(hash_2) hash_1的值不会改变

hash_1.merge!(hash_2) hash_1的值会改变

irb(main):012:0> hash1 = {a:100,b:200}

=> {:a=>100, :b=>200}

irb(main):013:0> hash2 = {b:300,d:400}

=> {:b=>300, :d=>400}

irb(main):014:0> hash1.merge(hash2)

=> {:a=>100, :b=>300, :d=>400}

#hash1中的值没有改变

irb(main):016:0> hash1

=> {:a=>100, :b=>200}

#merge!

irb(main):017:0> hash1.merge!(hash2)

=> {:a=>100, :b=>300, :d=>400}

#hash1中的值发生了改变

irb(main):018:0> hash1

=> {:a=>100, :b=>300, :d=>400}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

(12)reject、select返回不满足条件或者满足条件的键值对

hash.reject {|key,value| block} 返回所有不满足block条件的元素,hash本身不会改变

hash.reject! {|key,value| block} 返回所有不满足block条件的元素,hash本身会改变

hash.select {|key,value| block} 返回所有满足block条件的元素,hash本身不会改变

hash.select! {|key,value| block} 返回所有满足block条件的元素,hash本身会改变

irb(main):019:0> hash = {a:100,b:200,c:300}

=> {:a=>100, :b=>200, :c=>300}

irb(main):020:0> hash.reject {|k,v| v > 200}

=> {:a=>100, :b=>200}

irb(main):021:0> hash.select {|k,v| v > 200}

=> {:c=>300}

————————————————

版权声明:本文为CSDN博主「猫宁一」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/shine_a/article/details/85276270

你可能感兴趣的:(ruby的数据结构--哈希(Hash))