首页 > 编程语言 > 详细

java中TreeMap有什么用,举例说明?

时间:2021-07-13 12:01:26      阅读:15      评论:0      收藏:0      [点我收藏+]

3.7 TreeMap的用法

马克-to-win:TreeSet是按升序顺序输出的。TreeMap也是按升序输出,但是和它的区别就是TreeSet存储的是单个元素,而TreeMap存储的是一个一个的键值对。

例:3.7.1

import java.util.*;
public class TestMark_to_win {
    public static void main(String args[]) {
        TreeMap tm = new TreeMap();
        tm.put("zs", new Double(1212.34));
        tm.put("lsMark", new Double(3245.22));
        tm.put("ww", new Double(2345.00));
        tm.put("zl", new Double(3323.22));
        /* entrySet取出一个一个的键值对。 */
        Set set = tm.entrySet();
        // Get an iterator
        Iterator i = set.iterator();
        // Display elements according to the order of the key.
        while (i.hasNext()) {
            Map.Entry me = (Map.Entry) i.next();
            System.out.print(me.getKey() + ": ");
            System.out.println(me.getValue());
        }
        System.out.println();
        // Deposit 1000 into John Doe‘s account
        double balance = ((Double) tm.get("zs")).doubleValue();
        tm.put("zs", new Double(balance + 1000));
        System.out.println("zs‘s new qian: " + tm.get("zs"));
    }
}

 

更多请见:https://blog.csdn.net/qq_44639795/article/details/103087211

java中TreeMap有什么用,举例说明?

原文:https://www.cnblogs.com/xiaolongxia1922/p/15004940.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!