How to choose four hashmap key-value pairs declared in object class in Android kotlin?

57 Views Asked by At

I have declared an object class ImageStore, where there is imagesNamesMap containing 5 key-value pairs, I want to randomly choose 4 key-value pairs out of the imagesNameMap then randomly assign the keys and value between those 4.

ImageStore

object ImageStore { 
   val imagesNamesMap= hashMapOf(
      R.drawable.africa to "Africa",
      R.drawable.asia to "Asia",
      R.drawable.europe to "Europe",
      R.drawable.antartica to "Antarctica",
      R.drawable.airplane to "Airplane"
   )
    
   val selectedImagesNamesMap = imagesNamesMap.toList().take(4).toMap()
}

Example

val imagesNamesMap= hashMapOf(
  R.drawable.africa to "Africa",
  R.drawable.asia to "Asia",
  R.drawable.europe to "Europe",
  R.drawable.antartica to "Antarctica",
  R.drawable.airplane to "Airplane"
)

// randomly choose 4 out of imagesNamesMap
val random4 = hashMapOf(
  R.drawable.africa to "Africa",
  R.drawable.asia to "Asia",
  R.drawable.europe to "Europe",
  R.drawable.airplane to "Airplane"
)

// reassign key and values
val random4 = hashMapOf(
  R.drawable.africa to "Asia",
  R.drawable.asia to "Africa",
  R.drawable.europe to "Airplane",
  R.drawable.airplane to "Europe"
) <--- I want this map... How to get so?
2

There are 2 best solutions below

2
Sidharth Mudgil On

update the ImageStore object like this

object ImageStore { 
    val imagesNamesMap = hashMapOf(
        R.drawable.africa to "Africa",
        R.drawable.asia to "Asia",
        R.drawable.europe to "Europe",
        R.drawable.antartica to "Antarctica",
        R.drawable.airplane to "Airplane"
    )

    fun getRandom4(): MutableMap<Int, String> {
        val selectedImagesNamesMap: MutableMap<Int, String> = mutableMapOf()
        val randomKeys = imagesNamesMap.keys.shuffled().take(4).map {
            selectedImagesNamesMap[it] = imagesNamesMap[it] ?: ""
        }
        val selectedValues = selectedImagesNamesMap.values.shuffled().toMutableList()
            selectedImagesNamesMap.keys.toList().forEachIndexed { index, key ->
            selectedImagesNamesMap[key] = selectedValues[index]
        }
        return selectedImagesNamesMap
    }
}

now you can get the map using

val random4 = ImageStore.getRandom4()
3
Praveen Kumar On

use this function and modify your's accordingly

object ImageStore {

val imagesNamesMap = hashMapOf(
    R.drawable.africa to "Africa",
    R.drawable.asia to "Asia",
    R.drawable.europe to "Europe",
    R.drawable.antartica to "Antarctica",
    R.drawable.airplane to "Airplane"
)

val selectedImagesNamesMap: Map<Int, String> = imagesNamesMap.toList()
    .shuffled()
    .take(4)
    .toMap() }

Here's what the code does:

imagesNamesMap remains unchanged. It contains all the key-value pairs.

selectedImagesNamesMap is modified as follows:

.toList() converts the imagesNamesMap into a list of key-value pairs.

.shuffled() shuffles the list randomly.

.take(4) selects the first four key-value pairs from the shuffled list.

.toMap() converts the selected key-value pairs back into a map.

By using the .shuffled() function, you ensure that you're selecting random key-value pairs from the imagesNamesMap each time you run the code. This approach prevents the exclusion of any particular key-value pair and provides the random selection you're looking for.