对象容器——HashMap,TreeMap

来源:互联网 发布:淘宝客店铺怎么做 编辑:程序博客网 时间:2024/05/16 12:40

与Set不同之处是,Map接口的对象将键映射至值,因此,在加载内容的时候,就会有键和值两个。读取的时候,直接按照键去读出值。

以下是HashMap的用法:

import java.util.*;
public class HashMapdemo{
public static void main(String[] args){
HashMap<String,String> hashmap=new HashMap<String,String>();
hashmap.put("lele","laolong");
hashmap.put("pipi","laonan");
System.out.print(hashmap.get("lele"));
System.out.print("  nihao  ");
System.out.print(hashmap.get("pipi"));
}
}

TreeMap也是相同的用法,只不过是在排序的时候,默认是根据字典排序的。

原创粉丝点击