由买买提看人间百态

topics

全部话题 - 话题: hashmap
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)
f*****n
发帖数: 2126
1
来自主题: JobHunting版 - hashmap和hashtable的区别?
There are several differences between HashMap and Hashtable in Java:
Hashtable is synchronized, whereas HashMap is not. This makes HashMap better
for non-threaded applications, as unsynchronized Objects typically perform
better than synchronized ones.
Hashtable does not allow null keys or values. HashMap allows one null key
and any number of null values.
One of HashMap's subclasses is LinkedHashMap, so in the event that you'd
want predictable iteration order (which is insertion order by default... 阅读全帖
f*********5
发帖数: 576
2
来自主题: JobHunting版 - hashmap跟hash table有啥区别?
Q:
Difference between HashMap and HashTable?
A: The HashMap class is roughly equivalent to Hashtable, except
that it is unsynchronized and permits nulls. (HashMap allows null values
as key and value whereas Hashtable doesnt allow). HashMap does not
guarantee that the order of the map will remain constant over time.
HashMap is unsynchronized and Hashtable is synchronized.

uses
person's
hash fu
array el
sought.
S**********5
发帖数: 896
3
来自主题: JobHunting版 - HashMap这样写对吗?
面试一个简单题,想到用hashmap来做。
我是这样写的:
Map map = new HashMap<>();
题做完后没问题,但是烙印面试官说我JAVA基础不行,因为hashmap用了上面的写法,
他说会写代码的有经验的人是这样写的:
HashMap map = new HashMap();
不知道最后会不会给过,但是我觉得我写的没问题,网上看到大家也这么写啊。谁能给
说一下?他是不是想黑我?还是以后面试按他的写法比较保险?
t******h
发帖数: 120
4
HashSet internally uses HashMap, an entry in HashMap is a pair,
HashSet would only use "key" to store its value, and put dummy data in "
value".
A hash function would map the "key" to one position in the internal array in
HashMap. If collision happens in HashMap, the old value will be kicked out.
You can read the JDK source code.
w*****e
发帖数: 721
5
来自主题: JobHunting版 - Isomorphic Strings 的单Hashmap解法
char [] a="abcd".toCharArray();
char [] b="pqap".toCharArray();
int i=0;
boolean TF=true;
HashMap h1=new HashMap();
HashMap h2=new HashMap();
for (i=0;i< a.length; i ++){
if (
( h1.containsKey(a[i]) && (h1.get(a[i]).compareTo(b[i])!=0) ) ||
( h2.containsKey(b[i]) && (h2.get(b[i]).compareTo(a[i])!=0) )
){
TF=false;
}else{
h1.put(a[i],b[i]);
h2.put(b[i],a[i]);
... 阅读全帖
r********7
发帖数: 102
6
这题是基本问题。 但是自己总拿不准怎么做才算正确。 面试官只是说 “HashMap
Implementation”。 我知道要考虑“hash function” "equal"两个函数。但是
1,如何重写这两个函数才算标准。
2, 用什么data structure 存储key 和 value 实现 hashmap呢?
只是说让implement hashmap, 但啥具体信息都没给,很摸不着头脑。
求大牛给个标准答案或者遇到类似XXX Implementation时的解题思路。
i*****e
发帖数: 63
7
来自主题: JobHunting版 - Isomorphic Strings 的单Hashmap解法
一个hashmap两个hashmap的解法区别在哪里?空间复杂度吗?一个是n,另一个是2n,不
过都是O(n).
如果非要一个hashmap,那就在每个字符的前面加个特殊符号来表示左映射或者右映射
,比如:
b --> o
{">b" : "o",
" 在判断
bcb --> ood
就会第二个字母发现问题,因为 " {">b" : "o",
" ">c": "o"}
m******o
发帖数: 774
8
I created the web service using annotation @WebService, etc. It appears that
a wsdl file is created automatically when the code is deployed within JBoss
. The wsdl file specifies that the return type of this web service method
would be 'hashMap'. However when the method is being called, an error is
thrown saying 'hashMap' is not a recognized type. I googled around and it
does look like HashMap,ArrayList etc. are no longer supported by current web
service specification. I assume java2wsdl would f
f**********3
发帖数: 295
9
在做leetcode LRU,自己写个LinkedList没问题,但是想用java的实现,遇到困难了...
比如
HashMap map = new HashMap();
Integer key = new Integer(3);
map.put(key, 10);
LinkedList ll = new LinkedList();
ll.add(1);
ll.add(key); //同样的key object,加到linked list里面
ll.add(10);
// the list should be 1->3->10 now
int keyToLookUp = 3;
然后怎样可以在map里拿到原来的key object reference然后把ll里面对应的node删除
呢? 就是HashMap里需要给key的value,找key的object reference。 LinkedList里需
要给node object reference,然后从list里删掉。我知道大... 阅读全帖
m*****k
发帖数: 731
10
来自主题: Java版 - JDK HashMap 的 Entry class
有一个int 成员 hash, 貌似没有用啊, 有人知道它留着何用?
static class Entry implements Map.Entry {
final K key;
V value;
Entry next;
final int hash;
/**
* Creates new entry.
*/
Entry(int h, K k, V v, Entry n) {
value = v;
next = n;
key = k;
hash = h;
}
public final K getKey() {
return key;
}
public final V getValue() {
return val... 阅读全帖
r**********1
发帖数: 292
11
来自主题: JobHunting版 - HashMap, HashTable and Array 有啥区别
我看到有人就用array来代替hashmap,比如有个问题是问字符串1是否包含字符串2的所
有字符的时候,有人用256大小的array来做,如果是hashMap的话,有啥区别?
这三者有啥区别啊?
R*****i
发帖数: 2126
12
来自主题: JobHunting版 - 请教个面试题, tree和hashmap的区别
感觉比较tree和hashmap就是比较牛头和马嘴,除了都是collection以外,没有任何相
似之处。tree是一种hierarchy,寻找的cost是O(log(n)),并且可以让您快速找到大小顺
序中的左邻右舍,hashmap是hashtable?, 根据key找value,寻找的cost是O(1).
z****e
发帖数: 54598
13
来自主题: JobHunting版 - 请教个面试题, tree和hashmap的区别
tree是一种结构
hashcode是另外一种结构
java常见两个对比
hashmap vs treemap,这个是结构的不同,索引方式的不同
hashmap vs hashtable,这个是并发差异,结构是类似的
r****7
发帖数: 2282
14
来自主题: JobHunting版 - Isomorphic Strings 的单Hashmap解法
怎么定义是“单Hashmap”?
一个Hashmap用完一次清空再重新用算么?
c********t
发帖数: 5706
15
来自主题: JobHunting版 - 问一个hashtable/hashmap capacity的问题
java API对hashmap capacity有这么一段话
Higher capacity values decrease the space overhead but increase the lookup
cost (reflected in most of the operations of the HashMap class, including
get and put)
我不明白大capacity, 为什么会增加lookup cost呢?lookup不是用hash value作index
直接取value吗?我觉得大capacity减少了collision,应该decrease lookup cost才对
啊。
M******r
发帖数: 120
16
来自主题: JobHunting版 - 如何O(1)复杂度比较两个Hashmap相等
有一道题,要maintain一个sliding window,这个window用Hashmap来实现,
但是java里面hashmap.equals复杂度是O(n)
原题
Given a list of keywords and a list of search words, return a list of
indices that indicate the beginning of sequences of adjacent keywords.
Examples:
Search list: ['hello', 'hi', 'welcome', 'greetings', 'hi', 'greetings', 'hey
', 'hello']
Keywords: ['hi', 'hey', 'greetings']. from: 1point3acres.com/bbs
Output: [4]
Search list: ['i', 'saw', 'susie', 'sitting', 'in', 'a', 'shoe', 'shine', '
shop', 'where', 'she'... 阅读全帖

发帖数: 1
17
来自主题: JobHunting版 - 如何O(1)复杂度比较两个Hashmap相等
我觉得可以这样。
把Keywords建踹树。
然后hashmap对keywords统计。
把hashmap里面每个当前的值删了。如果map为空,就记录左指针。
并且把左指针的的值放入map里。
这样就O(n * k)了 k为字符串平均长度。
M******r
发帖数: 120
18
来自主题: JobHunting版 - 如何O(1)复杂度比较两个Hashmap相等
有一道题,要maintain一个sliding window,这个window用Hashmap来实现,
但是java里面hashmap.equals复杂度是O(n)
原题
Given a list of keywords and a list of search words, return a list of
indices that indicate the beginning of sequences of adjacent keywords.
Examples:
Search list: ['hello', 'hi', 'welcome', 'greetings', 'hi', 'greetings', 'hey
', 'hello']
Keywords: ['hi', 'hey', 'greetings']. from: 1point3acres.com/bbs
Output: [4]
Search list: ['i', 'saw', 'susie', 'sitting', 'in', 'a', 'shoe', 'shine', '
shop', 'where', 'she'... 阅读全帖

发帖数: 1
19
来自主题: JobHunting版 - 如何O(1)复杂度比较两个Hashmap相等
我觉得可以这样。
把Keywords建踹树。
然后hashmap对keywords统计。
把hashmap里面每个当前的值删了。如果map为空,就记录左指针。
并且把左指针的的值放入map里。
这样就O(n * k)了 k为字符串平均长度。
q*******i
发帖数: 353
20
来自主题: CS版 - 问个JAVA hashMap的问题
用A*search解决15 puzzle的问题。15puzzle有16!不同状态,每个状态我都存在
hashMap中,一个key(double)对应一个puzzle状态(object)。请
问一下,一个hashMap能容纳16!=20922789888000个key及对应的puzzle状态吗?
F****n
发帖数: 3271
21
来自主题: Java版 - HashMap cache
I was not accurate. Same hash codes may not happen but it is possible
that two different hash codes are treated same in a hashmap. If you
look at the implementation of HashMap, it just & the the lower bits of
the hash code.
BTW, off course two strings can have the same hash code. The Hashtable
will use hash code as internal index, and they will do a Object.equals
to check if the key is the same.
For object that qualifies as keys, the only thing that must be assured
is that if two objects pass eq
r*****l
发帖数: 2859
22
来自主题: Java版 - 来问两个HashMap的问题
HashMap是用什么数据结构实现的?
Java怎样serialize一个HashMap?
z*********e
发帖数: 10149
23
来自主题: Java版 - HashMap一问
我有个HashMap hm
如果某个ListNode ln没有被push进这个hashmap,那么hm.get(ln)返回null,这个正常
null != true 得到的结果也是true,没问题
可是hm.get(ln) != true得到是个NullPointerException,而 hm.get(ln) == null返
回结果是true,是正常的
这是为什么?
l*******g
发帖数: 82
24
来自主题: Java版 - HashMap一问
你之所以那个get() != true出现null pointer exception是因为jvm试图先吧返回值转
化成Boolean,所以你就看见了这个exception。

我有个HashMap hm如果某个ListNode ln没有被
push进这个hashmap,那么hm.get........
l******0
发帖数: 244
25
来自主题: Java版 - HashMap 和 Trie
Time complexity, the worst case is O(N) for HashMap and O(K), where N is the
number of entries in the map, and K is the length of the input String S.
What about space complexity? 由于 Trie 可以共享字符串,好像要节省存储空间,
看资料说 trie 比 hashmap 更耗空间。为什么?
c*******e
发帖数: 290
26
来自主题: Java版 - 请教个问题 HashMap
如果用 HashMap,需要存的数目是已知的,比如 10 万条记录。一旦 HashMap 把这 10
万条记录装载后,其内容就不会更改,以后主要是查询操作。为了确保最大的 space
& time efficiency, 是不是把 initialCapacity 刚好设为 10 万,loadFactor=1 最
好?
l*******g
发帖数: 82
27
把数据结构改成更细小粒度。然后sync。
HashMap accounts;
public void put(acctId, money) {
Account acct = accounts.get(acctId);
synchronize(acct) {
...
}
}

前两天面试。模拟一个银行账户,有get(key)和put(key, value)两个method,怎么
implement。我随口说hashmap,然后他说要实现多线程,我就说用........
k**l
发帖数: 2966
28
来自主题: Programming版 - 一个关于unordered_map/hashmap的问题
没在其他语言用过hashmap,想来这个问题应该不限于c++
我知道c++ unordered_map 可以自定义 hash function
hash function 要求要尽量没有collision, 然后最好不太大。
我的问题是多大会太大影响速度,如果我自定义一个hash function把6个大写字母
hash成一个唯一的int (0~2^30的任意数字),这个hashmap岂不需要占据30位的内存空
间,这是不是太大了---还是我理解错了,unordered_map 到底如何占据内存?
多谢
y****n
发帖数: 579
29
来自主题: JobHunting版 - hashmap跟hash table有啥区别?
自己google嘛。
http://www.artima.com/forums/flat.jsp?forum=1&thread=78029
1. Both are classes comes under the util package and both are used to store
keyvalue pair values
2. Hashtable is synchronized and called as legacy classes Hashmap is Non -
synchronized & called as Non Legacy classes
(Serializable: it store the object state in ur machine ok. it similar to the
network layer process. while sending ur data to the other system through
network for avoiding pocket loss we use serialization. this proc
l*****a
发帖数: 14598
30
来自主题: JobHunting版 - HashMap, HashTable and Array 有啥区别
if the input data has fixed scope, u can use array instead of hashmap
hash_map is a C++ template which contains key/value pairs
hashtable is the internal implementation of hash_map
i**********e
发帖数: 1145
31
来自主题: JobHunting版 - HashMap, HashTable and Array 有啥区别
HashMap 是 one to many relationship,hashtable one to one.
看情况,主要 hashtable 和 array 的区别在于前者比较 general:
比如,要计算一个字符串里每个字母的出现次数,用简单的 array 再也适合不过了。
如果只是 ascii 字符,那 256 的大小就够了。
那另外一个例子,如果要计算一本书里每个字的出现次数,那简单的 array 做不到了
。因为 key 是 word,不能直接以 index 来 hash,那就用比较general的hashtable来
做。
还有另一种情况 hashtable 是比 array 好的,就是节省空间。为什么呢?
比方你的 key 的range在于 1 - 1,000,000,但是最多你只会 insert 不超过 100 个
element。那用array来做,那相当于只用数组里的100个,其余的都是空的,非常浪费
空间。但是 hashtable就没有这个问题,因为是 dynamic 的数据结构。
t*****s
发帖数: 124
32
来自主题: JobHunting版 - HashMap, HashTable and Array 有啥区别

这里谈论的HashMap和hashtable概念是什么语言的?
e***l
发帖数: 710
33
来自主题: JobHunting版 - 如何实现hashMap里value的unique
弄个key-value反转的HashMap(v,k)就行啦
c*******u
发帖数: 1657
34
来自主题: JobHunting版 - 怎么实现hashmap?
如何coding实现一个hashmap? 有什么资料参考吗?
c*******u
发帖数: 1657
35
来自主题: JobHunting版 - 怎么实现hashmap?
如何coding实现一个hashmap? 有什么资料参考吗?
w**a
发帖数: 487
36
来自主题: JobHunting版 - 怎么实现hashmap?
有很多人认为hashTable和hashMap比,好像在处理多线程的时候,有安全性问题.
哪位大牛能详细讲讲这个问题?
还有,怎么解决这个问题?
谢谢!
w**z
发帖数: 8232
37
来自主题: JobHunting版 - 怎么实现hashmap?
take a look at concurrent hashmap.
l**h
发帖数: 893
38
好像FB考过,一时找不到了。
一个嵌套Map,就是一个HashMap, 它的value可以是一个element也可以是另外一个
嵌套map或是空的map. 实现一个iterator来遍历这个map里面的所有element。
r**h
发帖数: 1288
39
在C++中,set/map是用的红黑树,unordered_set/unordered_map使用的线性表+hash函数
那么java里面的Set、HashSet和HashMap的内部实现是什么呢?
b*****n
发帖数: 618
40
Set是个interface
HashSet和HashMap实现方法是一样的,hash function + linkedlist 处理collision
z*******3
发帖数: 13709
41
来自主题: JobHunting版 - 多线程hashmap。 实现方法讨论
这题考类库
实际工作中没有人自己去实现
都是用concurrenthashmap
1.5的new feature
再往前一点这题是hashtable vs hashmap
这几个应该算是java里面最常见的工具类了
c***G
发帖数: 88
42
来自主题: JobHunting版 - 请教个面试题, tree和hashmap的区别
tree和hashmap有啥区别?有啥优缺点?什么情况下用哪一个?
我已经多次碰到过这个问题了,有次就这个问题被问了20多分钟,实在是不知道要怎么
回答了。。。
各位大牛能给点建议么?
谢谢!
z****e
发帖数: 54598
43
来自主题: JobHunting版 - 请教个面试题, tree和hashmap的区别
java吧
其它语言hashtable比较多
java里面对比hashmap vs treemap
你google一下,可以看到很多
f*******w
发帖数: 1243
44
来自主题: JobHunting版 - 请教个面试题, tree和hashmap的区别
个人感觉两点:一是复杂度,O(1)和O(logn)的区别; 二是tree结构通常是说BST,所以
整体是排好序的,而hashmap不是。所以当你需要in order输出,或者找到前一个/后一
个element的时候用tree比hash好。
反正我面试的时候碰到这题回答这两点一般就move on了……
g********r
发帖数: 89
45
来自主题: JobHunting版 - Isomorphic Strings 的单Hashmap解法
given "foo", "app"; returns true we can map f -> a and o->p
given "bar", "foo"; returns false we can't map both 'a' and 'r' to 'o'
given "ab", "ca"; returns true we can map 'a' -> 'b' and 'c' -> 'a'
记得lolhaha大牛说过一个hashmap的解法,具体怎么做呢?
v****a
发帖数: 236
46
来自主题: JobHunting版 - Isomorphic Strings 的单Hashmap解法
public static boolean isIsomorphic(String inputString1, String inputString2)
{
int length1 = inputString1.length();
int length2 = inputString2.length();
if (length1 != length2) {
return false;
}
if (length1 == 1) {
return true;
}
Map map = new HashMap();
for (int i = 0; i < length1; i++) {
if (!map.containsKey(inputString1.charAt(i)) && !map.
containsVal... 阅读全帖
v****a
发帖数: 236
47
来自主题: JobHunting版 - Isomorphic Strings 的单Hashmap解法
这个是false吧?
我也发现containsValue 不是O(1)了。。不知道怎么写是单HashMap的O(N)
S**********n
发帖数: 250
48
你的map 是 HashMap
Integer 和 int是不一样的
map.get(s_char) == 128 // true 正确写法
map.get(s_char).equals(map.get(t_char)) // true, 正确写法
map.get(s_char) == map.get(t_char) // 有时候true,有时候false;错误写法

get(
s
P******r
发帖数: 1342
49
2楼不对,== 的 比较不调 equals。
5楼是对的,HashMap里面必然是用Integer而不是int。所以用==可能会有问题
b***e
发帖数: 383
50
来自主题: JobHunting版 - 问一个hashtable/hashmap capacity的问题
Where did you get that statement?
Below is the original text from the Java API Document.
"As a general rule, the default load factor (.75) offers a good tradeoff
between time and space costs. Higher values decrease the space overhead but
increase the lookup cost (reflected in most of the operations of the HashMap
class, including get and put)."
So, higher values refer to load factor, not capacity.
1 2 3 4 5 6 7 8 9 10 下页 末页 (共10页)