Oleh Ponomarenko
02/26/2019, 11:57 AMgildor
02/26/2019, 11:58 AMDico
02/26/2019, 12:00 PMgildor
02/26/2019, 12:02 PMOleh Ponomarenko
02/26/2019, 12:04 PMAlan Evans
02/26/2019, 12:05 PMIterable<Int>
you use:
val next: Int? = iterable.dropWhile { it != 8 }.getOrNull(1)
So would apply to all Set
implementations, or List
implementations.gildor
02/26/2019, 12:09 PMAlan Evans
02/26/2019, 12:11 PMThe main goal to add previous and next button on Details screen to be able going through the set.
gildor
02/26/2019, 12:14 PMAlan Evans
02/26/2019, 12:14 PMNavigableSet
? And why not a SortedSet
?gildor
02/26/2019, 12:15 PMDico
02/26/2019, 12:16 PMgildor
02/26/2019, 12:16 PMAlan Evans
02/26/2019, 12:17 PMval next: Int? = iterable
.asSequence()
.dropWhile { it != 8 }
.elementAtOrNull(1)
gildor
02/26/2019, 12:17 PMAlan Evans
02/26/2019, 12:19 PMwhile
loop?Dico
02/26/2019, 12:20 PMOleh Ponomarenko
02/26/2019, 12:20 PMAlan Evans
02/26/2019, 12:21 PMiterable
So it works for the TreeSet
or a List
.Dico
02/26/2019, 12:21 PMAlan Evans
02/26/2019, 12:21 PMList
.Oleh Ponomarenko
02/26/2019, 12:21 PMDico
02/26/2019, 12:22 PMOleh Ponomarenko
02/26/2019, 12:22 PMAlan Evans
02/26/2019, 12:22 PMval list: List<Int> = set.sortedBy { it }
val next: Int? = list
.asSequence()
.dropWhile { it != 8 }
.elementAtOrNull(1)
int
and are sorting something else, it is far easier to specify that sort order than support Comparable
, or write a Comparator
which is what you need to do if you use TreeSet
.gildor
02/26/2019, 12:35 PMOleh Ponomarenko
02/27/2019, 11:04 AMAlan Evans
02/27/2019, 1:18 PMit is far easier to specify that sort order than support, or write aComparable
which is what you need to do if you useComparator
.TreeSet
val set = TreeSet<Int> { a, b -> a.compareTo(b) }
class MyClass(val field1: String, val field2: Int)
val byField1 = Comparator<MyClass> { o1, o2 -> o1.field1.compareTo(o2.field1) }
val byField2 = Comparator<MyClass> { o1, o2 -> o1.field2.compareTo(o2.field2) }
val byField2Descending = byField2.reversed()
val set = TreeSet<MyClass>(byField1.then(byField2Descending))
Oleh Ponomarenko
02/27/2019, 1:25 PMAlan Evans
02/27/2019, 1:26 PMOleh Ponomarenko
02/27/2019, 1:27 PMAlan Evans
02/27/2019, 1:28 PMsortedBy { it.someField }
it
worked for Int
Oleh Ponomarenko
02/27/2019, 1:29 PMAlan Evans
02/27/2019, 1:29 PMOleh Ponomarenko
02/27/2019, 1:30 PMAlan Evans
02/27/2019, 1:31 PMset.sortedWith(byField1.then(byField2Descending))
Oleh Ponomarenko
02/27/2019, 1:46 PM