abu naser
09/21/2022, 11:52 AMimport org.junit.Test
class TestMutableSetDelete {
@Test
fun testRemoveMutableSet(){
val data = TestSetList(mutableSetOf(), mutableSetOf())
add(data)
change(data)
//deletes not work in any inner value ever change
delete(data)
// removeIf always works . but i my code need to work below api 24 too.
// data.span.removeIf { it.start==it.end }
// FIXME: without change this works . but if change happend its not
data.range.forEach { println("start > ${it.start} > ${it.end}") }
assert(data.range.size==2)
}
}
data class TestSetList(
val range: MutableSet<Range> = mutableSetOf(),
val para: MutableSet<Para> = mutableSetOf()
)
data class Para(
val start:Int,
val end:Int
)
data class ChangedData(
var name:String="",
var age:Long
)
data class Range(
val start:Int,
val end:Int,
val changedData: ChangedData
)
fun add(data:TestSetList){
val cd =ChangedData("naser",26)
data.range.add(Range(0,0,cd))
data.range.add(Range(1,2,cd))
data.range.add(Range(1,5,cd))
data.range.add(Range(2,2,cd))
data.range.add(Range(3,3,cd))
}
fun change(data: TestSetList){
//simulate some change in the process .
data.range.forEach { it.changedData.name = it.start.toString() }
}
fun delete(data:TestSetList){
data.range.filter { it.start == it.end }.forEach { data.range.remove(it) }
}
Ioannis Mavroukakis
09/21/2022, 12:05 PMIoannis Mavroukakis
09/21/2022, 12:06 PMabu naser
09/21/2022, 12:13 PMabu naser
09/21/2022, 12:15 PMabu naser
09/21/2022, 12:18 PMMichael de Kaste
09/21/2022, 12:33 PMMichael de Kaste
09/21/2022, 12:34 PMMichael de Kaste
09/21/2022, 12:41 PMclass TestMutableSetDelete {
fun testRemoveMutableSet() {
val data = TestSetList(mutableMapOf(), mutableSetOf())
add(data)
change(data)
delete(data)
data.range.forEach { println("start > ${it.key.start} > ${it.key.end}") }
assert(data.range.size == 2)
}
}
data class TestSetList(
val range: MutableMap<RangeKey, ChangedData> = mutableMapOf(),
val para: MutableSet<Para> = mutableSetOf()
)
data class Para(
val start: Int,
val end: Int
)
data class ChangedData(
var name: String = "",
var age: Long
)
data class RangeKey(
val start: Int,
val end: Int
)
fun add(data: TestSetList) {
val cd = ChangedData("naser", 26)
data.range[RangeKey(0, 0)] = cd
data.range[RangeKey(1, 2)] = cd
data.range[RangeKey(1, 5)] = cd
data.range[RangeKey(2, 2)] = cd
data.range[RangeKey(3, 3)] = cd
}
fun change(data: TestSetList) {
// simulate some change in the process .
data.range.forEach { (k,v) -> v.name = k.start.toString() }
}
fun delete(data: TestSetList) {
data.range.keys.filter { it.start == it.end }.forEach { data.range.remove(it) }
}
Ioannis Mavroukakis
09/21/2022, 12:49 PMSet
equality. Behind the scenes, Set is expressed as keys in a Map
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
Michael de Kaste
09/21/2022, 12:58 PMfun add(data: TestSetList) {
val cd = ChangedData("naser", 26)
data.range[RangeKey(0, 0)] = cd.copy()
data.range[RangeKey(1, 2)] = cd.copy()
data.range[RangeKey(1, 5)] = cd.copy()
data.range[RangeKey(2, 2)] = cd.copy()
data.range[RangeKey(3, 3)] = cd.copy()
}
consider using different actual classes as a value for your keys, otherwise the 5 keys will all point to the same value at which point
fun change(data: TestSetList) {
// simulate some change in the process .
data.range.forEach { (k,v) -> v.name = k.start.toString() }
}
does nothing but changes the name of the value on all keys to "3"abu naser
09/21/2022, 2:08 PM