hash

HashMap 内部原理

孤人 提交于 2019-12-30 12:11:33
HashMap 内部实现 通过名字便可知道的是,HashMap 的原理就是散列。HashMap内部维护一个 Buckets 数组。每一个 Bucket 封装为一个 Entry<K, V> 键值对形式的链表结构。这个 Buckets 数组也称为表。表的索引是 密钥 K 的散列值(散列码)。 例如以下图所看到的: 链表的每一个节点是一个名为 Entry<K,V> 的类的实例。 Entry 类实现了 Map.Entry 接口,以下是Entry类的代码: private static class Entry<K,V> implements Map.Entry<K,V> { final K key; final int hash; V value; Entry<K,V> next; } 注: 每一个 Entry 对象仅与一个特定 key 相关联。但其 value 是能够改变的(假设同样的 key 之后被又一次插入不同的 value) - 因此键是终于的,而值不是。 每一个Entry对象都有一个名为 next 的字段,它指向下一个Entry,所以实际上为单链表结构。hash 字段存储了 Entry 对象在 Buckets 数组索引,也就是 key 的散列值。 假设发生Hash碰撞,也就是两个key的hash值同样,或者假设这个元素所在的位子上已经存放有其它元素了

深入理解HashMap

淺唱寂寞╮ 提交于 2019-12-30 12:11:10
1、为什么用HashMap? HashMap是一个散列桶(数组和链表),它存储的内容是键值对(key-value)映射 HashMap采用了数组和链表的数据结构,能在查询和修改方便继承了数组的线性查找和链表的寻址修改 HashMap是非synchronized,所以HashMap很快 HashMap可以接受null键和值,而Hashtable则不能(原因就是equlas()方法需要对象,因为HashMap是后出的API经过处理才可以) 2、HashMap的工作原理是什么? HashMap是基于hashing的原理,我们使用put(key, value)存储对象到HashMap中,使用get(key)从HashMap中获取对象。当我们给put()方法传递键和值时,我们先对键调用hashCode()方法,计算并返回的hashCode是用于找到Map数组的bucket位置来储存Node 对象。这里关键点在于指出,HashMap是在bucket中储存键对象和值对象,作为Map.Node 。   以下是HashMap初始化 ,简单模拟数据结构 Node[] table=new Node[16] 散列桶初始化,table class Node { hash;//hash值 key;//键  value;//值  node next;//用于指向链表的下一层(产生冲突,用拉链法) }

Could a page display diferrent content if the URL hash changes?

好久不见. 提交于 2019-12-30 10:37:29
问题 How could a page display different content based on the URL hash? I'm not talking about the browser scrolling down to display the anchored section, but something like JavaScript reacting to that hash and loading different content via AJAX. Is this common or even probable? 回答1: Oh yes - it's becoming a common pattern to handle page-state-to-URL persistence when content is AJAX driven. Javascript can access this value via window.location.hash. Once that's done, you can perform any actions based

Anagrams - Hashing with chaining and probing in C

一个人想着一个人 提交于 2019-12-30 10:26:24
问题 My title got edited, so I wanted to make sure everyone knows this is homework. The problem is just to optimize the program, the hashing is my idea. -- I'm working on optimizing a C program that groups together words that are anagrams of each other, and then prints them out. Currently the program is basically a linked list of linked lists. Each link in the outer list is a group of words that are anagrams of each other. The profile for the program shows that by far, the largest portion of

Persisting hashlib state

一个人想着一个人 提交于 2019-12-30 10:13:07
问题 I'd like to create a hashlib instance, update() it, then persist its state in some way. Later, I'd like to recreate the object using this state data, and continue to update() it. Finally, I'd like to get the hexdigest() of the total cumulative run of data. State persistence has to survive across multiple runs. Example: import hashlib m = hashlib.sha1() m.update('one') m.update('two') # somehow, persist the state of m here #later, possibly in another process # recreate m from the persisted

Using a time-based, rotating hash or string for security

南楼画角 提交于 2019-12-30 09:35:33
问题 In a CMS app I occasionally need to open an iframe of another domain. At the moment I am setting the URL for that iframe to something very obscure. Like http://domain.com/iframe/jhghjg34787386/. This works but theoretically that iframe source url will get saved in the user's history and could be accessed from the outside world. So, I am wondering about using a time-based approach to an ever-changing hash or string that is processed on the request side and is checked on the iframe source side.

Zend_Auth setCredentialTreatment

邮差的信 提交于 2019-12-30 08:29:09
问题 I'm using Zend_Auth with setCredentialTreatment to set the hash method and salt. I see all examples doing something like this, where the salt seems to be inserted as a text. ->setCredentialTreatment('SHA1(CONCAT(?,salt))' but my salt is stored in the database. I could retrieve it first then use it in setCredentialTreatment but is there a way I could define it directly as a field name, so setCredentialTreatment would know to get it from that field? sort of like the way we define the field name

make perl shout when trying to access undefined hash key

淺唱寂寞╮ 提交于 2019-12-30 08:22:21
问题 I think the title is self-explanatory. Many times I have small typos and I get unexpected results when trying to access undefined hash keys. I know I can add some defined check before each time I access a hash key, but I wonder if there's any cleaner way to warn against such cases.... Best, Dave 回答1: This is probably best done with a tied hash. Tied variables allow you to define the implementation of the low level operations of the variable. In this case, we want a special fetch method that

How should I delete hash elements while iterating?

耗尽温柔 提交于 2019-12-30 08:08:32
问题 I have fairly large hash (some 10M keys) and I would like to delete some elements from it. I usually don't like to use delete or splice , and I wind up copying what I want instead of deleting what I don't. But this time, since the hash is really large, I think I'd like to delete directly from it. So I'm doing something like this: foreach my $key (keys %hash) { if (should_be_deleted($key)) { delete($hash{$key}); } } And it seems to work OK. But.. what if I'd like to delete some elements even

How to convert torrent info hash for scrape?

ぃ、小莉子 提交于 2019-12-30 07:13:04
问题 I have a torrent hash from the magnet link. For example: fda164e7af470f83ea699a529845a9353cc26576 When I try to get information about leechers and peers I should request: http://tracker.publicbt.com/scrape?info_hash=??? How should I convert info hash for this request? Is it url encoding or becoding? how? In PHP. 回答1: It's a raw hexadecimal representation. Use pack() with H to convert it. Then URL encode it. 回答2: Got this python snippet from a colleague, r = '' s =