在jdk 1.6中比较HashMap
和Hashtable
源代码,我在HashMap中看到了下面的代码
/**
* The default initial capacity - MUST be a power of two.
*/
static final int DEFAULT_INITIAL_CAPACITY = 16;
int capacity = 1;
while (capacity < initialCapacity)
capacity <<= 1;
Run Code Online (Sandbox Code Playgroud)
但是,在Hashtable中,我看到下面的代码?
table = new Entry[initialCapacity];
public Hashtable() {
this(11, 0.75f);
}
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:为什么hashMap需要2的幂作为初始容量?而哈希表选择11作为默认初始容量?我认为这与哈希表是线程安全并且不允许空键或值的事情无关.
谢谢.
为什么String.hashcode()有这么多冲突?
我在jdk1.6中读取String.hashCode(),下面是代码
public int hashCode() {
int h = hash;
if (h == 0) {
int off = offset;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
hash = h;
}
return h;
}
Run Code Online (Sandbox Code Playgroud)
这对我来说很混乱,因为它有很多冲突; 虽然它不需要是唯一的(我们仍然可以依赖于equals()),但是更少的冲突意味着更好的性能而无需访问链表中的条目.
假设我们有两个字符,那么只要我们找到两个匹配下面的方程的字符串,那么我们就会有相同的hashcode()
a * 31 +b = c * 31 +d
Run Code Online (Sandbox Code Playgroud)
很容易得出结论,(a-c) * 31 = d-b
一个简单的例子是make ac = 1和db = 31; 所以我写下面的代码进行简单的测试
public void testHash() {
System.out.println("A:" …
Run Code Online (Sandbox Code Playgroud) 我试着用JUnit测试写一个蚂蚁,但得到以下结果:
unittest:
[junit] Running com.mytest.utiltest
[junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
[junit] Test com.mytest.utiltest FAILED
Run Code Online (Sandbox Code Playgroud)
它只显示没有打印细节的错误,我在build.xml中指定下面的参数也尝试开始ant -v or ant -debug
,但没有得到任何运气.有人可以帮忙吗?
<junit printsummary="yes" showoutput="true">
Run Code Online (Sandbox Code Playgroud)
ant 1.8.2,sun jdk1.6.0_20,junit 4.8.2
为了缩小问题范围,我创建了一个单独的项目,这是我的build.xml
<project name = "TestPrj" default="unittest" basedir = ".">
<target name="unittest" >
<junit printsummary="yes" showoutput="true" >
<classpath>
<pathelement location="./junit-4.8.2.jar"/>
<pathelement location="./ant-junit4.jar"/>
</classpath>
<test name = "com.mytest.unittest.SimpleTest" todir="."/>
</junit>
</target>
</project>
Run Code Online (Sandbox Code Playgroud)
下面是simpletest.java
package com.mytest.unittest;
import junit.framework.TestCase;
public class SimpleTest extends TestCase{
public void testFirst()
{
assertTrue(true); …
Run Code Online (Sandbox Code Playgroud) 对于java web应用程序,通常我们需要在前端使用javascript进行验证,然后在后端使用java进行验证,后端可以使用一些java验证工具,如hibernate验证器,而在客户端则有jquery表单验证,
但问题是,有没有更简单的方法将两者结合起来?例如,当使用带有hiberate验证器的springmvc时,前端验证会自动出现吗?谢谢
按地址,我只指国家,州,城市,地区,街道,建筑物,
在哪里可以有效地在其他表格(例如人员)中引用该地址,以便我们可以选择同一城市等的人员?谢谢。
我使用Firefox 8.0.1
margin-top
没有按预期工作,有人可以帮忙吗?谢谢.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>New Border</title>
<style type="text/css">
#page {
width:980px;
margin: 50px auto;
}
#board {
width:600px;
height:400px;
background-color: gray;
}
.cell {
border: 1px solid;
width:50px;
height:20px;
margin: 30px 5px;
}
</style>
</head>
<body>
<div id="page">
<div id="board">
<div class="cell">
</div>
</div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
所以,margin-top
在cell
div中没有按预期工作.
我认为应该将div margin-left
放在cell
div之下board
,但事实并非如此.
这是截图:
我在Windows XP上的Hotspot JDK 1.6中运行下面的代码,我运行了两次,我得到了下面的结果.
所以基本上似乎object.hashcode()
也有冲突?看起来它没有返回VM中的内存地址.
但是,JDK中的一条评论说价值观应该是截然不同的,任何人都可以解释一下吗?
尽可能合理,Object类定义的hashCode方法确实为不同的对象返回不同的整数.(这通常通过将对象的内部地址转换为整数来实现,但Java TM编程语言不需要此实现技术.)
Run Code Online (Sandbox Code Playgroud)@return a hash code value for this object. @see java.lang.Object#equals(java.lang.Object) @see java.util.Hashtable
这是第一个结果:
i,hashcode(): 361,9578500
i,hashcode(): 1886,9578500
conflict:1886, 361
i,hashcode(): 1905,14850080
i,hashcode(): 2185,14850080
conflict:2185, 1905
9998
Run Code Online (Sandbox Code Playgroud)
这是第二个结果:
i,hashcode(): 361,5462872
i,hashcode(): 1886,29705835
conflict:1887, 362
i,hashcode(): 1905,9949222
i,hashcode(): 2185,2081190
conflict:2186, 1906
9998
10000
Run Code Online (Sandbox Code Playgroud)
我的代码:
@Test
public void testAddr()
{
Set<Integer> s = new TreeSet<Integer>();
Map<Integer, Integer> m = new TreeMap<Integer, Integer>();
Set<Object> os = new HashSet<Object>();
for(int i = 0; i …
Run Code Online (Sandbox Code Playgroud) java ×5
hashcode ×2
ant ×1
css ×1
database ×1
firefox ×1
hashmap ×1
hashtable ×1
html ×1
javascript ×1
junit ×1
margin ×1
spring-mvc ×1
string ×1
unit-testing ×1