1

I had a list and each item of the list has its parent id. I created hashmap which contain list item as per its parentId. The list is in proper sequence and I add item to hashmap. In the end, the hashmap sequence changed. I don't know where.

var package = HashMap<String, MutableList<Model>>()
var temp: MutableList<Model>? = ArrayList<Model>()
for (item in list!!) {
    temp = package?.get(item.parentId!!)
    Log.e("parent id",Gson().toJson(item.parentId!!))
    if (temp != null)
        temp.add(item)
    else {
        temp = ArrayList<Model>()
        temp.add(item)
    }

    package?.put(item.parentId!!, temp)
    Log.e("package",Gson().toJson(package.keys))
Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
Rahul sharma
  • 1,492
  • 12
  • 26
  • 1
    https://stackoverflow.com/questions/2144776/is-the-order-of-values-retrieved-from-a-hashmap-the-insertion-order – Rohit5k2 Dec 30 '19 at 07:00
  • It's not guaranteed that the `HashMap` sequence will be the same as how you inserted. Use `LinkedHashSet` if you would like to preserve the order. https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-linked-hash-set/index.html – Amosa Dec 30 '19 at 21:07

0 Answers0