java_集合体系之Map框架相关抽象类接口详解、源码――08(三)

2014-11-24 07:32:21 · 作者: · 浏览: 3
b.append(key == this "(this Map)" : key); sb.append('='); sb.append(value == this "(this Map)" : value); if (! i.hasNext()) return sb.append('}').toString(); sb.append(", "); } } /** 返回当前Map的克隆的值*/ protected Object clone() throws CloneNotSupportedException { AbstractMap result = (AbstractMap )super.clone(); result.keySet = null; result.values = null; return result; } /** 比较两个对象是否相等*/ private static boolean eq(Object o1, Object o2) { return o1 == null o2 == null : o1.equals(o2); } // Implementation Note: SimpleEntry and SimpleImmutableEntry // are distinct unrelated classes, even though they share // some code. Since you can't add or subtract final-ness // of a field in a subclass, they can't share representations, // and the amount of duplicated code is too small to warrant // exposing a common abstract class. /** 维护键和值的 Entry、此类支持setValue*/ public static class SimpleEntry implements Entry , java.io.Serializable { private static final long serialVersionUID = -8499721149061103585L; private final K key; private V value; public SimpleEntry(K key, V value) { this.key = key; this.value = value; } public SimpleEntry(Entry entry) { this.key = entry.getKey(); this.value = entry.getValue(); } public K getKey() { return key; } public V getValue() { return value; } public V setValue(V value) { V oldValue = this.value; this.value = value; return oldValue; } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry e = (Map.Entry)o; return eq(key, e.getKey()) && eq(value, e.getValue()); } public int hashCode() { return (key == null 0 : key.hashCode()) ^ (value == null 0 : value.hashCode()); } public String toString() { return key + "=" + value; } } /**维护不可变的键和值的 Entry、此类不支持 setValue 方法。一般用在多线程环境中*/ public static class SimpleImmutableEntry implements Entry , java.io.Serializable { private static final long serialVersionUID = 7138329143949025153L; private final K key; private final V value; public SimpleImmutableEntry(K key, V value) { this.key = key; this.value = value; } public SimpleImmutableEntry(Entry entry) { this.key = entry.getKey(); this.value = entry.getValue(); } public K getKey() { return key; } public V getValue() { return value; } public V setValue(V value) { throw new UnsupportedOperationException(); } public boolean equals(Object o) { if (!(o instanceof Map.Entry)) return false; Map.Entry e = (Map.Entry)o; return eq(key, e.getKey()) && eq(value, e.getValue()); } public int hashCode() { return (key == null 0 : key.hashCode()) ^ (value == null 0 : value.hashCode()); } public String toString() { return key + "=" + value; } } }

四:SortedMap


1、接口简介:

a)进一步提供关于键的总体排序的Map。该映射是根据其键的自然顺序进行排序的,或者根据通常在创建有序映射时提供的Comparator 进行排序

b)插入所有有序映射的键都要实现Comparable接口、所有这些键都是可以比较的。

c)内部方法都是围绕key的排序定义的、概括为: (1)获取大于、小于指定键的子Map、 (2)获取key所在指定范围的子集Map。获取第一个、最后一个键、 (3)获取三种视图的方法。(4)获取当前集合的排序比较器。

2、源码分析:

package com.chy.collection.core;

import java.util.Comparator;


public interface SortedMap
  
    extends Map
   
     { /** 返回对此映射中的键进行排序的比较器;如果此映射使用键的自然顺序,则返回 null。*/ Comparator
     comparator(); /** 返回此映射的部分视图,其键值的范围从 fromKey(包括)到 toKey(不包括)。*/ SortedMap
    
      subMap(K fromKey, K toKey); /** 返回此映射的部分视图,其键值严格小于 toKey。*/ SortedMap
     
       headMap(K toKey); /** 返回此映射的部分视图,其键大于等于 fromKey。*/ SortedMap
      
        tailMap(K fromKey); /** 返回此映射中当前第一个(最低)键。*/ K firstKey(); /** 返回此映射中当前第一个(最低)键。*/ K lastKey(); /** 返回在此映射中所包含键的 Set 视图。*/ Set