Add upperbound constraint Comparable<T> to toSortedSet() and sortedMapOf()
Provide an overload of toSortedSet() with Comparator<in T> parameter #KT-10115 Fixed
This commit is contained in:
@@ -6274,7 +6274,7 @@ public fun ShortArray.toSet(): Set<Short> {
|
|||||||
/**
|
/**
|
||||||
* Returns a [SortedSet] of all elements.
|
* Returns a [SortedSet] of all elements.
|
||||||
*/
|
*/
|
||||||
public fun <T> Array<out T>.toSortedSet(): SortedSet<T> {
|
public fun <T: Comparable<T>> Array<out T>.toSortedSet(): SortedSet<T> {
|
||||||
return toCollection(TreeSet<T>())
|
return toCollection(TreeSet<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -6334,6 +6334,15 @@ public fun ShortArray.toSortedSet(): SortedSet<Short> {
|
|||||||
return toCollection(TreeSet<Short>())
|
return toCollection(TreeSet<Short>())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [SortedSet] of all elements.
|
||||||
|
* Elements in the set returned are sorted according to the given [comparator].
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmVersion
|
||||||
|
public fun <T> Array<out T>.toSortedSet(comparator: Comparator<in T>): SortedSet<T> {
|
||||||
|
return toCollection(TreeSet<T>(comparator))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array.
|
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1065,10 +1065,19 @@ public fun <T> Iterable<T>.toSet(): Set<T> {
|
|||||||
/**
|
/**
|
||||||
* Returns a [SortedSet] of all elements.
|
* Returns a [SortedSet] of all elements.
|
||||||
*/
|
*/
|
||||||
public fun <T> Iterable<T>.toSortedSet(): SortedSet<T> {
|
public fun <T: Comparable<T>> Iterable<T>.toSortedSet(): SortedSet<T> {
|
||||||
return toCollection(TreeSet<T>())
|
return toCollection(TreeSet<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [SortedSet] of all elements.
|
||||||
|
* Elements in the set returned are sorted according to the given [comparator].
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmVersion
|
||||||
|
public fun <T> Iterable<T>.toSortedSet(comparator: Comparator<in T>): SortedSet<T> {
|
||||||
|
return toCollection(TreeSet<T>(comparator))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.
|
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -559,10 +559,19 @@ public fun <T> Sequence<T>.toSet(): Set<T> {
|
|||||||
/**
|
/**
|
||||||
* Returns a [SortedSet] of all elements.
|
* Returns a [SortedSet] of all elements.
|
||||||
*/
|
*/
|
||||||
public fun <T> Sequence<T>.toSortedSet(): SortedSet<T> {
|
public fun <T: Comparable<T>> Sequence<T>.toSortedSet(): SortedSet<T> {
|
||||||
return toCollection(TreeSet<T>())
|
return toCollection(TreeSet<T>())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a [SortedSet] of all elements.
|
||||||
|
* Elements in the set returned are sorted according to the given [comparator].
|
||||||
|
*/
|
||||||
|
@kotlin.jvm.JvmVersion
|
||||||
|
public fun <T> Sequence<T>.toSortedSet(comparator: Comparator<in T>): SortedSet<T> {
|
||||||
|
return toCollection(TreeSet<T>(comparator))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence.
|
* Returns a single sequence of all elements from results of [transform] function being invoked on each element of original sequence.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ public fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<in K>): SortedMap
|
|||||||
*
|
*
|
||||||
* @sample test.collections.MapJVMTest.createSortedMap
|
* @sample test.collections.MapJVMTest.createSortedMap
|
||||||
*/
|
*/
|
||||||
public fun <K, V> sortedMapOf(vararg pairs: Pair<K, V>): SortedMap<K, V>
|
public fun <K : Comparable<K>, V> sortedMapOf(vararg pairs: Pair<K, V>): SortedMap<K, V>
|
||||||
= TreeMap<K, V>().apply { putAll(pairs) }
|
= TreeMap<K, V>().apply { putAll(pairs) }
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -128,6 +128,18 @@ class CollectionJVMTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@test fun toSortedSet() {
|
||||||
|
val data = listOf("foo", "Foo", "bar")
|
||||||
|
val set1 = data.toSortedSet()
|
||||||
|
assertEquals(listOf("Foo", "bar", "foo"), set1.toList())
|
||||||
|
|
||||||
|
val set2 = data.toSortedSet(reverseOrder())
|
||||||
|
assertEquals(listOf("foo", "bar", "Foo"), set2.toList())
|
||||||
|
|
||||||
|
val set3 = data.toSortedSet(String.CASE_INSENSITIVE_ORDER)
|
||||||
|
assertEquals(listOf("bar", "foo"), set3.toList())
|
||||||
|
}
|
||||||
|
|
||||||
@test fun takeReturnsFirstNElements() {
|
@test fun takeReturnsFirstNElements() {
|
||||||
expect(setOf(1, 2)) { sortedSetOf(1, 2, 3, 4, 5).take(2).toSet() }
|
expect(setOf(1, 2)) { sortedSetOf(1, 2, 3, 4, 5).take(2).toSet() }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -44,11 +44,26 @@ fun snapshots(): List<GenericFunction> {
|
|||||||
templates add f("toSortedSet()") {
|
templates add f("toSortedSet()") {
|
||||||
deprecate(Strings) { forBinaryCompatibility }
|
deprecate(Strings) { forBinaryCompatibility }
|
||||||
include(CharSequences, Strings)
|
include(CharSequences, Strings)
|
||||||
|
typeParam("T: Comparable<T>")
|
||||||
doc { f -> "Returns a [SortedSet] of all ${f.element}s." }
|
doc { f -> "Returns a [SortedSet] of all ${f.element}s." }
|
||||||
returns("SortedSet<T>")
|
returns("SortedSet<T>")
|
||||||
body { "return toCollection(TreeSet<T>())" }
|
body { "return toCollection(TreeSet<T>())" }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
templates add f("toSortedSet(comparator: Comparator<in T>)") {
|
||||||
|
only(Iterables, ArraysOfObjects, Sequences)
|
||||||
|
jvmOnly(true)
|
||||||
|
doc { f ->
|
||||||
|
"""
|
||||||
|
Returns a [SortedSet] of all ${f.element}s.
|
||||||
|
|
||||||
|
Elements in the set returned are sorted according to the given [comparator].
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
returns("SortedSet<T>")
|
||||||
|
body { "return toCollection(TreeSet<T>(comparator))" }
|
||||||
|
}
|
||||||
|
|
||||||
templates add f("toArrayList()") {
|
templates add f("toArrayList()") {
|
||||||
doc { f -> "Returns an [ArrayList] of all ${f.element}s." }
|
doc { f -> "Returns an [ArrayList] of all ${f.element}s." }
|
||||||
returns("ArrayList<T>")
|
returns("ArrayList<T>")
|
||||||
|
|||||||
Reference in New Issue
Block a user