⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 treemapdemo.java~2~

📁 java2参考大全上的例子的源码和自己的理解.
💻 JAVA~2~
字号:
package treemap;

/**
 TreeMap类通过使用树实现Map接口。TreeMap提供了按排序顺序存储关键字/值对的有
 效手段,同时允许快速检索。应该注意的是,不像散列映射,树映射保证它的元素按照关
 键字升序排序。
 下面的TreeMap构造函数定义为:
 TreeMap( )
 TreeMap(Comparator comp)
 TreeMap(Map m)
 TreeMap(SortedMap sm)
 第一种形式构造一个空树的映射,该映射使用其关键字的自然顺序来排序。第二种形
 式构造一个空的基于树的映射,该映射通过使用Comparator comp来排序(比较函数
 Comparators将在本章后面进行讨论)。第三种形式用从m的输入初始化树映射,该映射使
 用关键字的自然顺序来排序。第四种形式用从sm的输入来初始化一个树映射,该映射将按
 与sm相同的顺序来排序。
 TreeMap实现SortedMap并且扩展AbstractMap。而它本身并没有另外定义其他方法。
 下面的程序重新使前面的例子运转,以便在其中使用TreeMap:
 */

import java.util.*;

class TreeMapDemo {
  public static void main(String args[]) {

    // Create a tree map
    TreeMap tm = new TreeMap();

    // Put elements to the map
    tm.put("John Doe", new Double(3434.34));
    tm.put("Tom Smith", new Double(123.22));
    tm.put("Jane Baker", new Double(1378.00));
    tm.put("Todd Hall", new Double(99.22));
    tm.put("Ralph Smith", new Double( -19.08));

    // Get a set of the entries
    Set set = tm.entrySet();

    // Get an iterator
    Iterator i = set.iterator();

    // Display elements
    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("John Doe")).doubleValue();
    tm.put("John Doe", new Double(balance + 1000));
    System.out.println("John Doe's new balance: " +
                       tm.get("John Doe"));
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -