Why Kotlin is not allowing me to add elements to a Array list - arraylist

I have a Kotlin object which I am constructing from a API response
schedule: Map<String, ArrayList<String>>
my object looks like below.
{0=[0, 2.00, 23.59], 1=[1, 2.00, 23.59], 2=[2, 2.00, 23.59], 4=[4,
2.00, 23.59], 5=[5, 2.00, 23.59], 6=[6, 2.00, 23.59]}
At some point, I would like to add/delete an entry from the collection. For example, after my 3rd object, I would like to add an entry
3=[3, 2.00, 23.59].
What I tried so far is schedule.schedule.plus() method unsuccessfully.
This should allow me to add a Map<String, ArrayList<String> But it wouldn't?
Update: Following has helped me add the desired object but Now I cannot seem to sort the collection.
var list = ArrayList<String>()
list.add(0,"2")
list.add(1,"23")
schedule.schedule?.put(num.toString(),list)

If you want to assign a value to a list then use plusAssign() function. because the plus() method is just like the behaviour as a = b + c and the plusAssign() is a+=b.

Following two has helped me add or remove entries.
Add
var list = ArrayList<String>()
list.add(0,num.toString())
list.add(1,"5.0")
list.add(2,"14.0")
schedule.schedule?.put(num.toString(),list)
And for removal.
schedule.schedule.remove("0")
But I had to change the Map to MutableMap<String, ArrayList<String>>

Related

How can I make a mutable list of mutable maps?

My attempt:
val areas = mutableListOf<mutableMapOf<Double, Double>()>()
On mutableListOf it shows an error Function invocation 'mutableListOf(...)' expected
mutableMapOf<Double, Double>() is a function call that creates a map. You need to first create a list and only specify the type of its items, which is: MutableMap<Double, Double>:
val areas = mutableListOf<MutableMap<Double, Double>>()
Then you can add maps to it:
areas += mutableMapOf(1.0 to 1.0)

adding to a list that is contained in livedata and adding elements to that list

adding to a list that is contained in livedata and adding elements to that list
val resourcesLiveData by lazy { MutableLiveData<List<File>>() }
I thought this should work as my LiveData is a list of files and I just want to add elements to it. But the value of live data is always an empty list. The res is the different file resources I am trying to add
resourceLiveData.value?.toMutableList()?.add(res)
So I tried it more expicity using this version but the list is still empty
val listOfRes = resourceLiveData.value ?: emptyList()
listOfRes.toMutableList().add(res)
resourceLiveData.value = listOfRes.toList()
Can anyone see if I am doing something wrong.
Just want to add to the list that is contained in the value
Agree to #KeyserSoze answer, if you have to use only List then you can do below
resourceLiveData.value = resourceLiveData.value?.toMutableList()?.apply { add(res) }?: emptyList()
You are creating a new object by calling toMutableList() instead of updating the original.
Change your LiveData type to MutableList:
val resourcesLiveData by lazy { MutableLiveData<MutableList<File>>() }
Then, update the value accordingly:
resourceLiveData.value?.add(res)

How I can remove\delete object in kotlin?

I have class and two objects. I want to delete 1st object. How I can delete it?
I tried just delete() (I found it on kotlinlangcom) but it doesn't work. I have red light bulb what recommend: "Create member function Person.delete", "Rename reference" and "Create extension function Person.delete".
fun main() {
// copy object in object
data class Person (var name: String = "Orig", var type: String = "piece",
var age: Int = 18, var high: Double = 25.7, var code: Int = 1522)
{
var info: String = "0"
get() = "Name: $name Age: $age Type: $type High: $high Code: $code"
}
val ann: Person = Person("Ann", "man", 10, 0.5, 1408) // 1st object with some properties
var bob: Person = Person("Bob", "girl", 20, 15.0, 1239) // 2nd object without prop
println(ann.info)// props 1st object
println(bob.info)// props 2nd object
print(" ---- ")
bob = ann.copy() // copy 1st in 2nd
println("Bob has Anns' props: ")
print("final " + bob.info) // new props 2nd object
bob.delete()
}
You don't need to thing about deleting objects like in other languages like c++/c ... the garbage collector of the JVM is taking care of it (if you use kotlin with jvm)
All you need to know is keeping no references on the object
So if you have a collection (list,map ...) where you put the object in, you also have to put it out if the collection is a property of a long living class like a model or something ... thats the only possiblity to getting into trouble within kotlin, putting a reference into a collection which is referenced by a static or long living object.
Within a function there is no need to delete the objects created withing.
Keep in mind that the garbage collector (GC) is not running instantly after finishing the method. There are different strategies depending on the age of the object and the garbage collector itself. If you would like to see the GC in action, this tool (visualgc) https://www.oracle.com/technetwork/java/visualgc-136680.html has some pretty nice visualisations.
You could also find much more details about garbage collection here: https://www.oracle.com/webfolder/technetwork/tutorials/obe/java/gc01/index.html

Get unique css selector from WebElement

is it possible to get a css selector of an WebElement?
eg
var optionSelectors = mutableListOf<String>()
val options = selectWebElement?.findElements(By.cssSelector("option")).orEmpty()
for(option in options){
var optionSelector = option.getSelector()
optionSelectors.add(optionSelector)
}
return toJson(optionSelectors)
Thank you in advance
You can always use Reflection to get foundBy property value like:
Field field = element.getClass().getDeclaredField("foundBy");
field.setAccessible(true);
String foundBy = field.get(element).toString();
However the nature of your question is a little bit weird, given you found the element already you should know its selector, shouldn't you? If you want to interact with the Select option values you can go for the relevant Select class which has getOptions() function.
Also consider going for Page Object Model design pattern, it is one of best practices to keep your test logic separate from UI layer

How to copy or clone mutableList of data object without using collection map in Kotlin

I create copy of one MutableList. When I update element value of copy MutableList But Original List element value also changed. When I use map It working fine but It is like a iteration of whole list, Is any way to do achieve without iteration ? how to copy elements of the MutableList.
val array: MutableList<UserData> = ArrayList()
val userData = UserData("DataOne")
array.add(userData)
val arrayCopy = ImmutableList.copyOf(array)// not working
//val arrayCopy = array.toMutableList()// not working
// val arrayCopy = array.map { it.copy() }.toMutableList()//working
Log.i("----> array ", array[0].name)//print DataOne
Log.i("----> arrayCopy ", arrayCopy[0].name)//print DataOne
arrayCopy[0].name = "DataTwo"
Log.d("----> array ", array[0].name)//print DataTwo
Log.d("----> arrayCopy", arrayCopy[0].name) //print DataTwo
ImmutableList.copyOf does copy the list. The problem is that you want to copy elements of the list as well. Of course you have to iterate over the list to do that, but so does copyOf and I don't see why you expect it's possible to avoid. A slightly better version of map is
array.mapTo(mutableListOf()) { it.copy() }
because it iterates only once.
Sorry but there wouldn't be any other way cause to convert one element you will have to read/copy it once, for n number of element you'll have to iterate n times to get the proper copy.
The only other way I can think of is to create the desired immutable/mutable list in the first place and not copying it all at once later.
Hope this helps