Content deleted Content added
removed Category:Java (programming language); added Category:JDK components using HotCat |
m instatiate→instantiate - toolforge:typos |
||
(7 intermediate revisions by 7 users not shown) | |||
Line 15:
<code>Collection</code>s cannot hold primitive data types such as <code>int</code>, <code>long</code>, or <code>double</code>.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}} Instead, <code>Collection</code>s can hold [[wrapper class]]es such as {{Javadoc|module=java.base|package=java.lang|class=Integer|monotype=y}}, {{Javadoc|module=java.base|package=java.lang|class=Long|monotype=y}}, or {{Javadoc|module=java.base|package=java.lang|class=Double|monotype=y}}.<ref name=":0">{{Cite book|title=Big Java Early Objects|last=Horstmann|first=Cay|year=2014}}</ref>
<code>Collection</code>s are generic and hence invariant, but arrays are [[Covariance and contravariance (computer science)|covariant]]. This can be considered an advantage of generic objects such as {{code|Collection}} when compared to arrays, because under circumstances, using the generic {{code|Collection}} instead of an array prevents run time exceptions by instead throwing a compile-time exception to inform the developer to fix the code. For example, if a developer declares an {{code|Object[]}} object, and assigns the {{code|Object[]}} object to the value returned by a new {{code|Long[]}} instance with a certain capacity, no compile-time exception will be thrown. If the developer attempts to add a {{code|String}} to this {{code|Long[]}} object, the java program will throw an {{code|ArrayStoreException}}. On the other hand, if the developer instead declared a new instance of a {{code|Collection<Object>}} as {{code|ArrayList<Long>}}, the Java compiler will (correctly) throw a compile-time exception to indicate that the code is written with incompatible and incorrect type, thus preventing any potential run-time exceptions.The developer can fix the code by instantianting {{code|Collection<Object>}} as an {{code|ArrayList<Object>}} object. If the code is using Java SE7 or later versions, the developer can
<code>Collection</code>s are generic and hence [[reification (computer science)|reified]], but arrays are not reified.{{sfn|Bloch|2018|loc=Chapter §5 Item 28: Prefer lists to arrays|pp=126-129}}
Line 84:
{{Javadoc|module=java.base|package=java.util|class=Collection|member=contains(E)|text=contains(E e)|monotype=y}} method checks if a specified element exists in the <code>Collection</code>.
The <code>Collection</code> interface is a subinterface of '''{{Javadoc|module=java.base|package=java.lang|class=Iterable|monotype=y}}''', so any <code>Collection</code> may be the target of a [[Foreach loop|for-each]] statement. (The <code>Iterable</code> interface provides the {{Javadoc|module=java.base|package=java.util|class=Iterable|member=iterator()|text=iterator()|monotype=y}} method used by for-each statements.) All <code>Collection</code>s
<code>Collection</code> is generic. Any <code>Collection</code> can store any {{code|Object}}. For example, any implementation of {{code|Collection<String>}} contains {{code|String}} objects. No casting is required when using the {{code|String}} objects from an implementation of <code>Collection<String></code>.
=== Types of collection ===
Line 100:
==List interface==
Lists are implemented in the collections framework via the '''{{Javadoc|module=java.base|package=java.util|class=List|monotype=y}}'''interface. It defines a list as essentially a more flexible version of an array. Elements have a specific order, and duplicate elements are allowed. Elements can be placed in a specific position. They can also be searched for within the list.
===List implementations===
Line 110:
{{java|AbstractList}} is an example of a ''skeletal implementation'', which leverages and combines the advantages of interfaces and abstract classes by making it easy for the developer to develop their own implementation for the given interface.{{sfn|Bloch|2018|loc=Chapter §4 Item 20: Prefer interfaces to abstract classes|pp=99-103}}
=====
The '''{{Javadoc|module=java.base|package=java.util|class=ArrayList|monotype=y}}''' class implements the <code>List</code> as an array. Whenever functions specific to a <code>List</code> are required, the class moves the elements around within the array in order to do it.
Line 134:
==Queue interfaces==
The '''{{Javadoc|module=java.base|package=java.util|class=Queue|monotype=y}}''' interface defines the queue data structure, which stores elements in the order in which they are inserted. New additions go to the end of the line, and elements are removed from the front. It creates a [[FIFO (computing and electronics)|first-in first-out]] system. This interface is implemented by <code>java.util.LinkedList</code>, '''{{Javadoc|module=java.base|package=java.util|class=ArrayDeque|monotype=y}}''', and '''{{Javadoc|module=java.base|package=java.util|class=PriorityQueue|monotype=y}}'''.
===Queue implementations===
Line 144:
Note that {{java|ArrayDeque}} and {{java |
ConcurrentLinkedDeque}} both extend {{java|AbstractCollection}} but do not extend any other
{{java|AbstractQueue}} is an example of a ''skeletal implementation''.
=====PriorityQueue class=====
Line 170:
=====LinkedList class=====
<code>LinkedList</code>, of course, also implements the <code>List</code> interface and can also be used as one. But it also has the <code>Queue</code> methods. <code>LinkedList</code> implements the '''{{Javadoc|module=java.base|package=java.util|class=Deque|monotype=y}}''' interface, giving it more flexibility.<ref name="Queue Java Platform SE 7">{{cite web|url=http://docs.oracle.com/javase/7/docs/api/java/util/Queue.html |title=Queue (Java Platform SE 7) |publisher=Docs.oracle.com |date=2013-06-06 |access-date=2013-08-16}}</ref>
=====ArrayDeque class=====
<code>ArrayDeque</code> implements the <code>Queue</code> as an array. Similar to <code>LinkedList</code>, <code>ArrayDeque</code> also implements the '''{{Javadoc|module=java.base|package=java.util|class=Deque|monotype=y}}''' interface.<ref
====BlockingDeque interface====
The '''{{Javadoc:SE|module=java.base|package=java.util.concurrent|java/util/concurrent|BlockingDeque}}''' interface extends <code>java.util.concurrent.BlockingQueue</code>.{{sfn|Goetz|Peierls|Bloch|Bowbeer|2006|loc=§5.3.3 Deques and work stealing|p=92}} {{java|BlockingDeque}} is similar to {{java|BlockingQueue}}. It provides the same methods for insertion and removal with time limits for waiting for the insertion or removal to become possible. However, the interface also provides the flexibility of a <code>Deque</code>. Insertions and removals can take place at both ends. The blocking function is combined with the <code>Deque</code> function.<ref>{{cite web|url=http://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html |title=BlockingDeque (Java Platform SE 7 ) |publisher=Docs.oracle.com |date=2013-06-06 |access-date=2013-08-16}}</ref>
Line 180 ⟶ 181:
==Set interfaces==
Java's '''{{Javadoc|module=java.base|package=java.util|class=Set|monotype=y}}'''interface defines the <code>Set</code>. A <code>Set</code> can't have any duplicate elements in it. Additionally, the <code>Set</code> has no set order. As such, elements can't be found by index. <code>Set</code> is implemented by '''{{Javadoc|module=java.base|package=java.util|class=HashSet|monotype=y}}''', '''{{Javadoc|module=java.base|package=java.util|class=LinkedHashSet|monotype=y}}''', and '''{{Javadoc|module=java.base|package=java.util|class=TreeSet|monotype=y}}'''.
===Set interface implementations===
There are several implementations of the Set interface, including {{java|AbstractSet}} and its subclasses, and the final static inner class {{java|ConcurrentHashMap.KeySetView<K,V>}} (where {{java|K}} and {{java|V}} are formal type parameters).
Line 207 ⟶ 208:
=====HashSet class=====
<code>HashSet</code> uses a hash table. More specifically, it uses a '''{{Javadoc|module=java.base|package=java.util|class=LinkedHashMap|monotype=y}}''' to store the hashes and elements and to prevent duplicates.
======LinkedHashSet class======
Line 214 ⟶ 215:
=====CopyOnWriteArraySet class=====
{{code|CopyOnWriteArraySet}} is a concurrent replacement for a synchronized {{java|Set}}. It provides improved concurrency in many situations by removing the need to perform synchronization or making a copy of the object during iteration, similar to how {{code|CopyOnWriteArrayList}} acts as the concurrent replacement for a synchronized {{java|List}}.{{sfn|Goetz|Peierls|Bloch|Bowbeer|2006|loc=§5.2.3 CopyOnWriteArrayList|pp=86-89}}
On the other hand, similar to {{code|CopyOnWriteArrayList}}, {{code|CopyOnWriteArraySet}} should not be used when
===SortedSet interface===
Line 226 ⟶ 227:
=====ConcurrentSkipListSet class=====
{{code|ConcurrentSkipListSet}} acts as a concurrent replacement for implementations of a synchronized {{java|SortedSet}}. For example it replaces a {{java|TreeSet}} that has been wrapped by the {{java|
==Map interfaces==
Maps are defined by the '''{{Javadoc|module=java.base|package=java.util|class=Map|monotype=y}}''' interface in Java.
===Map interface implementations===
Line 243 ⟶ 244:
=====EnumMap=====
{{java|EnumMap}} extends {{java|AbstractMap}}. {{java|EnumMap}} has comparable speed with an ordinal-indexed array.{{sfn|Bloch|2018|loc=Chapter §6 Item 36: Use EnumMap instead of ordinal indexing|pp=171-175}} This is because {{java|EnumMap}} internally uses an array, with implementation details completely hidden from the developer.{{sfn|Bloch|2018|loc=Chapter §6 Item 36: Use EnumMap instead of ordinal indexing|pp=171-175}} Hence, the EnumMap gets the type safety of a {{java|Map}} while the performance advantages of an array.{{sfn|Bloch|2018|loc=Chapter §6 Item 36: Use EnumMap instead of ordinal indexing|pp=171-175}}
=====HashMap=====
Line 260 ⟶ 261:
=====ConcurrentSkipListMap class=====
{{code|ConcurrentSkipListMap }} acts as a concurrent replacement for implementations of a synchronized {{java|SortedMap}}. {{code|ConcurrentSkipListMap }} is very similar to {{code|ConcurrentSkipListSet}}, since {{code|ConcurrentSkipListMap }} replaces a {{java|TreeMap}} that has been wrapped by the {{java|
===Map subinterfaces===
Line 289 ⟶ 290:
==Citation==
{{Reflist|2}}
==References==
|