Java collections framework: Difference between revisions

Content deleted Content added
CopyOnWriteArrayList class: Add in-line citation
Map interfaces: Add subheadings
Line 186:
 
==Map interfaces==
Maps are defined by the '''{{Javadoc|module=java.base|package=java.util|class=Map|monotype=y}}''' interface in Java.
Maps are defined by the '''{{Javadoc|module=java.base|package=java.util|class=Map|monotype=y}}''' interface in Java. Maps are simple data structures that associate a key with an element. This lets the map be very flexible. If the key is the hash code of the element, the <code>Map</code> is essentially a <code>Set</code>. If it's just an increasing number, it becomes a list. <code>Map</code>s are implemented by <code>java.util.HashMap</code>, '''{{Javadoc|module=java.base|package=java.util|class=LinkedHashMap|monotype=y}}''' , and '''{{Javadoc|module=java.base|package=java.util|class=TreeMap|monotype=y}}'''.
 
===Map interface implementations===
Maps are defined by the '''{{Javadoc|module=java.base|package=java.util|class=Map|monotype=y}}''' interface in Java. Maps are simple data structures that associate a key with an element. This lets the map be very flexible. If the key is the hash code of the element, the <code>Map</code> is essentially a <code>Set</code>. If it's just an increasing number, it becomes a list. <code>Map</code>s are implemented by <code>java.util.HashMap</code>, '''{{Javadoc|module=java.base|package=java.util|class=LinkedHashMap|monotype=y}}''' , and '''{{Javadoc|module=java.base|package=java.util|class=TreeMap|monotype=y}}'''.
 
====HashMap====
<code>HashMap</code> uses a [[hash table]]. The hashes of the keys are used to find the elements in various buckets.
 
=====LinkedHashMap=====
<code>LinkedHashMap</code> extends <code>HashMap</code>by creating a [[doubly linked list]] between the elements, allowing them to be accessed in the order in which they were inserted into the map. <code>LinkedHashMap</code> contains a <code>protected</code> <code>removeEldestEntry</code> method which is called by the <code>put</code> method whenever a new key is added to the <code>Map</code>.{{sfn|Bloch|2018|loc=Chapter §44 Favor the use of standard functional interfaces|pp=199-202}} The <code>Map</code> removes its eldest entry whenever <code>removeEldestEntry</code> returns true.{{sfn|Bloch|2018|loc=Chapter §44 Favor the use of standard functional interfaces|pp=199-202}} The <code>removeEldestEntry</code> method can be overridden.{{sfn|Bloch|2018|loc=Chapter §44 Favor the use of standard functional interfaces|pp=199-202}}
 
====TreeMap====
<code>TreeMap</code>, in contrast to <code>HashMap</code> and <code>LinkedHashMap</code>, uses a red–black tree. The keys are used as the values for the nodes in the tree, and the nodes point to the elements in the <code>Map</code>.<ref>{{cite web|url=http://docs.oracle.com/javase/7/docs/api/java/util/Map.html |title=Map (Java Platform SE 7 ) |publisher=Docs.oracle.com |date=2013-06-06 |access-date=2013-08-16}}</ref>