Java Collections


A Java storage framework provides an architecture to store and manipulate a group of objects. The Java archive framework includes:

  • Interfaces
  • Classes
  • Algorithm

Interfaces: Interface in Java They allow Java collections to be manipulated independently from the details of their representation. Also, they form a hierarchy in object-oriented programming languages.

Classes: Classes in Java are the implementation of the collection interface. It basically refers to the data structures that were used again and again.

Algorithm: Algorithm refers to the methods which are used to perform operations such as searching and sorting, on objects that implement collection interfaces. Algorithms are polymorphic in nature as the same method can be used to take many forms or you can say different implementations of the Java collection interface.


Interface

The Iterator is an interface that it reacts to the elements, it is used to cross the list and modify the elements. The Iterator interface has three methods which are mentioned below:

  1. public boolean hasNext() – This method 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() – This method removes the last elements returned by the iterator. 

List 

In a list, those elements are collected which can be duplicated. It is an interface that extends the archive interface. The lists are further classified into the following:

  1. ArrayList
  2. LinkedList
  3. Vectors
Array list: The implementation of the Array  list interface, where the elements can be dynamically added or removed from the list. In addition, the size of the list increases dynamically if the elements are added more than the initial size.

Syntax - 

ArrayList object = new ArrayList ();

MethodDescription
 boolean add(Collection c)

 Appends the specified element to the end of a list.

void add(int index, Object element) Inserts the specified element at the specified position.
 void clear() Removes all the elements from this list.
 int lastIndexOf(Object o)Return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.
 Object clone()

 Return a shallow copy of an ArrayList.

 Object[] toArray() Returns an array containing all the elements in the list.
 void trimToSize() Trims the capacity of this ArrayList instance to be the list’s current size.

Example - 

import java.util.*;
 class ArrayListExample{
 public static void main(String args[]){
  
 ArrayList al=new ArrayList();  // creating array list
 al.add("Jack");                // adding elements   
 al.add("Tyler");
 Iterator itr=al.iterator();
 while(itr.hasNext()){
 System.out.println(itr.next());
 }
 }
 }

Linked List: Linked List is a sequence of links in which items are included. Each link has a
connection to another link.

Syntax - 

Linkedlist object = new Linkedlist();

Java Linked List class uses two types of Linked list to store the elements:
  • Singly Linked List
  • Doubly Linked List 
Example - 

import java.util.*;
public class LinkedlistExample{
public static void main(String args[]){
LinkedList<String> al=new LinkedList<String>(); // creating linked list
al.add("Rachit");                               // adding elements
al.add("Rahul");
al.add("Rajat");
Iterator<String> itr = al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
 }
 }


Vector: Vectors are similar to array, where elements of vector objects can be accessed through the vector through an index.
Vector implements a dynamic array In addition, vector is not limited to a specific size, it can automatically shrink or increase whenever
it is needed. It's similar to ArrayList, but with two differences

Syntax - 

Vector object = new Vector(size,increment);



MethodDescription
 boolean add(Object o) Appends the specified element to the end of the list.
 void clear() Removes all of the elements from this list. 
 void add(int index, Object element) Inserts the specified element at the specified position.
 boolean remove(Object o) Removes the first occurrence of the specified element from this list.
 boolean contains(Object element) Returns true if this list contains the specified element.
 int indexOfObject (Object element) Returns the index of the first occurrence of the specified element in the list, or -1.
 int size() Returns the number of elements in this list.
 int lastIndexOf(Object o) Return the index of the last occurrence of the specified element in the list, or -1 if the list does not contain any element.


Queue

The queue in Java follows a FIFO approach. This gives elements the order for the first time in the first out method. In a queue, the first element is removed first and the last element is finally removed. Each basic method exists in two forms: an exception throws if the operation fails, the second gives a special value.


MethodDescription
 boolean add(object) Inserts the specified element into the queue and returns true if it is a success.
 boolean offer(object) Inserts the specified element into this queue.
 Object remove() Retrieves and removes the head of the queue.
 Object poll() Retrieves and removes the head of the queue, or returns null if the queue is empty.
 Object element() Retrieves, but does not remove the head of the queue.
 Object peek() Retrieves, but does not remove the head of this queue, or returns null if the queue is empty.

Example - 

import java.util.*;
class QueueExample {
public static void main(String args[]){
PriorityQueue<String> queue=new PriorityQueue<String>(); // creating priority queue
queue.add("Amit");                                       // adding elements
queue.add("Rachit");
queue.add("Rahul");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
System.out.println("iterating the queue elements:");
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
queue.remove();
queue.poll();
System.out.println("after removing two elements:");
Iterator<String> itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
}
}




Share on Google Plus

About It E Research

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment