Difference between revisions of "Collections"
Line 1: | Line 1: | ||
+ | Collections in java is a framework that provides an architecture to store and manipulate the group of objects. | ||
+ | |||
+ | All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections. | ||
+ | |||
+ | Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces ([[Set]], [[List]], [[Queue]], [[Deque]] etc.) and classes ([[ArrayList]], [[Vector]], [[LinkedList]], [[PriorityQueue]], [[HashSet]], [[LinkedHashSet]], [[TreeSet]] etc). | ||
+ | |||
https://www.javatpoint.com/images/collection-hierarchy.png | https://www.javatpoint.com/images/collection-hierarchy.png | ||
+ | |||
+ | ==Methods of Collection interface== | ||
+ | There are many methods declared in the Collection interface. They are as follows: | ||
+ | |||
+ | ==Iterator interface== | ||
+ | Iterator interface provides the facility of iterating the elements in forward direction only. | ||
+ | |||
+ | ===Methods of Iterator interface=== | ||
+ | There are only three methods in the Iterator interface. They are: | ||
+ | |||
+ | <table class="alt"> | ||
+ | <tbody><tr><th>No.</th><th>Method</th><th>Description</th></tr> | ||
+ | <tr><td>1</td><td>public boolean hasNext()</td><td>It returns true if iterator has more elements.</td></tr> | ||
+ | <tr><td>2</td><td>public Object next()</td><td>It returns the element and moves the cursor pointer to the next element.</td></tr> | ||
+ | <tr><td>3</td><td>public void remove()</td><td>It removes the last elements returned by the iterator. It is rarely used.</td></tr> | ||
+ | </tbody></table> | ||
+ | |||
+ | |||
+ | <table> | ||
+ | <tbody><tr><th>No.</th><th>Method</th><th>Description</th></tr> | ||
+ | <tr><td>1</td><td>public boolean add(Object element)</td><td> is used to insert an element in this collection.</td></tr> | ||
+ | <tr><td>2</td><td>public boolean addAll(Collection c)</td><td>is used to insert the specified collection elements in the invoking collection.</td></tr> | ||
+ | <tr><td>3</td><td>public boolean remove(Object element)</td><td>is used to delete an element from this collection.</td></tr> | ||
+ | <tr><td>4</td><td>public boolean removeAll(Collection c)</td><td>is used to delete all the elements of specified collection from the invoking collection.</td></tr> | ||
+ | <tr><td>5</td><td>public boolean retainAll(Collection c)</td><td>is used to delete all the elements of invoking collection except the specified collection.</td></tr> | ||
+ | <tr><td>6</td><td>public int size()</td><td>return the total number of elements in the collection.</td></tr> | ||
+ | <tr><td>7</td><td>public void clear()</td><td>removes the total no of element from the collection.</td></tr> | ||
+ | <tr><td>8</td><td>public boolean contains(Object element)</td><td>is used to search an element.</td></tr> | ||
+ | <tr><td>9</td><td>public boolean containsAll(Collection c)</td><td>is used to search the specified collection in this collection.</td></tr> | ||
+ | <tr><td>10</td><td>public Iterator iterator()</td><td>returns an iterator.</td></tr> | ||
+ | <tr><td>11</td><td>public Object[] toArray()</td><td>converts collection into array.</td></tr> | ||
+ | <tr><td>12</td><td>public boolean isEmpty()</td><td>checks if collection is empty.</td></tr> | ||
+ | <tr><td>13</td><td>public boolean equals(Object element)</td><td>matches two collection.</td></tr> | ||
+ | <tr><td>14</td><td>public int hashCode()</td><td>returns the hashcode number for collection.</td></tr> | ||
+ | </tbody></table> | ||
==[[Set]]== | ==[[Set]]== | ||
Line 11: | Line 52: | ||
==[[Map]]== | ==[[Map]]== | ||
==[[Generics]]== | ==[[Generics]]== | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
==[[Collection Interview Questions and Answers]]== | ==[[Collection Interview Questions and Answers]]== |
Revision as of 05:57, 1 August 2018
Collections in java is a framework that provides an architecture to store and manipulate the group of objects.
All the operations that you perform on a data such as searching, sorting, insertion, manipulation, deletion etc. can be performed by Java Collections.
Java Collection simply means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque etc.) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet etc).
Contents
Methods of Collection interface
There are many methods declared in the Collection interface. They are as follows:
Iterator interface
Iterator interface provides the facility of iterating the elements in forward direction only.
Methods of Iterator interface
There are only three methods in the Iterator interface. They are:
<tbody> </tbody>No. | Method | Description |
---|---|---|
1 | public boolean hasNext() | It returns true if iterator has more elements. |
2 | public Object next() | It returns the element and moves the cursor pointer to the next element. |
3 | public void remove() | It removes the last elements returned by the iterator. It is rarely used. |
No. | Method | Description |
---|---|---|
1 | public boolean add(Object element) | is used to insert an element in this collection. |
2 | public boolean addAll(Collection c) | is used to insert the specified collection elements in the invoking collection. |
3 | public boolean remove(Object element) | is used to delete an element from this collection. |
4 | public boolean removeAll(Collection c) | is used to delete all the elements of specified collection from the invoking collection. |
5 | public boolean retainAll(Collection c) | is used to delete all the elements of invoking collection except the specified collection. |
6 | public int size() | return the total number of elements in the collection. |
7 | public void clear() | removes the total no of element from the collection. |
8 | public boolean contains(Object element) | is used to search an element. |
9 | public boolean containsAll(Collection c) | is used to search the specified collection in this collection. |
10 | public Iterator iterator() | returns an iterator. |
11 | public Object[] toArray() | converts collection into array. |
12 | public boolean isEmpty() | checks if collection is empty. |
13 | public boolean equals(Object element) | matches two collection. |
14 | public int hashCode() | returns the hashcode number for collection. |