HashMap的keyset与value的对应

Map怎么确保得values()和keyset()的顺序是一样的呢?

 

原来用map都是先取key然后得到value。由于在map上封装了,不能直接取得entryset,只有keyset和values(),getValue(key)。那values()和keyset()中的元素顺序是否对应呢?

 

首先,写个test,看看顺序是不是一致的:

 

Java代码 复制代码  收藏代码
  1. public class HashMapOrder {   
  2.        
  3.     public static void main(String[] args) {   
  4.         HashMap map = new HashMap();   
  5.            
  6. //      map.put(1, "E");   
  7. //      map.put(2, "D");   
  8. //      map.put(3, "C");   
  9. //      map.put(4, "B");   
  10. //      map.put(5, "A");   
  11.   
  12.         map.put(12"E");   
  13.         map.put(2"D");   
  14.         map.put(31"C");   
  15.         map.put(4"B");   
  16.         map.put(56"A");   
  17.            
  18.         for(Object key:map.keySet())   
  19.             System.out.print(key + ", ");   
  20.            
  21.         System.out.println();   
  22.            
  23.         for(Object value:map.values())   
  24.             System.out.print(value+", ");   
  25.            
  26.     }   
  27.   
  28. }  
public class HashMapOrder {
	
	public static void main(String[] args) {
		HashMap map = new HashMap();
		
//		map.put(1, "E");
//		map.put(2, "D");
//		map.put(3, "C");
//		map.put(4, "B");
//		map.put(5, "A");

		map.put(12, "E");
		map.put(2, "D");
		map.put(31, "C");
		map.put(4, "B");
		map.put(56, "A");
		
		for(Object key:map.keySet())
			System.out.print(key + ", ");
		
		System.out.println();
		
		for(Object value:map.values())
			System.out.print(value+", ");
		
	}

}
 

 

发现没问题,第一个注释了的代码数据是有序的。输出

1, 2, 3, 4, 5, 

E, D, C, B, A, 

第二个无序数据输出:

2, 4, 56, 12, 31, 

D, B, A, E, C, 

也正常!

 

没问题,查看代码理解如下:

HashMap中存储数据的对象本质是有一个数组!使用Entry[]保存keyvalue对,  然后如果是一样的index会以链表的形式加在entry的next上。

keyset()和value()返回的都是匿名内部类对象,使用iterator迭代器对table的一个迭代!

 

首先,说明entryset()的来龙气脉,后面values()和keyset()都会用到这个方法。

java.util.HashMap.entrySet()

 

Java代码 复制代码  收藏代码
  1. public Set<Map.Entry<K,V>> entrySet() {   
  2.     return entrySet0();   
  3. }   
  4.   
  5. private Set<Map.Entry<K,V>> entrySet0() {   
  6.     Set<Map.Entry<K,V>> es = entrySet;   
  7.     return es != null ? es : (entrySet = new EntrySet());   
  8. }   
  9. // 内部类,没有static!   
  10. private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {   
  11.     public Iterator<Map.Entry<K,V>> iterator() {   
  12.         return newEntryIterator(); //!!!   
  13.     }   
  14.     public boolean contains(Object o) {   
  15.         if (!(o instanceof Map.Entry))   
  16.             return false;   
  17.         Map.Entry<K,V> e = (Map.Entry<K,V>) o;   
  18.         Entry<K,V> candidate = getEntry(e.getKey()); // !!!   
  19.         return candidate != null && candidate.equals(e);   
  20.     }   
  21.     public boolean remove(Object o) {   
  22.         return removeMapping(o) != null;   
  23.     }   
  24.     public int size() {   
  25.         return size;   
  26.     }   
  27.     public void clear() {   
  28.         HashMap.this.clear();   
  29.     }   
  30. }  
    public Set<Map.Entry<K,V>> entrySet() {
        return entrySet0();
    }

    private Set<Map.Entry<K,V>> entrySet0() {
        Set<Map.Entry<K,V>> es = entrySet;
        return es != null ? es : (entrySet = new EntrySet());
    }
    // 内部类,没有static!
    private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
        public Iterator<Map.Entry<K,V>> iterator() {
            return newEntryIterator(); //!!!
        }
        public boolean contains(Object o) {
            if (!(o instanceof Map.Entry))
                return false;
            Map.Entry<K,V> e = (Map.Entry<K,V>) o;
            Entry<K,V> candidate = getEntry(e.getKey()); // !!!
            return candidate != null && candidate.equals(e);
        }
        public boolean remove(Object o) {
            return removeMapping(o) != null;
        }
        public int size() {
            return size;
        }
        public void clear() {
            HashMap.this.clear();
        }
    }
 

java.util.HashMap.getEntry(Object)

 

Java代码 复制代码  收藏代码
  1. final Entry<K,V> getEntry(Object key) {   
  2.     int hash = (key == null) ? 0 : hash(key.hashCode());   
  3.     for (Entry<K,V> e = table[indexFor(hash, table.length)];   
  4.          e != null;   
  5.          e = e.next) {   
  6.         Object k;   
  7.         if (e.hash == hash &&   
  8.             ((k = e.key) == key || (key != null && key.equals(k))))   
  9.             return e;   
  10.     }   
  11.     return null;   
  12. }  
    final Entry<K,V> getEntry(Object key) {
        int hash = (key == null) ? 0 : hash(key.hashCode());
        for (Entry<K,V> e = table[indexFor(hash, table.length)];
             e != null;
             e = e.next) {
            Object k;
            if (e.hash == hash &&
                ((k = e.key) == key || (key != null && key.equals(k))))
                return e;
        }
        return null;
    }
 

 

