gpt4 book ai didi

kotlin - 如何在 kotlin 中打乱 HashMap

转载 作者:行者123 更新时间:2023-12-02 20:26:35 24 4
gpt4 key购买 nike

我创建了一个 HashMap 。现在我想打乱其中的对象。我们使用Collections.shuffle()来对列表中的所有元素进行洗牌。我怎样才能在 HashMap 中做同样的事情?

这是我的 HashMap :

val tips = hashMapOf("Having a balanced diet is the key" to "Have nutritious foods like vegetables and fruits along with legumes, whole wheat, cereals etc., at regular intervals. Avoid oily and spicy food to ease your pregnancy symptoms. Plan your food intake and have it as 4-5 light meals every day."
, "Fluids will help you manage" to "Drink sufficient water and fluids to maintain the retention of water in your body. This will help you control constipation, indigestion, dryness, fatigue, bloating and gas. Avoid alcohol and caffeine drinks which may have serious effects during pregnancy."
, "Do not miss prenatal supplements" to "Doctors prescribe prenatal vitamin and mineral supplements for the normal growth and development. Do not skip these supplements as they can prevent preterm labour and many serious health concerns in the newborn."
, "Folic acid is essential" to "During pregnancy, have folic acid (supplement) or folate (natural source of folic acid) to avoid various health problems. They are rich in green leafy vegetables, oranges, avocado etc.")

最佳答案

这段代码应该可以工作:

tips.map { it.key to it.value }.shuffled().toMap()

它将 Map 转换为 List,对其进行打乱,然后再转换回 Map

关于kotlin - 如何在 kotlin 中打乱 HashMap ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49721194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com