Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

find value in arraylist in kotlin

Hey I am working in kotlin. I am working on tree data structure. I added the value in list and now I want to find that value and modified their property. But I am getting the error.

VariantNode, StrengthNode, ProductVariant

StrengthNode.kt

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class StrengthNode : VariantNode() {
    var pricePerUnit: String? = null
    var defaultValue = AtomicBoolean(false)
}

ActivityViewModel.kt

class ActivityViewModel : ViewModel() {

    var baseNode: VariantNode = VariantNode()
    private val defaultValueId = "12643423243324"

    init {
        createGraph()
    }

    private fun createGraph() {
        val tempHashMap: MutableMap<String, VariantNode> = mutableMapOf()
        val sortedList = getSortedList()

        sortedList.forEach { productVariant ->
            productVariant.strength?.let { strength ->
                if (tempHashMap.containsKey("strength_${strength.value}")) {
 baseNode.children.contains(VariantNode(strength.value)) // getting error 
                    return@let
                }
                val tempNode = StrengthNode().apply {
                    value = strength
                    pricePerUnit = productVariant.pricePerUnit?.value
                    if (productVariant.id == defaultValueId) {
                        defaultValue.compareAndSet(false, true)
                    }
                }
                baseNode.children.add(tempNode)
                tempHashMap["strength_${strength.value}"] = tempNode
            }
            productVariant.quantity?.let { quantity ->
                if (tempHashMap.containsKey("strength_${productVariant.strength?.value}_quantity_${quantity.value}")) {
                    return@let
                }
                val tempNode = QuantityNode().apply {
                    value = quantity
                }
                val parent =
                    tempHashMap["strength_${productVariant.strength?.value}"] ?: baseNode
                parent.children.add(tempNode)

                tempHashMap["strength_${productVariant.strength?.value}_quantity_${quantity.value}"] =
                    tempNode
            }
            productVariant.subscription?.let { subscription ->
                val tempNode = SubscriptionNode().apply {
                    value = subscription
                }
                val parent =
                    tempHashMap["strength_${productVariant.strength?.value}_quantity_${productVariant.quantity?.value}"]
                        ?: baseNode
                parent.children.add(tempNode)
            }
        }
        baseNode
    }
}

I am getting error on this.

enter image description here

I want to find that node value and modified other property.

>Solution :

Your class VariantNode only has a single no-arg constructor, but you’re trying to call it with arguments, hence the error

Too many arguments for public constructor VariantNode() defined in com.example.optionsview.VariantNode

Either you have to provide a constructor, that matches your call, e.g.

open class VariantNode(var value: ProductValue?) {
    var children: MutableList<VariantNode> = arrayListOf()
}

or you need to adjust your code to use the no-arg constructor instead.

val node = VariantNode()
node.value = strength.value

baseNode.children.contains(node)

Note however, that your call to contains most likely will not work, because you do not provide a custom implementation for equals. This is provided by default, when using a data class.

If you just want to validate whether baseNode.children has any element, where value has the expected value, you can use any instead, e.g.:

baseNode.children.any { it.value == strength.value }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading