Java Notes-10

Summay: The Map Interface, Collection implementations

-Maps store and retrieve elements with key values

e.g:

[java]  view plain copy
  1. Map<String, Date> dateMap = new HashMap<String, Date>();  
  2. dateMap.put( "today"new Date() );  
  3. Date today = dateMap.get( "today" );  

-Functions:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public V put( K key , V value )//This method adds the specified key/value pair to the map.   
  2. public V get( K key )//This method retrieves the value corresponding to  key from the map.  
  3. public V remove( K key )//This method removes the value corresponding to  key from the map  
  4. public int size()//Use this method to find the number of key/value pairs in this map  
  5. public Set keySet()//This method returns a  Set that contains all the keys in this map.  
  6. public Collection values()//Use this method to retrieve all the values in this map  

-Map has one child interface,  SortedMap . A  SortedMap maintains its key/value pairs sorted
in a particular order according to the key values. 

-All of the methods of the  Collection interface would appear to make sense for  Map ,
except for  iterator() . A  Map , again, has two sets of objects: keys and values, and separate
iterators for each. This is why a  Map does not implement  Collection 

-ConcurrentMap, It extends the base  Map interface and adds atomic put, remove, and replace functionality that is
useful for concurrent programming

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. public V putIfAbsent( K key, V value )//This method associates the value with the key only if the key was not already in use.  
  2. public boolean remove( Object key, Object value )//This method removes the mapping (key and value) only if the current value asso‐  
  3. ciated with the key equals the supplied value  
  4. public boolean replace( K key, V existingValue, V newValue)//This method replaces the value associated with the key only if the existing value  
  5. equals the  existingValue argument.  
  6. public boolean replace( K key, V value )//This method replaces the value associated with the key only if a mapping already  
  7. exists for the key  

-Arrays are described as consuming constant time for retrieval, but linear time for insertion into or deletion from the body of the array. 

-Arrays are useful when you are mostly reading or exclusively appending to the end of the collection.

-Link List  holds its elements in a chain of nodes, each referencing the node before and after it (if any).


-Unlike the magic of an array, however, to retrieve an element from a linked list, you must traverse the list from either the head or
tail to reach the correct spot. As you might have guessed, this is a linear-time operation that gets more expensive as the number of elements grows. 

-Linked lists are useful when you are doing a lot of insertions or deletions on a collection

-Trees are useful for maintaining and searching large collections of sorted elements.




-A hash map (or hash table, as it is also called) uses a mathematical hash algorithm applied to its key value to distribute its element



-Java Collections implementations

Collections Framework implementation classes
InterfaceImplementation
SetHashSet
LinkedHashSet
CopyOnWriteArraySet
EnumSet
CopyOnWriteArraySet
SortedSet TreeSet
ConcurrentSkipListSet
ListArrayList
LinkedList
Vector a
Stack
CopyOnWriteArrayList
MapHashMap
EnumMap
LinkedHashMap
IdentityHashMap
Hashtable a
ConcurrentMapConcurrentHashMap
ConcurrentSkipListMap
SortedMapTreeMap
Queue / DequeueLinkedList
ArrayDeque
PriorityQueue
DelayQueue
SynchronousQueue
ConcurrentLinkedQueue
ConcurrentLinkedDequeue
BlockingQueueArrayBlockingQueue
LinkedBlockingQueue
PriorityBlockingQueue
  

Next Synchronized and Unsynchronized Collections...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值