1

In my android app with Kotlin, I have a variable "data" type of Map>,Which Product is a kotlin data class.

var data: Map<String, Array<Product>>

Product data class contains var availableQuantities. I want to loop the Map> and conserve only the product with availableQuantities != 0, and update the data variable.How can I do this? Here's my code

   try {
        val filteredMap: HashMap<String, Array<Product>> = HashMap()
        data.forEach { (key, value) ->
            val filtered = value.filter { prod -> prod.availableQuantities != 0 }
            if (filtered.isNotEmpty()) {
                filteredMap[key] = filtered.toTypedArray()
            }
        }
    } catch (e: Throwable) {
        e.message
    }

But, after running the code an exception appear as the following image :

The following image is the description of data :

enter image description here

How can I correct my code to make it functionnal?

4
  • Do you need result as Map or a just List of Products ? Commented Apr 15, 2020 at 11:35
  • I Need a result as Map, I want to update "data" Commented Apr 15, 2020 at 11:44
  • When you say your "app is blocked" in various comments below, do you mean that it crashes or that it hangs? Commented Apr 15, 2020 at 12:48
  • I add try catch to my code and I get the following exception java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.models.Product[] Commented Apr 15, 2020 at 12:49

5 Answers 5

1

I think it could use .values.flatten() to make a flat list.

  val tempProd = data.values.flatten().filter {
            it.availableQuantities != 0
        }
Sign up to request clarification or add additional context in comments.

Comments

0

Wouldn't something like this work?

val tempProd = StreamSupport.stream(data.values.toMutableList())
            .map{t -> t.filter{
                it.availableQuantities!= 0
            } }

You might get empty arrays though, since it's an array of arrays. If you only want a single array, you'd need to reduce the two-dimensional array

1 Comment

No, didn't work I want to loop the data, and update it with only th Product with quantity not zero Did you get me ?
0

Instead of using Stream, you could simply benefit on kotlin extension functions and get rid of empty arrays aswell using flatMap {} in order to merge all arrays of Product to single list.

So Your scenario would look like:

val temp = data.values.flatMap { products ->
    products.filter { product ->
        product.quantity != 0
    }
}

3 Comments

When, I use your answer, the app is blocked
what is the message ?
There's no message, only is blocked even if I debug
0
data = data.mapValues { (_, array) ->
    array.filter { it.availableQuantities != 0 }.toTypedArray()
}

9 Comments

I like ur answer, but the app is blocked I don't know why
It's a dynamic map, it's not statique
@Lina at the time when you use this code and the app is blocked. Anyway use it in different thread then
Yes, When I debug the app, only the message "the application is running" is appear
I add try catch to my code and I get the following exception java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.models.Product[]
|
0

There are two cases i can think of .If you do not want to keep empty product list against a Key.

val filteredMap: HashMap<String, Array<Product>> = HashMap()
data.forEach { (key, value) ->
    val filtered=value.filter {prod-> prod.availableQuantities!=0 }
    if(filtered.isNotEmpty()){
        filteredMap[key]=filtered.toTypedArray()
    }
 }

Or if you want to keep empty list against a Key then you can try something like :-

val filteredMap= data.mapValues { (_, value) ->
    value.filter { it.availableQuantities != 0 }.toTypedArray()
}

7 Comments

I like your answer, thank you But, I don't know why it not work
the app is blocked even if I debug, I don't know
I add try catch to my code and I get the following exception java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.models.Product[]
Edit your question with updated code .. i have tested this code it should work efficiently .. If you use Map<String, ArrayList<Product>> instead of Map<String, Array<Product>> things will be a bit easy ..
We couldn't write a code as the following ? " val tempVariant = StreamSupport.stream(data.toList()) .filter { o1 -> o1.second {it -> } .collect(Collectors.toList<>())"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.