Skip to content

Commit ae0e76d

Browse files
committed
8301120: Cleanup utility classes java.util.Arrays and java.util.Collections
Reviewed-by: smarks, darcy
1 parent b8e5abc commit ae0e76d

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

src/java.base/share/classes/java/util/Arrays.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
package java.util;
2727

28-
import jdk.internal.misc.Unsafe;
2928
import jdk.internal.util.ArraysSupport;
3029
import jdk.internal.vm.annotation.IntrinsicCandidate;
3130

@@ -2507,7 +2506,7 @@ public static boolean equals(int[] a, int aFromIndex, int aToIndex,
25072506
* @param a2 the other array to be tested for equality
25082507
* @return {@code true} if the two arrays are equal
25092508
*/
2510-
public static boolean equals(short[] a, short a2[]) {
2509+
public static boolean equals(short[] a, short[] a2) {
25112510
if (a==a2)
25122511
return true;
25132512
if (a==null || a2==null)

src/java.base/share/classes/java/util/Collections.java

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ private Collections() {
141141
* found to violate the {@link Comparable} contract
142142
* @see List#sort(Comparator)
143143
*/
144-
@SuppressWarnings("unchecked")
145144
public static <T extends Comparable<? super T>> void sort(List<T> list) {
146145
list.sort(null);
147146
}
@@ -175,7 +174,6 @@ public static <T extends Comparable<? super T>> void sort(List<T> list) {
175174
* found to violate the {@link Comparator} contract
176175
* @see List#sort(Comparator)
177176
*/
178-
@SuppressWarnings({"unchecked", "rawtypes"})
179177
public static <T> void sort(List<T> list, Comparator<? super T> c) {
180178
list.sort(c);
181179
}
@@ -267,7 +265,7 @@ else if (cmp > 0)
267265
* list listIterator.
268266
*/
269267
private static <T> T get(ListIterator<? extends T> i, int index) {
270-
T obj = null;
268+
T obj;
271269
int pos = i.nextIndex();
272270
if (pos <= index) {
273271
do {
@@ -651,10 +649,10 @@ public static <T extends Object & Comparable<? super T>> T min(Collection<? exte
651649
* @throws NoSuchElementException if the collection is empty.
652650
* @see Comparable
653651
*/
654-
@SuppressWarnings({"unchecked", "rawtypes"})
652+
@SuppressWarnings({"unchecked"})
655653
public static <T> T min(Collection<? extends T> coll, Comparator<? super T> comp) {
656654
if (comp==null)
657-
return (T)min((Collection) coll);
655+
return (T)min((Collection<Comparable<Object>>) coll);
658656

659657
Iterator<? extends T> i = coll.iterator();
660658
T candidate = i.next();
@@ -724,10 +722,10 @@ public static <T extends Object & Comparable<? super T>> T max(Collection<? exte
724722
* @throws NoSuchElementException if the collection is empty.
725723
* @see Comparable
726724
*/
727-
@SuppressWarnings({"unchecked", "rawtypes"})
725+
@SuppressWarnings({"unchecked"})
728726
public static <T> T max(Collection<? extends T> coll, Comparator<? super T> comp) {
729727
if (comp==null)
730-
return (T)max((Collection) coll);
728+
return (T)max((Collection<Comparable<Object>>) coll);
731729

732730
Iterator<? extends T> i = coll.iterator();
733731
T candidate = i.next();
@@ -1067,7 +1065,7 @@ static class UnmodifiableCollection<E> implements Collection<E>, Serializable {
10671065
public String toString() {return c.toString();}
10681066

10691067
public Iterator<E> iterator() {
1070-
return new Iterator<E>() {
1068+
return new Iterator<>() {
10711069
private final Iterator<? extends E> i = c.iterator();
10721070

10731071
public boolean hasNext() {return i.hasNext();}
@@ -1279,7 +1277,6 @@ public EmptyNavigableSet() {
12791277
private Object readResolve() { return EMPTY_NAVIGABLE_SET; }
12801278
}
12811279

1282-
@SuppressWarnings("rawtypes")
12831280
private static final NavigableSet<?> EMPTY_NAVIGABLE_SET =
12841281
new EmptyNavigableSet<>();
12851282

@@ -1392,7 +1389,7 @@ public void sort(Comparator<? super E> c) {
13921389
public ListIterator<E> listIterator() {return listIterator(0);}
13931390

13941391
public ListIterator<E> listIterator(final int index) {
1395-
return new ListIterator<E>() {
1392+
return new ListIterator<>() {
13961393
private final ListIterator<? extends E> i
13971394
= list.listIterator(index);
13981395

@@ -1634,10 +1631,9 @@ static class UnmodifiableEntrySet<K,V>
16341631
@java.io.Serial
16351632
private static final long serialVersionUID = 7854390611657943733L;
16361633

1637-
@SuppressWarnings({"unchecked", "rawtypes"})
1634+
@SuppressWarnings({"unchecked"})
16381635
UnmodifiableEntrySet(Set<? extends Map.Entry<? extends K, ? extends V>> s) {
1639-
// Need to cast to raw in order to work around a limitation in the type system
1640-
super((Set)s);
1636+
super((Set<Map.Entry<K, V>>)s);
16411637
}
16421638

16431639
static <K, V> Consumer<Map.Entry<? extends K, ? extends V>> entryConsumer(
@@ -1721,7 +1717,7 @@ public Stream<Entry<K,V>> parallelStream() {
17211717
}
17221718

17231719
public Iterator<Map.Entry<K,V>> iterator() {
1724-
return new Iterator<Map.Entry<K,V>>() {
1720+
return new Iterator<>() {
17251721
private final Iterator<? extends Map.Entry<? extends K, ? extends V>> i = c.iterator();
17261722

17271723
public boolean hasNext() {
@@ -1920,7 +1916,7 @@ static class UnmodifiableNavigableMap<K,V>
19201916
private static final long serialVersionUID = -4858195264774772197L;
19211917

19221918
/**
1923-
* A class for the {@link EMPTY_NAVIGABLE_MAP} which needs readResolve
1919+
* A class for the {@link #EMPTY_NAVIGABLE_MAP} which needs readResolve
19241920
* to preserve singleton property.
19251921
*
19261922
* @param <K> type of keys, if there were any, and of bounds
@@ -1943,7 +1939,7 @@ public NavigableSet<K> navigableKeySet()
19431939
}
19441940

19451941
/**
1946-
* Singleton for {@link emptyNavigableMap()} which is also immutable.
1942+
* Singleton for {@link #emptyNavigableMap()} which is also immutable.
19471943
*/
19481944
private static final EmptyNavigableMap<?,?> EMPTY_NAVIGABLE_MAP =
19491945
new EmptyNavigableMap<>();
@@ -3191,7 +3187,7 @@ public Iterator<E> iterator() {
31913187
// JDK-6363904 - unwrapped iterator could be typecast to
31923188
// ListIterator with unsafe set()
31933189
final Iterator<E> it = c.iterator();
3194-
return new Iterator<E>() {
3190+
return new Iterator<>() {
31953191
public boolean hasNext() { return it.hasNext(); }
31963192
public E next() { return it.next(); }
31973193
public void remove() { it.remove(); }
@@ -3582,7 +3578,7 @@ public boolean addAll(int index, Collection<? extends E> c) {
35823578
public ListIterator<E> listIterator(final int index) {
35833579
final ListIterator<E> i = list.listIterator(index);
35843580

3585-
return new ListIterator<E>() {
3581+
return new ListIterator<>() {
35863582
public boolean hasNext() { return i.hasNext(); }
35873583
public E next() { return i.next(); }
35883584
public boolean hasPrevious() { return i.hasPrevious(); }
@@ -3890,7 +3886,7 @@ public boolean addAll(Collection<? extends Map.Entry<K, V>> coll) {
38903886
public Iterator<Map.Entry<K,V>> iterator() {
38913887
final Iterator<Map.Entry<K, V>> i = s.iterator();
38923888

3893-
return new Iterator<Map.Entry<K,V>>() {
3889+
return new Iterator<>() {
38943890
public boolean hasNext() { return i.hasNext(); }
38953891
public void remove() { i.remove(); }
38963892

@@ -4745,7 +4741,6 @@ public boolean equals(Object o) {
47454741

47464742
// Override default methods in Map
47474743
@Override
4748-
@SuppressWarnings("unchecked")
47494744
public V getOrDefault(Object k, V defaultValue) {
47504745
return defaultValue;
47514746
}
@@ -4826,7 +4821,7 @@ public static <T> Set<T> singleton(T o) {
48264821
}
48274822

48284823
static <E> Iterator<E> singletonIterator(final E e) {
4829-
return new Iterator<E>() {
4824+
return new Iterator<>() {
48304825
private boolean hasNext = true;
48314826
public boolean hasNext() {
48324827
return hasNext;
@@ -4859,7 +4854,7 @@ public void forEachRemaining(Consumer<? super E> action) {
48594854
* @return A singleton {@code Spliterator}
48604855
*/
48614856
static <T> Spliterator<T> singletonSpliterator(final T element) {
4862-
return new Spliterator<T>() {
4857+
return new Spliterator<>() {
48634858
long est = 1;
48644859

48654860
@Override
@@ -5061,7 +5056,7 @@ public Set<K> keySet() {
50615056

50625057
public Set<Map.Entry<K,V>> entrySet() {
50635058
if (entrySet==null)
5064-
entrySet = Collections.<Map.Entry<K,V>>singleton(
5059+
entrySet = Collections.singleton(
50655060
new SimpleImmutableEntry<>(k, v));
50665061
return entrySet;
50675062
}
@@ -5429,8 +5424,8 @@ public int compare(T t1, T t2) {
54295424

54305425
public boolean equals(Object o) {
54315426
return (o == this) ||
5432-
(o instanceof ReverseComparator2 &&
5433-
cmp.equals(((ReverseComparator2)o).cmp));
5427+
(o instanceof ReverseComparator2<?> that &&
5428+
cmp.equals(that.cmp));
54345429
}
54355430

54365431
public int hashCode() {
@@ -5459,7 +5454,7 @@ public Comparator<T> reversed() {
54595454
* @see Enumeration
54605455
*/
54615456
public static <T> Enumeration<T> enumeration(final Collection<T> c) {
5462-
return new Enumeration<T>() {
5457+
return new Enumeration<>() {
54635458
private final Iterator<T> i = c.iterator();
54645459

54655460
public boolean hasMoreElements() {

0 commit comments

Comments
 (0)