Rubyɨä֮HashʮһÎÊ
×î½üÔÚѧϰRails,ÒòΪѧϰµÄjavaΪÈëÃÅÓïÑÔ.ЩÐíµÄ²»Ì«ÊÊÓ¦.ÌرðÊÇRubyµÄHash.²»¹ýϸϸµÄÏëÏë¾ÍͨÁË.»ù±¾É϶¼ÊÇͨµÄKey-Value.³ýÁ˸տªÊ¼²»Ì«ÊÊӦд·¨Ã»Ê²Ã´´óµÄÎÊÌâ.Õâ¸öÊÇÎÒÔÚÍøÉÏ¿´µ½µÄһƪÈÕÖ¾.
<-----------------------------------------Ê©Ö÷»¹ÊǸîÁË°É-------------------------------------------------->
1. ÈçºÎ´´½¨Hash?
x = Hash.new
x = {}
x = {:a => 1, :b => 2}
ÕâЩ¶¼ÊÇ´´½¨HashµÄ·½·¨¡£
µÚÒ»¸öºÍµÚ¶þ¸öÒ»Ñù£¬±íʾ´´½¨¿ÕµÄHash.
µÚÈý¸ö±íʾ´´½¨µÄHash£¬ÓÐ2¶Ô¼ü/Öµ¡£
2. HashµÄ¼üÊÇÄÄЩÀàÐÍ?
¿ÉÒÔÊÇÈκÎÄ¿±ê£¬È磺
irb(main):002:0> x={}
=> {}
irb(main):003:0> x[1]=2
=> 2
irb(main):004:0> x["a"]="b"
=> "b"
irb(main):005:0> x[:z] = 100
=> 100
irb(main):006:0> x[[1,2,3]] = [4,5,6]
=> [4, 5, 6]
irb(main):007:0> x
=> {"a"=>"b", [1, 2, 3]=>[4, 5, 6], 1=>2, :z=>100}
ÓÐÁ½µã×¢Ò⣺
£¨1£©ÈκÎÄ¿±ê°üÀ¨Êý×é¡¢Hash¶¼¿ÉÒÔÊÇKey¡£
£¨2£©Ê¹ÓÃ×Ö·û×÷ΪKeyʱ£¬×îºÃʹÓöÔÓ¦µÄ·ûºÅ¡£ÀýÈ磬¼ü"a"¿ÉÒÔÓÃ:aÀ´´úÌæ¡£ÕâÊÇÒòΪÿ¸ö×Ö·û"a"¶¼ÊÇÒ»¸öÄ¿±ê£¬¶ø:aÊÇΨһµÄ£¬
¸ü½ÚÊ¡
ÄÚ´æ¡£
3. ÈçºÎ¸øHash¸³Ä¬ÈϵÄÖµ?
irb(main):003:0> x=Hash.new([])
=> {}
irb(main):004:0> x[:a]
=> []
±íʾ´´½¨Ò»¸öHash£¬ËüµÄĬÈÏÖµÊÇ¿ÕµÄÊý×é[]¡£
È»ºó·ÃÎÊx[:a]£¬ËäÈ»Õâ¸ö¼üûÓÐÊÂÏȶ¨Ò壬µ«»¹ÊÇÓÐĬÈÏÖµ¡£
Çë×¢Ò⣺ÕâÖÖ·½·¨¸³µÄĬÈÏÖµ£¬¶¼Ö¸Ïòͬһ¸öÄ¿±ê£¬Õâ¸öÄ¿±ê¸Ä±äÁË£¬»áÓ°Ïìµ½ËùÓÐĬÈÏÖµ¡£
irb(main):005:0> x[:a] << 1
=> [1]
irb(main):006:0> x[:b]
=> [1]
Òª×öµ½Ã¿¸ö¼ü¶¼¶ÔӦΨһµÄĬÈÏÖµ£¬Ó¦¸ÃÈç´Ë´´½¨Hash:
irb(main):007:0> y=Hash.new do |h,k| h[k] =[] end
=> {}
ÈÃÎÒÃÇÔÙ¿´¿´£º
irb(main):009:0> y[:a] << 1
=> [1]
irb(main):010:0> y[:b]
=> []
Ò»¸öÖµµÄ¸Ä±ä£¬²»»áÓ°Ïìµ½ÆäËûĬÈÏÖµ¡£
4. 鍼뱎˜Hash?
irb(main):018:0* x={1=>2,3=>4,5=>6}
=> {5=>6, 1=>2, 3=>4}
irb(main):019:0> x.each do |k,v| puts "#{k} #{v}" end
5 6
1 2
3 4
=> {5=>6, 1=>2, 3=>4}
ʹÓÃHashµÄeach·½·¨¡£
5. ÈçºÎ±éÀúHashµÄKeyºÍValue?
irb(main):020:0> x.keys.each do |k| puts k end
irb(main):021:0> x.values.each do |v| puts v end
ʹÓÃHashµÄkeysºÍvalues·½·¨£¬ËüÃÇ·µ»ØÒ»¸öÊý×é¡£
µ±È»»¹¿ÉÒÔʹÓÃeach_keyºÍeach_value·½·¨£¬¹ËÃû˼Òå¾ÍÊDZéÀúkeyºÍvalue.
irb(main):016:0> x.each_key do |s| puts s end
1
3
=> {1=>2, 3=>4}
irb(main):017:0> x.each_value do |s| puts s end
2
4
=> {1=>2, 3=>4}
6. Hash¿ÉÒÔsortÂð?
rubyµÄ
hashʵ¼ÊÊÇ¿ÉÒÔsortµÄ£¬·µ»ØÒ»¸ösortºóµÄ¶þάÊý×é¡£
irb(main):027:0> x
=> {5=>6, 1=>2, 3=>4}
irb(main):028:0> x.sort
=> [[1, 2], [3, 4], [5, 6]]
ÁíÍârubyÓÐOrderedHashÕâ¸öÀ࣬Ëü¿ÉÒÔÈÃHashÔªËØ°´²åÈë˳ÐòÀ´sort£¬ÀàËÆÓÚÊý×éµÄÄÜÁ¦¡£
7. ÈçºÎ´ÓHashÀïÌí¼ÓºÍɾ³ýÔªËØ?
Ôö¼ÓÔªËØ£¬Ö±½Ó¶¨ÒåKey/Value£º
irb(main):040:0> x[7]=8
=> 8
irb(main):041:0> x
=> {5=>6, 1=>2, 7=>8, 3=>4}
ɾ³ýÔªËØ£¬Ê¹ÓÃdelete·½·¨£¬²ÎÊýÊÇÏëɾ³ýµÄKey:
irb(main):042:0> x.delete(7)
=> 8
irb(main):043:0> x
=> {5=>6, 1=>2, 3=>4}
µ±È»Ò²¿ÉÒÔÓÃdelete_if´øÌõ¼þµÄɾ³ýHashÔªËØ¡£ÀýÈçɾ³ýËùÓÐKey´óÓÚ3µÄ£º
irb(main):074:0* x.delete_if do |k,v| k>3 end
=> {1=>2, 3=>4}
8. ÈçºÎ²éÕÒHashÔªËØ?
ʹÓÃhas_key?ºÍhas_value?À´ÅжÏÊÇ·ñÓжÔÓ¦¼üºÍÖµ£º
irb(main):052:0> x.has_key? 1
=> true
irb(main):053:0> x.has_value? 4
=> true
irb(main):054:0> x.has_key? 9
=> false
¿¼Âǵ½Ä¬ÈÏÖµµÄÇé¿ö£¬²»ÄÜÖ±½ÓÓÃif hash[:key]À´Åжϣ¬ÀýÈ磺
irb(main):055:0> y=Hash.new(9)
=> {}
irb(main):056:0> y.has_key? :a
=> false
irb(main):057:0> puts 1 if y[:a]
1
=> nil
ÆäËûµÄ²éÕÒ·½·¨£¬»¹ÓÐfind¡¢selectµÈ£¬ÀýÈçÕÒµ½ËùÓÐKey´óÓÚ2µÄÔªËØ£º
irb(main):038:0> x.select do |k,v| k>2 end
=> [[5, 6], [3, 4]]
9. ¿ÉÒÔ·´×ªHashÂð?
´ð°¸ÊÇ¿ÉÒÔ£¬Ê¹ÓÃinvert·½·¨£º
irb(main):011:0* x={1=>2,3=>4,5=>6}
=> {5=>6, 1=>2, 3=>4}
irb(main):012:0> x.invert
=> {6=>5, 2=>1, 4=>3}
10. HashºÍÊý×éÈçºÎת»»?
Hashת»»µ½Êý×éºÜ¼òµ¥£º
irb(main):020:0> x.to_a
=> [[5, 6], [1, 2], [3, 4]]
Êý×éת»»µ½HashÂé·³Ò»µã£¬×Ô¼ºÐ´Ò»¸ö·½·¨to_hash£º
irb(main):023:0>
class Array
irb(main):024:1> def to_hash
irb(main):025:2> unless size%2 == 0
irb(main):026:3> raise "array with odd number of elements"
irb(main):027:3> end
irb(main):028:2> hash = Hash.new
irb(main):029:2> 0.step(size-1,2) {|x| hash[self[x]] = self[x+1] }
irb(main):030:2> hash
irb(main):031:2> end
irb(main):032:1> end
=> nil
irb(main):033:0> x=[1,2,3,4,5,6]
=> [1, 2, 3, 4, 5, 6]
irb(main):034:0> x.to_hash
=> {5=>6, 1=>2, 3=>4}
11. HashµÄ¶ªÖµÐÐΪ?
ÈκÎʱºòÐÞ¸ÄÁËHashµÄKey£¨¼ÇסKeyÊÇ°üÀ¨Êý×éÔÚÄÚµÄÄ¿±ê£©£¬¶¼»á·¢Éú¶ªÖµÐÐΪ¡£
irb(main):001:0> h=Hash.new
=> {}
irb(main):002:0> x=[1,2,3]
=> [1, 2, 3]
irb(main):003:0> h[x]=4
=> 4
irb(main):004:0> h[x]
=> 4
irb(main):005:0> x<<4
=> [1, 2, 3, 4]
irb(main):006:0> h[x]
ÉÏÊöÐÞ¸ÄÁ˼üxµÄÖµ£¬ÄÇôÕâ¸ö¼ü¶ÔÓ¦µÄValue¾Í¶ªÊ§ÁË¡£
½â¾ö°ì·¨ÊÇrehash¡£ÈκÎʱºòÐÞ¸ÄÁËKeyµÄÖµ£¬¶¼ÒªrehashÒ»´Î¡£
irb(main):007:0> h.rehash
=> {[1, 2, 3, 4]=>4}
irb(main):008:0> h[x]
=> 4