星空网 > 软件开发 > Java

[javaSE] 集合框架(Map概述)

Map集合,将key对象映射到value对象

三个主要的子类:Hashtable,HashMap,TreeMap

 

Hashtable:底层是哈希表数据结构,不允许使用null值,线程同步

HashMap:底层是哈希表数据结构,允许使用null值,线程不同步

TreeMap:底层是二叉树数据结构,线程不同步,可以用于给Map集合中的键排序

 

使用keySet()方法遍历Map集合

调用Map对象的keySet()方法,得到Set对象,这里存储的是所有的键

 

import java.util.HashMap;import java.util.Map;import java.util.Set;import java.util.TreeMap;public class MapDemo {  /**   * @param args   */  public static void main(String[] args) {    Map<String,String> map=new HashMap<String,String>();    map.put("name", "taoshihan");    map.put("age", "100");    map.put("gender", "female");            Set<String> keySet=map.keySet();    for(String key:keySet){      System.out.println(map.get(key));    }        //TreeMap可排序    Map<StudentTreeMap,String> treeMap=new TreeMap<StudentTreeMap,String>();    treeMap.put(new StudentTreeMap("taoshihan1", 40), "陶士涵");    treeMap.put(new StudentTreeMap("taoshihan2", 30), "陶士涵2");    treeMap.put(new StudentTreeMap("taoshihan3", 50), "陶士涵3");    Set<StudentTreeMap> treeMapSet=treeMap.keySet();    for(StudentTreeMap key:treeMapSet){      System.out.println(key.name+"====="+key.age);    }  }}class StudentTreeMap implements Comparable<StudentTreeMap>{    public int age;  public String name;  public StudentTreeMap(String name,int age) {    this.name=name;    this.age=age;  }  @Override  public int compareTo(StudentTreeMap o) {    if(o.age<this.age){      return 1;    }else{      return -1;    }  }  }

 

[javaSE] 集合框架(Map概述)

 




原标题:[javaSE] 集合框架(Map概述)

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

去马尼拉的海运费:https://www.goluckyvip.com/tag/97451.html
马尼拉的海运:https://www.goluckyvip.com/tag/97452.html
到曼萨尼约的海运:https://www.goluckyvip.com/tag/97453.html
便宜的柬埔寨海运:https://www.goluckyvip.com/tag/97454.html
到塞尔维亚的海运:https://www.goluckyvip.com/tag/97455.html
海运费印尼:https://www.goluckyvip.com/tag/97456.html
西安到都江堰旅游攻略有什么?:https://www.vstour.cn/a/407236.html
9月张家界有什么好玩的地方 张家界有几个好玩的地方:https://www.vstour.cn/a/407237.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流