HashMap的keyset与value的对应
来源:互联网 发布:青岛阿里云联系方式 编辑:程序博客网 时间:2024/05/05 17:22
Map怎么确保得values()和keyset()的顺序是一样的呢?
原来用map都是先取key然后得到value。由于在map上封装了,不能直接取得entryset,只有keyset和values(),getValue(key)。那values()和keyset()中的元素顺序是否对应呢?
首先,写个test,看看顺序是不是一致的:
- public class HashMapOrder {
- public staticvoid 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+", ");
- }
- }
- public class HashMapOrder {
- public staticvoid 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()
- 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 finalclass 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();
- }
- }
- 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 finalclass 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)
- 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;
- }
- 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;
- }
- Iterator<Map.Entry<K,V>> newEntryIterator() {
- return new EntryIterator();
- }
- private finalclass EntryIterator extends HashIterator<Map.Entry<K,V>> {
- public Map.Entry<K,V> next() {
- return nextEntry();
- }
- }
- java.util.HashMap.HashIterator.nextEntry()
- 。。。。。。
- Iterator<Map.Entry<K,V>> newEntryIterator() {
- return new EntryIterator();
- }
- private finalclass 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()
- 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;
- }
- 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();
- publicboolean hasNext() {
- return i.hasNext();
- }
- public V next() {
- return i.next().getValue();//!!!
- }
- publicvoid 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;
- }
0 0
- HashMap的keyset与value的对应
- HashMap的keyset与value的对应
- HashMap的entrySet与keySet
- HashMap的entrySet与keySet
- HashMap遍历:entrySet和keySet的比较
- jdk:HashMap的keySet(),values(),entrySet()
- HashMap底层之entrySet,keySet,value实现
- HashMap中keySet()以及values()方法的使用
- 发现Set<String> set = hashMap.keySet()的奥秘
- 遍历hashmap的键值一,通过 keySet()方法
- HashMap 中的keySet()和entrySet()方法的比较
- 关于遍历HashMap时用keySet、EntrySet之间的速度比较
- HashMap中keySet()以及values()方法的使用
- HashMap的keySet遍历和entrySet遍历时间效率比较
- 发现Set<String> set = hashMap.keySet()的奥秘
- HashMap的两种取出方式keySet和entrySet
- 高效编程之HashMap的entryset和keyset比较
- java 中如何遍历hashMap的key所对应的value?
- 求教oracle项目,把伪代码写成真的代码,能运行的,跪求帮助
- NSMutableString转换成NSString的N种方法
- Sublime Text 2 使用指南 + ubuntu 下安装 + 喜欢 sublime Text 理由
- Leap Motion开发第一步环境配置
- 算法之旅,直奔<algorithm>之十二 equal_range
- HashMap的keyset与value的对应
- 启动tomcat远程调试,懒程序员必备哦!
- Cocos2d-x2.0 各种粒子效果详解
- B. Little Elephant and Sorting
- MFC如何使控件大小随着对话框大小自动调整
- 环境变量JAVA_HOME, CLASSPATH和PATH的作用
- Objective-c中数字型字符串转换NSNumber的三种方法
- HACMP的网络环境(Boot、Service、persistent IP)
- fx_Fb