map学习笔记

collection是单列集合,map是双列集合。其中包含<k,v>键值对,注意:键具有唯一性,而值不唯一。

在此列举三个读取方式:keyset,valueset,及entryset。

keyset是获取所有键的集合。valueset是获取所有值得集合。entryset是获取所有条目的集合,entry是每一个条目的意思包含其中的(键与值)。

具体用法为:

 1 package javastudy;
 2 
 3 import java.util.Collection;
 4 import java.util.HashMap;
 5 import java.util.Iterator;
 6 import java.util.Map.Entry;
 7 import java.util.Set;
 8 
 9 public class TextMap {
10     public static void main(String args[])
11     {
12         //建立一个hashmap数组,(注意:因为因为map类型的是键值对,且值得类型首字母要大些)
13         //HashMap<Float, String> hp =new HashMap<Float ,String>();
14         HashMap<String,String> hp =new HashMap<String ,String>();
15         //用put定义,映射出实际的键与值
16         hp.put( "010", "北京");
17         hp.put( "021", "上海");
18         hp.put( "022", "重庆");
19         hp.put( "023", "天津");
20         //遍历打印其数组
21         //获得所有键的集合
22          Set<String> sd=hp.keySet();
23          Iterator<String> zips= sd.iterator();
24          while(zips.hasNext())
25          {
26              //得到键值
27              String zip=zips.next();
28              //因为键值唯一,所以从每个键中得到值,键唯一,值不唯一。
29              String cityName= hp.get(zip);
30              System.out.println(zip+":"+cityName);
31          }
32         //values得到所有值得集合
33          Collection<String> cs=hp.values();
34          Iterator<String> it = cs.iterator();
35          while(it.hasNext())
36          {
37             System.out.println(it.next());
38          }
39 
40         //entrySet是打印获得所有条目,entry=一行,一个条目的意思;
41          Set<Entry<String, String>> se=    hp.entrySet();
42          Iterator<Entry<String, String>> ie= se.iterator();
43          while(ie.hasNext())
44          {
45              //键唯一,值不唯一;getKey是得到键的集合,getValue得到所有值得集合;
46              Entry<String, String> es= ie.next();
47              System.out.println(es.getKey()+":"+es.getValue());
48          }
49          
50     }
51 
52 }

另外尝试利用自定义的概念建立一个比较器及其People的类的集合排序,具体为(如果有疑问参考上一章的具体介绍):

 1 package javastudy;
 2 
 3 import java.util.Comparator;
 4 import java.util.Iterator;
 5 import java.util.Map.Entry;
 6 import java.util.Set;
 7 import java.util.TreeMap;
 8 
 9 public class TextTree {
10 
11     public static void main(String[] args) {
12         // TODO Auto-generated method stub
13         TreeMap<People,People> tm= new TreeMap<People,People>(new MyComp()); 
14         tm.put(new People("Mark",12), new People("Jine",19));
15         tm.put(new People("Keven",16), new People("Bob",20));
16         Set<Entry<People,People>> se =tm.entrySet();
17         Iterator<Entry<People, People>> is=se.iterator();
18         while(is.hasNext())
19         {
20             Entry<People, People>es=is.next();
21             System.out.println(es.getKey()+"	"+es.getValue());
22         }
23     }
24 }
25 class MyComp implements Comparator<People>
26 {
27 
28     @Override
29     public int compare(People o1, People o2) {
30         // TODO Auto-generated method stub
31         return o1.name.compareTo(o2.name);
32     }
33     
34 } 
35 class People
36 {
37     String name;
38     int age;
39     People(String name,int age)
40     {
41         this.name=name;
42         this.age=age;
43     }
44     void show()
45     {
46         System.out.println(String.format("姓名=%s,年龄=%d", name,age));
47     }
48     @Override
49     public String toString() {
50         return "name=" + name + ", age=" + age ;
51     }
52 }
原文地址:https://www.cnblogs.com/xiadongqing/p/5082130.html