cs notebook0
  • Introduction
  • Core Java
  • some notes
  • Data structure&algorithm
  • Garbage Collection
  • HashMap
  • Collection0
  • Collection1
  • Collection2
  • comparatorVScomparable
  • exception0
  • exception1
  • exception2
  • Enum in Java
  • JVM
  • Wrapper Classes in Java
  • String, int convert
  • HashSetVSTreeSetVSLinkedHashSet
  • Pair
Powered by GitBook
On this page
  • 1 Methods of Collection interface
  • 2.ArrayList
  • 3. LinkedList
  • 4. ArrayList and LinkedList
  • 5. HashSet(hashsetVShashmap)
  • 6. LinkedHashSet
  • 7. TreeSet
  • 8 Java Queue Interface
  • 9. Java PriorityQueue
  • 10.Java Deque Interface
  • Methods of Java Deque Interface

Was this helpful?

Collection0

PreviousHashMapNextCollection1

Last updated 6 years ago

Was this helpful?

1 Methods of Collection interface

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.

2.ArrayList

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.

obj.add("hello");

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.

obj.add(2,"bye");

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.

obj.remove("Chaitanya");

This statement will remove the string “Chaitanya” from the ArrayList.

4)remove(int index): Removes element from a given index.

obj.remove(3);

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.

obj.set(2,"Tom");

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.

int pos = obj.indexOf("Tom");

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.

String str= obj.get(2);

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.

int numberofitems = obj.size();

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.

obj.contains("Steve");

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.

obj.clear();

3. LinkedList

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.

4. ArrayList and LinkedList

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.

5. HashSet(hashsetVShashmap)

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.

6. LinkedHashSet

notes:

  • Contains unique elements only like HashSet.

  • Provides all optional set operations, and permits null elements.

  • Maintains insertion order.

7. TreeSet

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.

8 Java Queue Interface

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.

9. Java PriorityQueue

It is not thread safe, so java provides PriorityBlockingQueue class that implements the BlockingQueue interface to use in java multithreading environment.

We can use theComparableinterface implemented in each element as its priority.

Or we can supply aComparatorobject, 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.

9.1 Comparable interface

//to be continue

10.Java Deque Interface

Java Deque Interface is a linear collection that supports element insertion and removal at both ends. Deque is an acronym for "double ended queue".

Methods of Java Deque Interface

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 returnstrueand throws an exception if it can't add the element, whereasoffer()is allowed to returnfalseif it can't add the element.

10.1 ArrayDeque Class

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.

boolean add(E e)
void addFirst(E e)
void addLast(E e)
void clear()
ArrayDeque<E> clone()
boolean contains(Object o)
Iterator<E> descendingIterator()
E element()
E getFirst()
E getLast()
boolean isEmpty()
Iterator<E> iterator()
boolean offer(E e)
boolean offerFirst(E e)
boolean offerLast(E e)
E peek()
E peekFirst()
E peekLast()
E poll()
E pollFirst()
E pollLast()
E pop()
void push(E e)
E remove()
boolean remove(Object o)
E removeFirst()
boolean removeFirstOccurrence(Object o)
E removeLast()
boolean removeLastOccurrence(Object o)
int size()
object[] toArray()