Vivek Modi
Vivek Modi

Reputation: 7151

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

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.

Upvotes: 1

Views: 1485

Answers (1)

Endzeit
Endzeit

Reputation: 5474

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 }

Upvotes: 1

Related Questions