Collection0
Last updated
Was this helpful?
Last updated
Was this helpful?
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.
Java ArrayList class can contain duplicate elements.
Java ArrayList class maintains insertion order.
Java ArrayList class is non synchronized.
Java ArrayList allows random access because array works at the index basis.
In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
method:
1)add( Object o): This method adds an object o to the arraylist.
This statement would add a string hello in the arraylist at last position.
2)add(int index, Object o): It adds the object o to the array list at the given index.
It will add the string bye to the 2nd index (3rd position as the array list starts with index 0) of array list.
3)remove(Object o): Removes the object o from the ArrayList.
This statement will remove the string “Chaitanya” from the ArrayList.
4)remove(int index): Removes element from a given index.
It would remove the element of index 3 (4th element of the list – List starts with o).
5)set(int index, Object o): Used for updating an element. It replaces the element present at the specified index with the object o.
It would replace the 3rd element (index =2 is 3rd element) with the value Tom.
6) int indexOf(Object o): Gives the index of the object o. If the element is not found in the list then this method returns the value -1.
int lastIndexOg(Object 0): It is used to return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
This would give the index (position) of the string Tom in the list.
7)Object get(int index): It returns the object of list which is present at the specified index.
Function get would return the string stored at 3rd position (index 2) and would be assigned to the string “str”. We have stored the returned value in string variable because in our example we have defined the ArrayList is of String type. If you are having integer array list then the returned value should be stored in an integer variable.
8)int size(): It gives the size of the ArrayList – Number of elements of the list.
9)boolean contains(Object o):
It checks whether the given object o is present in the array list if its there then it returns true else it returns false.
It would return true if the string “Steve” is present in the list else we would get false.
10)clear():It is used for removing all the elements of the array list in one go. The below code will remove all the elements of ArrayList whose object is obj.
notes:
Java LinkedList class can contain duplicate elements.
Java LinkedList class maintains insertion order.
Java LinkedList class is non synchronized.
In Java LinkedList class, manipulation is fast because no shifting needs to be occurred.
Java LinkedList class can be used as list, stack or queue.
Doubly Linked List
In case of doubly linked list, we can add or remove elements from both side.
Method
Description
void add(int index, Object element)
It is used to insert the specified element at the specified position index in a list.
void addFirst(Object o)
It is used to insert the given element at the beginning of a list.
void addLast(Object o)
It is used to append the given element to the end of a list.
int size()
It is used to return the number of elements in a list
boolean add(Object o)
It is used to append the specified element to the end of a list.
boolean contains(Object o)
It is used to return true if the list contains a specified element.
boolean remove(Object o)
It is used to remove the first occurence of the specified element in a list.
Object getFirst()
It is used to return the first element in a list.
Object getLast()
It is used to return the last element in a list.
int indexOf(Object o)
It is used to return the index in a list of the first occurrence of the specified element, or -1 if the list does not contain any element.
int lastIndexOf(Object o)
It is used to return the index in a list of the last occurrence of the specified element, or -1 if the list does not contain any element.
ArrayList
LinkedList
1) ArrayList internally uses dynamic array to store the elements.
LinkedList internally uses doubly linked list to store the elements.
2) Manipulation with ArrayList is slow because it internally uses array. If any element is removed from the array, all the bits are shifted in memory.
Manipulation with LinkedList is faster than ArrayList because it uses doubly linked list so no bit shifting is required in memory.
3) ArrayList class can act as a list only because it implements List only.
LinkedList class can act as a list and queue both because it implements List and Deque interfaces.
4) ArrayList is better for storing and accessing data.
LinkedList is better for manipulating data.
notes:
HashSet stores the elements by using a mechanism called hashing.
HashSet contains unique elements only.
HashSet does not allow duplicate elements that means you can not store duplicate values in HashSet. HashMap does not allow duplicate keys however it allows to have duplicate values. HashSet permits to have a single null value. HashMap permits single null key and any number of null values.
List can contain duplicate elements whereas Set contains unique elements only.
Method
Description
void clear()
It is used to remove all of the elements from this set.
boolean contains(Object o)
It is used to return true if this set contains the specified element.
boolean add(Object o)
It is used to adds the specified element to this set if it is not already present.
boolean isEmpty()
It is used to return true if this set contains no elements.
boolean remove(Object o)
It is used to remove the specified element from this set if it is present.
Object clone()
It is used to return a shallow copy of this HashSet instance: the elements themselves are not cloned.
Iterator iterator()
It is used to return an iterator over the elements in this set.
int size()
It is used to return the number of elements in this set.
notes:
Contains unique elements only like HashSet.
Provides all optional set operations, and permits null elements.
Maintains insertion order.
notes:
Contains unique elements only like HashSet.
Access and retrieval times are quiet fast.
Maintains ascending order.
Java TreeSet implements NavigableSet interface. The NavigableSet interface extends SortedSet.
Method
Description
boolean addAll(Collection c)
It is used to add all of the elements in the specified collection to this set.
boolean contains(Object o)
It is used to return true if this set contains the specified element.
boolean isEmpty()
It is used to return true if this set contains no elements.
boolean remove(Object o)
It is used to remove the specified element from this set if it is present.
void add(Object o)
It is used to add the specified element to this set if it is not already present.
void clear()
It is used to remove all of the elements from this set.
Object clone()
It is used to return a shallow copy of this TreeSet instance.
Object first()
It is used to return the first (lowest) element currently in this sorted set.
Object last()
It is used to return the last (highest) element currently in this sorted set.
int size()
It is used to return the number of elements in this set.
Queue: FIFO(First In First Out)
Method
Description
boolean add(object)
It is used to insert the specified element into this queue and return true upon success.
boolean offer(object)
It is used to insert the specified element into this queue.
Object remove()
It is used to retrieves and removes the head of this queue.
Object poll()
It is used to retrieves and removes the head of this queue, or returns null if this queue is empty.
Object element()
It is used to retrieves, but does not remove, the head of this queue.
Object peek()
It is used to retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
It is not thread safe, so java provides PriorityBlockingQueue class that implements the BlockingQueue interface to use in java multithreading environment.
We can use theComparable
interface implemented in each element as its priority.
Or we can supply aComparator
object, which will determine the priority order of the elements.
When adding a new element to a priority queue, it is positioned in the queue based on its priority.
//to be continue
Java Deque Interface is a linear collection that supports element insertion and removal at both ends. Deque is an acronym for "double ended queue".
Method
Description
boolean add(object)
It is used to insert the specified element into this deque and return true upon success.
boolean offer(object)
It is used to insert the specified element into this deque.
Object remove()
It is used to retrieves and removes the head of this deque.
Object poll()
It is used to retrieves and removes the head of this deque, or returns null if this deque is empty.
Object element()
It is used to retrieves, but does not remove, the head of this deque.
Object peek()
It is used to retrieves, but does not remove, the head of this deque, or returns null if this deque is empty.
note: add()
always returnstrue
and throws an exception if it can't add the element, whereasoffer()
is allowed to returnfalse
if it can't add the element.
The important points about ArrayDeque class are:
Unlike Queue, we can add or remove elements from both sides.
Null elements are not allowed in the ArrayDeque.
ArrayDeque is not thread safe, in the absence of external synchronization.
ArrayDeque has no capacity restrictions.
ArrayDeque is faster than LinkedList and Stack.
S.N.
Method& Description
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
This method inserts the specified element at the end of this deque.
This method inserts the specified element at the front of this deque.
This method inserts the specified element at the end of this deque.
This method removes all of the elements from this deque.
This method returns a copy of this deque.
This method returns true if this deque contains the specified element.
This method returns an iterator over the elements in this deque in reverse sequential order.
This method retrieves, but does not remove, the head of the queue represented by this deque.
This method retrieves, but does not remove, the first element of this deque.
This method retrieves, but does not remove, the last element of this deque.
This method returns true if this deque contains no elements.
This method returns an iterator over the elements in this deque.
This method inserts the specified element at the end of this deque.
This method inserts the specified element at the front of this deque.
This method inserts the specified element at the end of this deque.
This method retrieves, but does not remove, the head of the queue represented by this deque, or returns null if this deque is empty.
This method retrieves, but does not remove, the first element of this deque, or returns null if this deque is empty.
This method retrieves, but does not remove, the last element of this deque, or returns null if this deque is empty.
This method retrieves and removes the head of the queue represented by this deque, or returns null if this deque is empty.
This method retrieves and removes the first element of this deque, or returns null if this deque is empty.
This method retrieves and removes the last element of this deque, or returns null if this deque is empty.
This method pops an element from the stack represented by this deque.
This method pushes an element onto the stack represented by this deque.
This method retrieves and removes the head of the queue represented by this deque.
This method removes a single instance of the specified element from this deque.
This method retrieves and removes the first element of this deque.
This method removes the first occurrence of the specified element in this deque.
This method retrieves and removes the last element of this deque.
This method removes the last occurrence of the specified element in this deque.
This method returns the number of elements in this deque.
This method returns an array containing all of the elements in this deque in proper sequence.