Map基本概念

数据结构中Map是一种重要的形式。Map接口定义的是查询表,或称查找表,其用于储存所谓的键/值对(key-value pair),其中key是映射表的索引。

JDK结构中还存在实现Map类似功能的遗留集合:

Hashtable(线程安全的散列映射表)

Properties(属性映射表),常用于配置文件(如db.properties)。

Map接口源代码

package java.util;

/* 映射表(查询表)泛型接口 */
public interface Map<K,V> { /* 基本与Collection相同,返回映射表中key-value映射对数量(内部属性size) */ int size(); /* 基本与Collection相同,返回映射表是否包含映射对 */ boolean isEmpty(); /* 返回映射表是否包含指定的键 */ boolean containsKey(Object key); /* 返回映射表是否包含指定的值,或者说指定的值是否有大于等于1个映射的键 */ boolean containsValue(Object value); /* 如果映射表中存在指定key,返回此key的值;否则,返回null */ V get(Object key); /* 将键值对放入映射表中。
* 如果键存在,则用现在的值替换原有值,返回原有值对象;
* 如果键不存在则存入键值对,返回null */
*/ V put(K key, V value); /* 移除指定键对应的值。如果存在键,则移除,并返回移除值对象;反之,则返回null */ V remove(Object key); /* 复制另一张映射表元素到本映射表中 */
void putAll(Map<? extends K, ? extends V> m); /* 基本同Collection,清空映射表 */ void clear(); /* 获得键的Set集合 */ Set<K> keySet(); /* 获得值的Collection集合 */ Collection<V> values(); /* 获得键值对的Set集合 */ Set<Map.Entry<K, V>> entrySet(); /* 内部接口 Entry<K,V> */ interface Entry<K,V> { /* 获取键值对的键 */ K getKey(); /* 获取键值对的值 */ V getValue(); /* 设置键值对的值 */ V setValue(V value); /* 比较entry(键值对) */ boolean equals(Object o); /* 生成entry对象的hash值 */ int hashCode(); } /* 比较映射表 */ boolean equals(Object o); /* 生成映射表对象的hash码*/ int hashCode(); }

深入理解码源

对象比较好基友:equals(Object obj)hashcode()

Map<K, V>接口及其内部接口Entry<K, V>都有这俩方法。如此设计,目的就是规范其实现子类,要求子类必须重写Object类的这俩方法,从而完成映射表这种数据结构的既定思想。

boolean equals(Object o);   // 对象比较
int hashCode();             // 哈希码

集合框架通用方法:size()isEmpty()

集合框架(包括Collection接口及其子接口ListSetMap接口)内部维护一个size属性,其描述用户提供可操纵元素数量,通过size()方法向用户提供可见性,通过isEmpty()方法向用户说明是否集合中仍存在其可操纵的元素。

int size();                // 元素保有量
boolean isEmpty();         // 元素数量是否为0 

键、值存在性判断:containsKey(Object key)containsValue(Object value)

boolean containsKey(Object key);       // 映射表是否包含指定键的元素
boolean containsValue(Object value);   // 映射表是否包含指定值得元素

映射表增删查改:

增、改  V put(K key, V value)  V putAll(Map<? extends K, ? value V> m)

删        V remove(Object key)   void clear()

查        V get(Object key)

V put(K key, V value)    // 放入或替换指定key的键值对      
V putAll(Map<? extends K, ? value V> m)    // 将另一映射表所有元素放入本映射表
V remove(Object key)     // 移除指定key的键值对 
V get(Object key)        // 获取指定key的键值对
void clear()             // 清除所有映射表元素 
package com.forget406.study;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class MapStudy {
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void main(String[] args) {
        /* JDK 1.7允许后面的尖括号内不写泛型变量 */
        Map<String, Coordinate> rect = 
            new HashMap<>();
        rect.put("point A", new Coordinate(0, 0));
        rect.put("point B", new Coordinate(1, 0));
        rect.put("point C", new Coordinate(1, 1));
        rect.put("point D", new Coordinate(0, 1));
        
        Map<String, Coordinate> line =
            new TreeMap<String, Coordinate>();
        line.put("point A", new Coordinate(0, 0));
        line.put("point B", new Coordinate(3, 3));
        
        /***** 实验测试部分  *****/
        System.out.println(rect);  // output rectangle
        System.out.println(line);  // output line
        System.out.println(rect.put("point D", new Coordinate(2, 2)));  // (0, 1)
        System.out.println(rect.get("point C"));  // (1,1) 
        System.out.println(rect.get("point E"));  // null
        rect.putAll(line);
        System.out.println(rect); 
        System.out.println(line.remove("point C"));  // null
        System.out.println(line.remove("point A"));  // (0, 0)    
    }

}

/**
 * 坐标类
 * 
 * @author forget406
 * @since  09/08/2016
 * @param <T> 泛型参数
 */
class Coordinate<T> {
    private T x;
    private T y;
    
    public Coordinate(T x, T y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((x == null) ? 0 : x.hashCode());
        result = prime * result + ((y == null) ? 0 : y.hashCode());
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Coordinate<T> other = (Coordinate<T>) obj;
        if (x == null) {
            if (other.x != null)
                return false;
        } else if (!x.equals(other.x))
            return false;
        if (y == null) {
            if (other.y != null)
                return false;
        } else if (!y.equals(other.y))
            return false;
        return true;
    }
    
    @Override
    public String toString() {
        return "(" + x + "," + y + ")";
    }
}
{point C=(1,1), point B=(1,0), point A=(0,0), point D=(0,1)}
{point A=(0,0), point B=(3,3)}
(0,1)
(1,1)
null
{point C=(1,1), point B=(3,3), point A=(0,0), point D=(2,2)}
null
(0,0)
程序运行结果 

相关文章:

  • 2021-04-01
  • 2021-11-19
  • 2021-03-31
  • 2021-06-13
  • 2022-12-23
  • 2021-04-20
  • 2021-11-24
猜你喜欢
  • 2021-09-07
  • 2022-12-23
  • 2021-09-08
  • 2021-05-27
  • 2022-12-23
  • 2021-07-21
  • 2021-05-31
相关资源
相似解决方案