TreeMap用法 示例
2006-06-14 09:14
591 查看
/*
TreeMap类通过使用树来实现Map接口.TreeMap提供了按排序顺序存储关键字/值对的有效手段,同时允许快速检索。不像散列映射,树映射保证它的元素按照关键字升序排序。
*/
import java.util.*;
class TreeMapDemo{
public static void main(String[] args)
{
//Creat a tree map
TreeMap tm = new TreeMap();
//Put elements to the map
tm.put("Evan",new Double(12345.77));
tm.put("Rose",new Double(78777));
tm.put("Magic",new Double(-99.10));
tm.put("Mike",new Double(100.00));
tm.put("Sue",new Double(17.15));
//Get a set of 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.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
//Deposit 1000 into Evan's account
double balance = ((Double)tm.get("Evan")).doubleValue();
tm.put("Evan",new Double(balance + 1000));
System.out.println("Evan's new balance : " + tm.get("Evan"));
}
}
TreeMap类通过使用树来实现Map接口.TreeMap提供了按排序顺序存储关键字/值对的有效手段,同时允许快速检索。不像散列映射,树映射保证它的元素按照关键字升序排序。
*/
import java.util.*;
class TreeMapDemo{
public static void main(String[] args)
{
//Creat a tree map
TreeMap tm = new TreeMap();
//Put elements to the map
tm.put("Evan",new Double(12345.77));
tm.put("Rose",new Double(78777));
tm.put("Magic",new Double(-99.10));
tm.put("Mike",new Double(100.00));
tm.put("Sue",new Double(17.15));
//Get a set of 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.println(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
//Deposit 1000 into Evan's account
double balance = ((Double)tm.get("Evan")).doubleValue();
tm.put("Evan",new Double(balance + 1000));
System.out.println("Evan's new balance : " + tm.get("Evan"));
}
}
相关文章推荐
- Linux find 用法示例(摘抄)
- springMVC拦截器HandlerInterceptor用法代码示例
- Python操作json用法及json示例
- PHP实现的无限分类类库定义与用法示例【基于thinkPHP】
- MediaStore用法示例
- Python生成器generator用法示例
- iOS UITableView 用法二:源码示例
- mysql union用法代码示例
- STRUTS2标签中符号#,$,%的用法示例
- IN子查询的用法示例
- Python抽象和自定义类定义与用法示例
- java中关于时间的用法示例
- Java锁机制Lock用法示例
- javascript中confirm(确认),alert(警告),prompt(提问)的用法及其示例
- Linux find 用法示例
- STL中的Vector用法示例
- python中map的基本用法示例
- 倒影view和surfaceView的示例用法
- Android开发高级组件之自动完成文本框(AutoCompleteTextView)用法示例【附源码下载】
- MySQL函数大全及用法示例