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:
Ilya Gorbunov
2015-11-20 18:43:00 +03:00
parent f8f257fa44
commit 2ed277a863
6 changed files with 58 additions and 4 deletions
+10 -1
View File
@@ -6274,7 +6274,7 @@ public fun ShortArray.toSet(): Set<Short> {
/**
* 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>())
}
@@ -6334,6 +6334,15 @@ public fun ShortArray.toSortedSet(): SortedSet<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.
*/
+10 -1
View File
@@ -1065,10 +1065,19 @@ public fun <T> Iterable<T>.toSet(): Set<T> {
/**
* 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>())
}
/**
* 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.
*/
+10 -1
View File
@@ -559,10 +559,19 @@ public fun <T> Sequence<T>.toSet(): Set<T> {
/**
* 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>())
}
/**
* 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.
*/
@@ -68,7 +68,7 @@ public fun <K, V> Map<K, V>.toSortedMap(comparator: Comparator<in K>): SortedMap
*
* @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) }
@@ -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() {
expect(setOf(1, 2)) { sortedSetOf(1, 2, 3, 4, 5).take(2).toSet() }
}