标签:
list,map,set的区别(首先假定小猪都是同一个细胞克隆出来的)List=排成一长队的小猪Map=放在一个个,有房间号的屋子里面的一群小猪Set=一群小猪贴上号,然后赶到一个猪圈里Map<String,String> map =newHashMap<String,String>();
map.put("1","001");
map.put("2","002");
map.put("3","003");
String key ="";
String value ="";
// 遍历1 keySet方式
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
key = it.next();
value = map.get(key);
System.out.println(key +" : "+ value);
}
// 遍历2 增强for循环
for(String string : map.keySet()){
value = map.get(string);
System.out.println(string +" : "+ value);
}
// 遍历3 entrySet
Iterator<Entry<String,String>> iter = map.entrySet().iterator();
Entry<String,String> entry;
while(iter.hasNext()){
entry = iter.next();
key = entry.getKey();
value = entry.getValue();
System.out.println(key +" : "+ value);
}
// 遍历4 entrySet foreach
for(Entry<String,String> entry2 : map.entrySet()){
key = entry2.getKey();
value = entry2.getValue();
System.out.println(key +" : "+ value);
}
1 publicboolean containsKey(Object key){ 2 Object k = maskNull(key); 3 int hash = hash(k); 4 int i = indexFor(hash, table.length); 5 Entry e = table[i]; 6 while(e !=null){ 7 if(e.hash == hash && eq(k, e.key)) 8 returntrue; 9 e = e.next; 10 } 11 returnfalse; 12 }
1 publicboolean containsValue(Object value){ 2 if(value ==null) 3 return containsNullValue(); 4 Entry[] tab = table; 5 for(int i =0; i < tab.length ; i++) 6 for(Entry e = tab[i]; e !=null; e = e.next) 7 if(value.equals(e.value)) 8 returntrue; 9 returnfalse; 10 }
1 public V put(K key, V value){ 2 K k = maskNull(key); 3 int hash = hash(k); 4 int i = indexFor(hash, table.length); 5 for(Entry<K,V> e = table[i]; e !=null; e = e.next){ 6 if(e.hash == hash && eq(k, e.key)){ 7 V oldValue = e.value; 8 e.value = value; 9 e.recordAccess(this); 10 return oldValue; 11 } 12 } 13 modCount++; 14 addEntry(hash, k, value, i); 15 returnnull; 16 }
1 public V remove(Object key){ 2 Entry<K,V> e = removeEntryForKey(key); 3 return(e ==null?null: e.value); 4 }
1 /** 2 * Removes all mappings from this map. 3 */ 4 publicvoid clear(){ 5 modCount++; 6 Entry[] tab = table; 7 for(int i =0; i < tab.length; i++) 8 tab[i]=null; 9 size =0; 10 }
1 publicboolean equals(Object o){ 2 if(!(o instanceofMap.Entry)) 3 returnfalse; 4 Map.Entry e =(Map.Entry)o; 5 Object k1 = getKey(); 6 Object k2 = e.getKey(); 7 if(k1 == k2 ||(k1 !=null&& k1.equals(k2))){ 8 Object v1 = getValue(); 9 Object v2 = e.getValue(); 10 if(v1 == v2 ||(v1 !=null&& v1.equals(v2))) 11 returntrue; 12 } 13 returnfalse; 14 } 15 publicint hashCode(){ 16 return(key==NULL_KEY ?0: key.hashCode())^ 17 (value==null?0: value.hashCode()); 18 } 19 publicString toString(){ 20 return getKey()+"="+ getValue(); 21 }
1 publicSet<K> keySet(){ 2 Set<K> ks = keySet; 3 return(ks !=null? ks :(keySet =newKeySet())); 4 }
1 publicSet<Map.Entry<K,V>> entrySet(){ 2 Set<Map.Entry<K,V>> es = entrySet; 3 return(es !=null? es :(entrySet =(Set<Map.Entry<K,V>>)(Set)newEntrySet())); 4 }
1 publicCollection<V> values(){ 2 Collection<V> vs = values; 3 return(vs !=null? vs :(values =newValues())); 4 }
标签:
原文地址:http://www.cnblogs.com/liubo6/p/4491189.html