Java代码 复制代码  收藏代码
  1.     Iterator<Map.Entry<K,V>> newEntryIterator()   {   
  2.         return new EntryIterator();   
  3.     }   
  4.   
  5.     private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {   
  6.         public Map.Entry<K,V> next() {   
  7.             return nextEntry();   
  8.         }   
  9.     }   
  10.   
  11. java.util.HashMap.HashIterator.nextEntry()   
  12.     。。。。。。  
    Iterator<Map.Entry<K,V>> newEntryIterator()   {
        return new EntryIterator();
    }

    private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
        public Map.Entry<K,V> next() {
            return nextEntry();
        }
    }

java.util.HashMap.HashIterator.nextEntry()
    。。。。。。
 

再来看keyset()和value()方法,通过iterator迭代entrySet,即获取Entry[]数组中的元素,然后分别取对应的key和value:

java.util.AbstractMap.keySet()

 

Java代码 复制代码  收藏代码
  1. public Set<K> keySet() {   
  2.     if (keySet == null) {   
  3.         keySet = new AbstractSet<K>() {   
  4.             public Iterator<K> iterator() {   
  5.                 return new Iterator<K>() {   
  6.                     private Iterator<Entry<K,V>> i = entrySet().iterator();   
  7.   
  8.                     public boolean hasNext() {   
  9.                         return i.hasNext();   
  10.                     }   
  11.   
  12.                     public K next() {   
  13.                         return i.next().getKey(); //!!!   
  14.                     }   
  15.   
  16.                     public void remove() {   
  17.                         i.remove();   
  18.                     }   
  19.                 };   
  20.             }   
  21.   
  22.             public int size() {   
  23.                 return AbstractMap.this.size();   
  24.             }   
  25.   
  26.             public boolean isEmpty() {   
  27.                 return AbstractMap.this.isEmpty();   
  28.             }   
  29.   
  30.             public void clear() {   
  31.                 AbstractMap.this.clear();   
  32.             }   
  33.   
  34.             public boolean contains(Object k) {   
  35.                 return AbstractMap.this.containsKey(k);   
  36.             }   
  37.         };   
  38.     }   
  39.     return keySet;   
  40. }   
  41.   
  42. public Collection<V> values() {   
  43.     if (values == null) {   
  44.         values = new AbstractCollection<V>() {   
  45.             public Iterator<V> iterator() {   
  46.                 return new Iterator<V>() {   
  47.                     private Iterator<Entry<K,V>> i = entrySet().iterator();   
  48.   
  49.                     public boolean hasNext() {   
  50.                         return i.hasNext();   
  51.                     }   
  52.   
  53.                     public V next() {   
  54.                         return i.next().getValue();//!!!   
  55.                     }   
  56.   
  57.                     public void remove() {   
  58.                         i.remove();   
  59.                     }   
  60.                 };   
  61.             }   
  62.   
  63.             public int size() {   
  64.                 return AbstractMap.this.size();   
  65.             }   
  66.   
  67.             public boolean isEmpty() {   
  68.                 return AbstractMap.this.isEmpty();   
  69.             }   
  70.   
  71.             public void clear() {   
  72.                 AbstractMap.this.clear();   
  73.             }   
  74.   
  75.             public boolean contains(Object v) {   
  76.                 return AbstractMap.this.containsValue(v);   
  77.             }   
  78.         };   
  79.     }   
  80.     return values;   
  81. }  
    public Set<K> keySet() {
        if (keySet == null) {
            keySet = new AbstractSet<K>() {
                public Iterator<K> iterator() {
                    return new Iterator<K>() {
                        private Iterator<Entry<K,V>> i = entrySet().iterator();

                        public boolean hasNext() {
                            return i.hasNext();
                        }

                        public K next() {
                            return i.next().getKey(); //!!!
                        }

                        public void remove() {
                            i.remove();
                        }
                    };
                }

                public int size() {
                    return AbstractMap.this.size();
                }

                public boolean isEmpty() {
                    return AbstractMap.this.isEmpty();
                }

                public void clear() {
                    AbstractMap.this.clear();
                }

                public boolean contains(Object k) {
                    return AbstractMap.this.containsKey(k);
                }
            };
        }
        return keySet;
    }

    public Collection<V> values() {
        if (values == null) {
            values = new AbstractCollection<V>() {
                public Iterator<V> iterator() {
                    return new Iterator<V>() {
                        private Iterator<Entry<K,V>> i = entrySet().iterator();

                        public boolean hasNext() {
                            return i.hasNext();
                        }

                        public V next() {
                            return i.next().getValue();//!!!
                        }

                        public void remove() {
                            i.remove();
                        }
                    };
                }

                public int size() {
                    return AbstractMap.this.size();
                }

                public boolean isEmpty() {
                    return AbstractMap.this.isEmpty();
                }

                public void clear() {
                    AbstractMap.this.clear();
                }

                public boolean contains(Object v) {
                    return AbstractMap.this.containsValue(v);
                }
            };
        }
        return values;
    }
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值