java-map类

双列集合:
-------------| Map 如果是实现了Map接口的集合类,具备的特点: 存储的数据都是以键值对的形式存在的,键不可重复,值可以重复。
----------------| HashMap
----------------| TreeMap
----------------| Hashtable

Map接口的方法:
添加

  1. put(K key, V value)
  2. putAll(Map<? extends K,? extends V> m)


删除

  1. remove(Object key)
  2. clear()

获取

  1. get(Object key)
  2. size()

判断

  1. containsKey(Object key)
  2. containsValue(Object value)

isEmpty()

public class Demo2 {    
    public static void main(String[] args) {
        Map<String,String> map = new HashMap<String, String>();
        //添加方法
        map.put("汪峰", "章子怡");
        map.put("文章", "马伊琍");
        map.put("谢霆锋","张柏芝");
        /*
        添加
        System.out.println("返回值:"+map.put("谢霆锋","黄菲"));  // 如果之前没有存在该键,那么返回的是null,如果之前就已经存在该键了,那么就返回该键之前对应 的值。
        Map<String,String> map2 = new HashMap<String, String>();
        map2.put("杨振宁", "翁帆");
        map2.put("习总", "彭丽媛");
        map.putAll(map2); // 把map2的元素添加到map集合中。
        
        */
        
        /*
        删除
        System.out.println("删除的数据是:"+map.remove("汪峰")) ;  //根据键删除一条map中的数据,返回的是该键对应 的值。
        map.clear(); //清空集合中的所有数据。
        */
        
        /* 获取
        System.out.println("根据指定 的键获取对应的值:"+ map.get("文章"));
        System.out.println("获取map集合键值对个数:"+map.size());
        
        
        判断
        System.out.println("判断map集合是否包含指定的键:"+ map.containsKey("文章"));
        System.out.println("判断map集合中是否包含指定 的值:"+ map.containsValue("张柏芝"));
        map.clear();
        System.out.println("判断map集合是否为空元素:"+ map.isEmpty());
        */
        System.out.println("集合的元素:"+ map);
    }
}

一、实现类HasMap:底层也是基于哈希表实现的

  往HashMap添加元素的时候,首先会调用键的hashCode方法得到元素 的哈希码值,然后经过运算就可以算出该

元素在哈希表中的存储位置。
  情况1: 如果算出的位置目前没有任何元素存储,那么该元素可以直接添加到哈希表中。

  情况2:如果算出 的位置目前已经存在其他的元素,那么还会调用该元素的equals方法与这个位置上的元素进行比较,如果equals方法返回 的是false,那么该元素允许被存储,如果equals方法返回的是true,那么该元素被视为重复元素,不允存储。

  

 1 class Emp {
 2     
 3     String name;
 4     
 5     int salary;
 6 
 7     public Emp(String name, int salary) {
 8         super();
 9         this.name = name;
10         this.salary = salary;
11     }
12     
13     
14     @Override
15     public String toString() {
16         return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";
17     }
18 
19     @Override
20     public int compareTo(Emp o) {
21         return this.salary - o.salary;
22     }
23     
24 }
25 public class Demo6 {
26     
27     public static void main(String[] args) {
28     /*    TreeMap<Character, Integer> tree = new TreeMap<Character, Integer>();
29         tree.put('c',10);
30         tree.put('b',2);
31         tree.put('a',5);
32         tree.put('h',12);
33         System.out.println(tree);*/
34         
35         
36         
37         TreeMap<Emp, String> tree = new TreeMap<Emp, String>();
38         tree.put(new Emp("冰冰", 2000),"001");
39         tree.put(new Emp("家宝", 1000),"002");
40         tree.put(new Emp("习总", 3000),"003");
41         tree.put(new Emp("克强", 5000),"005");
42         System.out.println(tree);
43         
44         
45         
46         
47     }
48 
49 }

二、实现类TreeMap TreeMap也是基于红黑树(二叉树)数据结构实现 的, 特点:会对元素的键进行排序存储。

TreeMap 要注意的事项:
1. 往TreeMap添加元素的时候,如果元素的键具备自然顺序,那么就会按照键的自然顺序特性进行排序存储。
2. 往TreeMap添加元素的时候,如果元素的键不具备自然顺序特性, 那么键所属的类必须要实现Comparable接口,把键
的比较规则定义在CompareTo方法上。

3. 往TreeMap添加元素的时候,如果元素的键不具备自然顺序特性,而且键所属的类也没有实现Comparable接口,那么就必须
在创建TreeMap对象的时候传入比较器

 1 class Emp {//implements Comparable<Emp>{
 2     
 3     String name;
 4     
 5     int salary;
 6 
 7     public Emp(String name, int salary) {
 8         super();
 9         this.name = name;
10         this.salary = salary;
11     }
12     
13     
14     @Override
15     public String toString() {
16         return "[姓名:"+this.name+" 薪水:"+ this.salary+"]";
17     }
18 
19 /*
20     @Override
21     public int compareTo(Emp o) {
22         return this.salary - o.salary;
23     }*/
24     
25 }
26 
27 
28 //自定义一个比较器
29 class MyComparator implements Comparator<Emp>{
30 
31     @Override
32     public int compare(Emp o1, Emp o2) {
33         return o1.salary - o2.salary;
34     }
35     
36 }
37 
38 
39 
40 
41 public class Demo6 {
42     
43     public static void main(String[] args) {
44     /*    TreeMap<Character, Integer> tree = new TreeMap<Character, Integer>();
45         tree.put('c',10);
46         tree.put('b',2);
47         tree.put('a',5);
48         tree.put('h',12);
49         System.out.println(tree);*/
50         
51         //创建一个自定义比较器
52         MyComparator comparator = new MyComparator();
53         
54         TreeMap<Emp, String> tree = new TreeMap<Emp, String>(comparator);
55         tree.put(new Emp("冰冰", 2000),"001");
56         tree.put(new Emp("家宝", 1000),"002");
57         tree.put(new Emp("习总", 3000),"003");
58         tree.put(new Emp("克强", 5000),"005");
59         
60         tree.put(new Emp("财厚", 5000),"008");
61         System.out.println(tree);
62         
63         
64         
65         
66     }
67 
68 }

 

原文地址:https://www.cnblogs.com/chenzxl/p/7803856.html