Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
1bfce010
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1bfce010
编写于
12月 17, 2013
作者:
M
mduigou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8029795: LinkedHashMap.getOrDefault() doesn't update access order.
Reviewed-by: psandoz
上级
67683e09
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
106 addition
and
28 deletion
+106
-28
src/share/classes/java/util/LinkedHashMap.java
src/share/classes/java/util/LinkedHashMap.java
+26
-13
test/java/util/LinkedHashMap/Basic.java
test/java/util/LinkedHashMap/Basic.java
+80
-15
未找到文件。
src/share/classes/java/util/LinkedHashMap.java
浏览文件 @
1bfce010
...
...
@@ -28,7 +28,6 @@ package java.util;
import
java.util.function.Consumer
;
import
java.util.function.BiConsumer
;
import
java.util.function.BiFunction
;
import
java.io.Serializable
;
import
java.io.IOException
;
/**
...
...
@@ -63,14 +62,17 @@ import java.io.IOException;
* provided to create a linked hash map whose order of iteration is the order
* in which its entries were last accessed, from least-recently accessed to
* most-recently (<i>access-order</i>). This kind of map is well-suited to
* building LRU caches. Invoking the <tt>put</tt> or <tt>get</tt> method
* results in an access to the corresponding entry (assuming it exists after
* the invocation completes). The <tt>putAll</tt> method generates one entry
* access for each mapping in the specified map, in the order that key-value
* mappings are provided by the specified map's entry set iterator. <i>No
* other methods generate entry accesses.</i> In particular, operations on
* collection-views do <i>not</i> affect the order of iteration of the backing
* map.
* building LRU caches. Invoking the {@code put}, {@code putIfAbsent},
* {@code get}, {@code getOrDefault}, {@code compute}, {@code computeIfAbsent},
* {@code computeIfPresent}, or {@code merge} methods results
* in an access to the corresponding entry (assuming it exists after the
* invocation completes). The {@code replace} methods only result in an access
* of the entry if the value is replaced. The {@code putAll} method generates one
* entry access for each mapping in the specified map, in the order that
* key-value mappings are provided by the specified map's entry set iterator.
* <i>No other methods generate entry accesses.</i> In particular, operations
* on collection-views do <i>not</i> affect the order of iteration of the
* backing map.
*
* <p>The {@link #removeEldestEntry(Map.Entry)} method may be overridden to
* impose a policy for removing stale mappings automatically when new mappings
...
...
@@ -112,8 +114,8 @@ import java.io.IOException;
* iteration order. In insertion-ordered linked hash maps, merely changing
* the value associated with a key that is already contained in the map is not
* a structural modification. <strong>In access-ordered linked hash maps,
* merely querying the map with <tt>get</tt> is a structural
*
modification.
</strong>)
* merely querying the map with <tt>get</tt> is a structural
modification.
* </strong>)
*
* <p>The iterators returned by the <tt>iterator</tt> method of the collections
* returned by all of this class's collection view methods are
...
...
@@ -443,8 +445,19 @@ public class LinkedHashMap<K,V>
}
/**
* Removes all of the mappings from this map.
* The map will be empty after this call returns.
* {@inheritDoc}
*/
public
V
getOrDefault
(
Object
key
,
V
defaultValue
)
{
Node
<
K
,
V
>
e
;
if
((
e
=
getNode
(
hash
(
key
),
key
))
==
null
)
return
defaultValue
;
if
(
accessOrder
)
afterNodeAccess
(
e
);
return
e
.
value
;
}
/**
* {@inheritDoc}
*/
public
void
clear
()
{
super
.
clear
();
...
...
test/java/util/LinkedHashMap/Basic.java
浏览文件 @
1bfce010
...
...
@@ -23,28 +23,29 @@
/**
* @test
* @bug 4245809
* @bug 4245809
8029795
* @summary Basic test for LinkedHashMap. (Based on MapBash)
*/
import
java.util.*
;
import
java.util.function.*
;
import
java.io.*
;
public
class
Basic
{
static
Random
rnd
=
new
Random
(
666
);
static
Object
nil
=
new
Integer
(
0
);
final
static
Random
rnd
=
new
Random
(
666
);
final
static
Integer
nil
=
new
Integer
(
0
);
public
static
void
main
(
String
[]
args
)
throws
Exception
{
int
numItr
=
500
;
int
mapSize
=
500
;
// Linked List test
// Linked List test
k
for
(
int
i
=
0
;
i
<
numItr
;
i
++)
{
Map
m
=
new
LinkedHashMap
();
Object
head
=
nil
;
Map
<
Integer
,
Integer
>
m
=
new
LinkedHashMap
();
Integer
head
=
nil
;
for
(
int
j
=
0
;
j
<
mapSize
;
j
++)
{
Object
newHead
;
Integer
newHead
;
do
{
newHead
=
new
Integer
(
rnd
.
nextInt
());
}
while
(
m
.
containsKey
(
newHead
));
...
...
@@ -57,7 +58,7 @@ public class Basic {
if
(
new
HashMap
(
m
).
hashCode
()
!=
m
.
hashCode
())
throw
new
Exception
(
"Incorrect hashCode computation."
);
Map
m2
=
new
LinkedHashMap
();
m2
.
putAll
(
m
);
Map
<
Integer
,
Integer
>
m2
=
new
LinkedHashMap
();
m2
.
putAll
(
m
);
m2
.
values
().
removeAll
(
m
.
keySet
());
if
(
m2
.
size
()!=
1
||
!
m2
.
containsValue
(
nil
))
throw
new
Exception
(
"Collection views test failed."
);
...
...
@@ -66,7 +67,7 @@ public class Basic {
while
(
head
!=
nil
)
{
if
(!
m
.
containsKey
(
head
))
throw
new
Exception
(
"Linked list doesn't contain a link."
);
Object
newHead
=
m
.
get
(
head
);
Integer
newHead
=
m
.
get
(
head
);
if
(
newHead
==
null
)
throw
new
Exception
(
"Could not retrieve a link."
);
m
.
remove
(
head
);
...
...
@@ -79,7 +80,7 @@ public class Basic {
throw
new
Exception
(
"Linked list size not as expected."
);
}
Map
m
=
new
LinkedHashMap
();
Map
<
Integer
,
Integer
>
m
=
new
LinkedHashMap
();
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
if
(
m
.
put
(
new
Integer
(
i
),
new
Integer
(
2
*
i
))
!=
null
)
throw
new
Exception
(
"put returns non-null value erroenously."
);
...
...
@@ -88,12 +89,12 @@ public class Basic {
throw
new
Exception
(
"contains value "
+
i
);
if
(
m
.
put
(
nil
,
nil
)
==
null
)
throw
new
Exception
(
"put returns a null value erroenously."
);
Map
m2
=
new
LinkedHashMap
();
m2
.
putAll
(
m
);
Map
<
Integer
,
Integer
>
m2
=
new
LinkedHashMap
();
m2
.
putAll
(
m
);
if
(!
m
.
equals
(
m2
))
throw
new
Exception
(
"Clone not equal to original. (1)"
);
if
(!
m2
.
equals
(
m
))
throw
new
Exception
(
"Clone not equal to original. (2)"
);
Set
s
=
m
.
entrySet
(),
s2
=
m2
.
entrySet
();
Set
<
Map
.
Entry
<
Integer
,
Integer
>>
s
=
m
.
entrySet
(),
s2
=
m2
.
entrySet
();
if
(!
s
.
equals
(
s2
))
throw
new
Exception
(
"Clone not equal to original. (3)"
);
if
(!
s2
.
equals
(
s
))
...
...
@@ -137,7 +138,7 @@ public class Basic {
// Test ordering properties with insert order
m
=
new
LinkedHashMap
();
List
l
=
new
ArrayList
(
mapSize
);
List
<
Integer
>
l
=
new
ArrayList
(
mapSize
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
new
Integer
(
i
);
m
.
put
(
x
,
x
);
...
...
@@ -164,7 +165,7 @@ public class Basic {
if
(!
m
.
equals
(
m2
))
throw
new
Exception
(
"Insert-order Map != clone."
);
List
l2
=
new
ArrayList
(
l
);
List
<
Integer
>
l2
=
new
ArrayList
(
l
);
Collections
.
shuffle
(
l2
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
...
...
@@ -175,7 +176,7 @@ public class Basic {
throw
new
Exception
(
"Clone: altered by read."
);
// Test ordering properties with access order
m
=
new
LinkedHashMap
(
1000
,
.
75
f
,
true
);
m
=
new
LinkedHashMap
(
2
*
mapSize
,
.
75
f
,
true
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
new
Integer
(
i
);
m
.
put
(
x
,
x
);
...
...
@@ -191,6 +192,70 @@ public class Basic {
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by read."
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
if
(!
m
.
getOrDefault
(
x
,
new
Integer
(
i
+
1000
)).
equals
(
x
))
throw
new
Exception
(
"Wrong value: "
+
i
+
", "
+
m
.
get
(
x
)+
", "
+
x
);
}
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by read."
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
if
(!
m
.
replace
(
x
,
x
).
equals
(
x
))
throw
new
Exception
(
"Wrong value: "
+
i
+
", "
+
m
.
get
(
x
)+
", "
+
x
);
}
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by replace."
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
if
(!
m
.
replace
(
x
,
x
,
x
))
throw
new
Exception
(
"Wrong value: "
+
i
+
", "
+
m
.
get
(
x
)+
", "
+
x
);
}
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by replace."
);
BiFunction
<
Integer
,
Integer
,
Integer
>
f
=
(
Integer
y
,
Integer
z
)
->
{
if
(!
Objects
.
equals
(
y
,
z
))
throw
new
RuntimeException
(
"unequal "
+
y
+
","
+
z
);
return
new
Integer
(
z
);
};
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
if
(!
x
.
equals
(
m
.
merge
(
x
,
x
,
f
)))
throw
new
Exception
(
"Wrong value: "
+
i
+
", "
+
m
.
get
(
x
)+
", "
+
x
);
}
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by replace."
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
if
(!
x
.
equals
(
m
.
compute
(
x
,
f
)))
throw
new
Exception
(
"Wrong value: "
+
i
+
", "
+
m
.
get
(
x
)+
", "
+
x
);
}
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by replace."
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
if
(!
x
.
equals
(
m
.
remove
(
x
)))
throw
new
Exception
(
"Missing key: "
+
i
+
", "
+
x
);
if
(!
x
.
equals
(
m
.
computeIfAbsent
(
x
,
Integer:
:
valueOf
)))
throw
new
Exception
(
"Wrong value: "
+
i
+
", "
+
m
.
get
(
x
)+
", "
+
x
);
}
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by replace."
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
(
Integer
)
l2
.
get
(
i
);
if
(!
x
.
equals
(
m
.
computeIfPresent
(
x
,
f
)))
throw
new
Exception
(
"Wrong value: "
+
i
+
", "
+
m
.
get
(
x
)+
", "
+
x
);
}
if
(!
new
ArrayList
(
m
.
keySet
()).
equals
(
l2
))
throw
new
Exception
(
"Insert order not properly altered by replace."
);
for
(
int
i
=
0
;
i
<
mapSize
;
i
++)
{
Integer
x
=
new
Integer
(
i
);
m
.
put
(
x
,
x
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录