Android中HashMap的使用

Android中HashMap的使用,第1张

Android中HashMap的使用 Android中HashMap的使用:
HashMap的简单实例:

01.创建Map:

class MainActivity : AppCompatActivity() {
    @RequiresApi(Build.VERSION_CODES.N)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val name = mapOf("name" to "alfied", "sex" to "man", "age" to "27")
        //key-value取值
        println(name.getValue("name"))
        //获取默认的值
        println(name.getOrDefault("weight", "70kg"))
        //除掉目标值
        println(name.minus("age"))
        //加上目标值
        println(name.plus("height" to "180cm"))
    }
}

02.MutableMap的数据增删改查

        //创建MutableMap
        val m1 = mutableMapOf("name" to "alfred")
        val m2 = mutableMapOf("sex" to "man", "age" to "27")
        //数据增加
        m1.put("身高", "128cm")
        println(m1)
        //数据赋值,增加数据
        m1["体重"] = "70kg"
        println(m1)
        //数据移除
        m1.remove("体重")
        println(m1)
        //数据增加
        m1.putAll(m2)
        println(m1)
        //数据移除
        m1.remove("体重", "70kg")
        println(m1)

创建HashMap、linkedMap、SortedMap

        //创建HashMapOf()、linkedMapOf()、SortedMapOf()
        val hash = hashMapOf("name" to "alfred")
        val linkedMap = linkedMapOf("age" to "27")
        val treeMap = sortedMapOf("sex" to "man")
        println(hash)
        println(linkedMap)
        println(treeMap)

HashMap的拓展实例:

01.HashMap以Key来进行排序

        //TreeMap的排序策略
        val map1 = sortedMapOf()
        map1.put("Java", 30)
        map1.put("Android", 40)
        map1.put("C++", 15)
        //TreeMap默认以key的升序进行排序
        println(map1)
        var comparator = kotlin.Comparator{key1: String, key2: String -> key2.compareTo(key1)}
        val map2 = sortedMapOf(comparator)
        map2.put("Java", 30)
        map2.put("Android", 40)
        map2.put("C++", 15)
        println(map2)

02.HashMap以value来进行排序

        //TreeMap以Value来进行排序
        var map3 = TreeMap()
        map3.put("Java", 30)
        map3.put("Android", 60)
        map3.put("C++", 15)
        map3.put("Kotlin", 100)
        map3["C"] = 0
        //升序
        val mapSort = map3.entries.sortedBy { it.value}
            .associateBy({it.key}, {it.value})
        println(mapSort)
        //倒序
        val mapDescSort = map3.entries.sortedByDescending { it.value }
            .associateBy({it.key}, {it.value})
        println(mapDescSort)
        //升序
        val toMap = map3.toList().sortedBy { it.second }.toMap()
        println(toMap)

03.HashMap的遍历

        //HashMap的遍历
        val map4 = hashMapOf("name" to "Jack", "age" to "12", "sex" to "man")
        val iter = map4.entries.iterator()
        while(iter.hasNext()){
            val entry = iter.next()
            println(entry)
        }
        for(key in map4.keys) {
            println("key = $key + value = ${map4.get(key)}")
        }

        //HashMap遍历
        map4.forEach {
            println("key==${it.key}, value==${it.value}")
        }

        //HashMap遍历
        for ((key, value) in map4) {
            println("key==$key, value==$value")
        }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5718241.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-18
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存