Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
e7584ab3
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
3
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,发现更多精彩内容 >>
提交
e7584ab3
编写于
1月 24, 2014
作者:
P
psandoz
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
8030848: Collections.sort(List l, Comparator) should defer to List.sort(Comparator )
Reviewed-by: mduigou
上级
0e9a4d8f
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
85 addition
and
94 deletion
+85
-94
src/share/classes/java/util/Arrays.java
src/share/classes/java/util/Arrays.java
+18
-14
src/share/classes/java/util/Collections.java
src/share/classes/java/util/Collections.java
+17
-75
src/share/classes/java/util/List.java
src/share/classes/java/util/List.java
+50
-5
未找到文件。
src/share/classes/java/util/Arrays.java
浏览文件 @
e7584ab3
/*
/*
* Copyright (c) 1997, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 201
4
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -1427,12 +1427,14 @@ public class Arrays {
...
@@ -1427,12 +1427,14 @@ public class Arrays {
* found to violate the {@link Comparator} contract
* found to violate the {@link Comparator} contract
*/
*/
public
static
<
T
>
void
sort
(
T
[]
a
,
Comparator
<?
super
T
>
c
)
{
public
static
<
T
>
void
sort
(
T
[]
a
,
Comparator
<?
super
T
>
c
)
{
if
(
c
==
null
)
if
(
c
==
null
)
{
c
=
NaturalOrder
.
INSTANCE
;
sort
(
a
);
if
(
LegacyMergeSort
.
userRequested
)
}
else
{
legacyMergeSort
(
a
,
c
);
if
(
LegacyMergeSort
.
userRequested
)
else
legacyMergeSort
(
a
,
c
);
TimSort
.
sort
(
a
,
0
,
a
.
length
,
c
,
null
,
0
,
0
);
else
TimSort
.
sort
(
a
,
0
,
a
.
length
,
c
,
null
,
0
,
0
);
}
}
}
/** To be removed in a future release. */
/** To be removed in a future release. */
...
@@ -1498,13 +1500,15 @@ public class Arrays {
...
@@ -1498,13 +1500,15 @@ public class Arrays {
*/
*/
public
static
<
T
>
void
sort
(
T
[]
a
,
int
fromIndex
,
int
toIndex
,
public
static
<
T
>
void
sort
(
T
[]
a
,
int
fromIndex
,
int
toIndex
,
Comparator
<?
super
T
>
c
)
{
Comparator
<?
super
T
>
c
)
{
if
(
c
==
null
)
if
(
c
==
null
)
{
c
=
NaturalOrder
.
INSTANCE
;
sort
(
a
,
fromIndex
,
toIndex
);
rangeCheck
(
a
.
length
,
fromIndex
,
toIndex
);
}
else
{
if
(
LegacyMergeSort
.
userRequested
)
rangeCheck
(
a
.
length
,
fromIndex
,
toIndex
);
legacyMergeSort
(
a
,
fromIndex
,
toIndex
,
c
);
if
(
LegacyMergeSort
.
userRequested
)
else
legacyMergeSort
(
a
,
fromIndex
,
toIndex
,
c
);
TimSort
.
sort
(
a
,
fromIndex
,
toIndex
,
c
,
null
,
0
,
0
);
else
TimSort
.
sort
(
a
,
fromIndex
,
toIndex
,
c
,
null
,
0
,
0
);
}
}
}
/** To be removed in a future release. */
/** To be removed in a future release. */
...
...
src/share/classes/java/util/Collections.java
浏览文件 @
e7584ab3
/*
/*
* Copyright (c) 1997, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 201
4
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -121,34 +121,9 @@ public class Collections {
...
@@ -121,34 +121,9 @@ public class Collections {
*
*
* <p>The specified list must be modifiable, but need not be resizable.
* <p>The specified list must be modifiable, but need not be resizable.
*
*
* <p>Implementation note: This implementation is a stable, adaptive,
* @implNote
* iterative mergesort that requires far fewer than n lg(n) comparisons
* This implementation defers to the {@link List#sort(Comparator)}
* when the input array is partially sorted, while offering the
* method using the specified list and a {@code null} comparator.
* performance of a traditional mergesort when the input array is
* randomly ordered. If the input array is nearly sorted, the
* implementation requires approximately n comparisons. Temporary
* storage requirements vary from a small constant for nearly sorted
* input arrays to n/2 object references for randomly ordered input
* arrays.
*
* <p>The implementation takes equal advantage of ascending and
* descending order in its input array, and can take advantage of
* ascending and descending order in different parts of the same
* input array. It is well-suited to merging two or more sorted arrays:
* simply concatenate the arrays and sort the resulting array.
*
* <p>The implementation was adapted from Tim Peters's list sort for Python
* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic
* Sorting and Information Theoretic Complexity", in Proceedings of the
* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
* January 1993.
*
* <p>This implementation dumps the specified list into an array, sorts
* the array, and iterates over the list resetting each element
* from the corresponding position in the array. This avoids the
* n<sup>2</sup> log(n) performance that would result from attempting
* to sort a linked list in place.
*
*
* @param <T> the class of the objects in the list
* @param <T> the class of the objects in the list
* @param list the list to be sorted.
* @param list the list to be sorted.
...
@@ -159,16 +134,11 @@ public class Collections {
...
@@ -159,16 +134,11 @@ public class Collections {
* @throws IllegalArgumentException (optional) if the implementation
* @throws IllegalArgumentException (optional) if the implementation
* detects that the natural ordering of the list elements is
* detects that the natural ordering of the list elements is
* found to violate the {@link Comparable} contract
* found to violate the {@link Comparable} contract
* @see List#sort(Comparator)
*/
*/
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
public
static
<
T
extends
Comparable
<?
super
T
>>
void
sort
(
List
<
T
>
list
)
{
public
static
<
T
extends
Comparable
<?
super
T
>>
void
sort
(
List
<
T
>
list
)
{
Object
[]
a
=
list
.
toArray
();
list
.
sort
(
null
);
Arrays
.
sort
(
a
);
ListIterator
<
T
>
i
=
list
.
listIterator
();
for
(
int
j
=
0
;
j
<
a
.
length
;
j
++)
{
i
.
next
();
i
.
set
((
T
)
a
[
j
]);
}
}
}
/**
/**
...
@@ -183,34 +153,9 @@ public class Collections {
...
@@ -183,34 +153,9 @@ public class Collections {
*
*
* <p>The specified list must be modifiable, but need not be resizable.
* <p>The specified list must be modifiable, but need not be resizable.
*
*
* <p>Implementation note: This implementation is a stable, adaptive,
* @implNote
* iterative mergesort that requires far fewer than n lg(n) comparisons
* This implementation defers to the {@link List#sort(Comparator)}
* when the input array is partially sorted, while offering the
* method using the specified list and comparator.
* performance of a traditional mergesort when the input array is
* randomly ordered. If the input array is nearly sorted, the
* implementation requires approximately n comparisons. Temporary
* storage requirements vary from a small constant for nearly sorted
* input arrays to n/2 object references for randomly ordered input
* arrays.
*
* <p>The implementation takes equal advantage of ascending and
* descending order in its input array, and can take advantage of
* ascending and descending order in different parts of the same
* input array. It is well-suited to merging two or more sorted arrays:
* simply concatenate the arrays and sort the resulting array.
*
* <p>The implementation was adapted from Tim Peters's list sort for Python
* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic
* Sorting and Information Theoretic Complexity", in Proceedings of the
* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
* January 1993.
*
* <p>This implementation dumps the specified list into an array, sorts
* the array, and iterates over the list resetting each element
* from the corresponding position in the array. This avoids the
* n<sup>2</sup> log(n) performance that would result from attempting
* to sort a linked list in place.
*
*
* @param <T> the class of the objects in the list
* @param <T> the class of the objects in the list
* @param list the list to be sorted.
* @param list the list to be sorted.
...
@@ -223,16 +168,11 @@ public class Collections {
...
@@ -223,16 +168,11 @@ public class Collections {
* list-iterator does not support the {@code set} operation.
* list-iterator does not support the {@code set} operation.
* @throws IllegalArgumentException (optional) if the comparator is
* @throws IllegalArgumentException (optional) if the comparator is
* found to violate the {@link Comparator} contract
* found to violate the {@link Comparator} contract
* @see List#sort(Comparator)
*/
*/
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
public
static
<
T
>
void
sort
(
List
<
T
>
list
,
Comparator
<?
super
T
>
c
)
{
public
static
<
T
>
void
sort
(
List
<
T
>
list
,
Comparator
<?
super
T
>
c
)
{
Object
[]
a
=
list
.
toArray
();
list
.
sort
(
c
);
Arrays
.
sort
(
a
,
(
Comparator
)
c
);
ListIterator
<
T
>
i
=
list
.
listIterator
();
for
(
int
j
=
0
;
j
<
a
.
length
;
j
++)
{
i
.
next
();
i
.
set
((
T
)
a
[
j
]);
}
}
}
...
@@ -4464,10 +4404,12 @@ public class Collections {
...
@@ -4464,10 +4404,12 @@ public class Collections {
* <pre>
* <pre>
* List<String> s = Collections.emptyList();
* List<String> s = Collections.emptyList();
* </pre>
* </pre>
* Implementation note: Implementations of this method need not
*
* create a separate <tt>List</tt> object for each call. Using this
* @implNote
* method is likely to have comparable cost to using the like-named
* Implementations of this method need not create a separate <tt>List</tt>
* field. (Unlike this method, the field does not provide type safety.)
* object for each call. Using this method is likely to have comparable
* cost to using the like-named field. (Unlike this method, the field does
* not provide type safety.)
*
*
* @param <T> type of elements, if there were any, in the list
* @param <T> type of elements, if there were any, in the list
* @return an empty immutable list
* @return an empty immutable list
...
...
src/share/classes/java/util/List.java
浏览文件 @
e7584ab3
/*
/*
* Copyright (c) 1997, 201
3
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 201
4
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
*
* This code is free software; you can redistribute it and/or modify it
* This code is free software; you can redistribute it and/or modify it
...
@@ -415,11 +415,49 @@ public interface List<E> extends Collection<E> {
...
@@ -415,11 +415,49 @@ public interface List<E> extends Collection<E> {
}
}
/**
/**
* Sorts this list using the supplied {@code Comparator} to compare elements.
* Sorts this list according to the order induced by the specified
* {@link Comparator}.
*
* <p>All elements in this list must be <i>mutually comparable</i> using the
* specified comparator (that is, {@code c.compare(e1, e2)} must not throw
* a {@code ClassCastException} for any elements {@code e1} and {@code e2}
* in the list).
*
* <p>If the specified comparator is {@code null} then all elements in this
* list must implement the {@link Comparable} interface and the elements'
* {@linkplain Comparable natural ordering} should be used.
*
* <p>This list must be modifiable, but need not be resizable.
*
*
* @implSpec
* @implSpec
* The default implementation is equivalent to, for this {@code list}:
* The default implementation obtains an array containing all elements in
* <pre>Collections.sort(list, c)</pre>
* this list, sorts the array, and iterates over this list resetting each
* element from the corresponding position in the array. (This avoids the
* n<sup>2</sup> log(n) performance that would result from attempting
* to sort a linked list in place.)
*
* @implNote
* This implementation is a stable, adaptive, iterative mergesort that
* requires far fewer than n lg(n) comparisons when the input array is
* partially sorted, while offering the performance of a traditional
* mergesort when the input array is randomly ordered. If the input array
* is nearly sorted, the implementation requires approximately n
* comparisons. Temporary storage requirements vary from a small constant
* for nearly sorted input arrays to n/2 object references for randomly
* ordered input arrays.
*
* <p>The implementation takes equal advantage of ascending and
* descending order in its input array, and can take advantage of
* ascending and descending order in different parts of the same
* input array. It is well-suited to merging two or more sorted arrays:
* simply concatenate the arrays and sort the resulting array.
*
* <p>The implementation was adapted from Tim Peters's list sort for Python
* (<a href="http://svn.python.org/projects/python/trunk/Objects/listsort.txt">
* TimSort</a>). It uses techniques from Peter McIlroy's "Optimistic
* Sorting and Information Theoretic Complexity", in Proceedings of the
* Fourth Annual ACM-SIAM Symposium on Discrete Algorithms, pp 467-474,
* January 1993.
*
*
* @param c the {@code Comparator} used to compare list elements.
* @param c the {@code Comparator} used to compare list elements.
* A {@code null} value indicates that the elements'
* A {@code null} value indicates that the elements'
...
@@ -434,8 +472,15 @@ public interface List<E> extends Collection<E> {
...
@@ -434,8 +472,15 @@ public interface List<E> extends Collection<E> {
* contract
* contract
* @since 1.8
* @since 1.8
*/
*/
@SuppressWarnings
({
"unchecked"
,
"rawtypes"
})
default
void
sort
(
Comparator
<?
super
E
>
c
)
{
default
void
sort
(
Comparator
<?
super
E
>
c
)
{
Collections
.
sort
(
this
,
c
);
Object
[]
a
=
this
.
toArray
();
Arrays
.
sort
(
a
,
(
Comparator
)
c
);
ListIterator
<
E
>
i
=
this
.
listIterator
();
for
(
Object
e
:
a
)
{
i
.
next
();
i
.
set
((
E
)
e
);
}
}
}
/**
/**
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录