Java Collections & Framework

Java is a programming language and a platform. It is a robust, object-oriented, and secure programming language and was developed by Sun Microsystem in 1995.

What Is Java?

Java is a general-purpose, concurrent, secured, object-oriented, and class-based programming language.

Java is a programming language and a platform. It is a robust, object-oriented, and secure programming language and was developed by Sun Microsystem in 1995. Java is credited to James Gosling as its creator. Earlier its name was Oak. James Gosling and his team changed the name from Oak to Java because Oak was already a recognized business.

Platform – The hardware or software environment in which a program runs, is known as a platform. Java has a runtime environment (JRE) and API, and so it is called a platform.

Application 

According to Sun, 3 billion-plus devices run Java. There are devices where Java is currently used. Some of them are as follows:

  • Desktop Applications such as acrobat reader, media player, antivirus, etc
  • Web applications such as irctc.co.in, javapoint.com, etc
  • Enterprise Applications such as banking applications
  • Embedded System
  • Smart Card
  • Mobile
  • Games
  • Robotics etc

Types of Java Applications

There are four types of applications that can be created using Java programming:

1) Standalone Application

It is also known as a window-based application. These are traditional software that users need to install on every machine. Examples are antivirus, media players, etc. For constructing standalone applications, java uses AWT and Swing.

2) Web Application

An application that creates a dynamic page and runs on the server side is called a web application, Servlet, JSP, Spring, JSF etc. Java web apps are made using several technologies.

3) Enterprise Application

An enterprise application is a distributed application, such as a banking application or another type. High-level security, load balancing, and clustering are some of its benefits. EJB is a Java framework for building enterprise applications.

4) Mobile Applications

Applications which are created for the mobile devices is called a mobile application. Currently, mobile applications are made with Android and Java ME.

What Is Collection in Java?

A group of objects, or a single object, is represented by a collection.

What Is Framework in Java?

  • Java framework provides ready-made architecture.
  • It displays a collection of classes and interfaces.
  • It is optional.

What Is a Collection Framework?

The Collection framework represents an undivided architecture for storing and manipulating a group of objects.

  1. Interfaces and their implementations, i.e., classes
  2. Algorithm

Image of Collection framework in Java

Collection Interface

It is the interface that all classes in the collection framework implement. It indicates the techniques each collection will use. The collection framework is built on a foundation is built on a foundation that is created by the Collection interface.

Some collection interface methods include void clear(), Boolean add (Object obj), and Boolean addAll (Collection c), etc. which are implemented by all the subclasses of the Collection interface.

Methods of Collection Interface

There are so many methods declared in the Collection interface. Given below:-

image of java Method & description

Iterator Interface

The Iterable interface is an interface in Java that provides the functionality to access all the collection classes one by one. The iterator over the elements of type T is returned. It just contains one abstract method i.e. It takes the place of enumeration in the Java collection framework and allows the user to access elements of the collection.

  • Iterator<T> iterator()

Methods of Iterator Interface

There are 3 methods in the Iterator interface. Given below:-

image of Methods & description of Iterator interface

List Interface

List interface is the child interface of the collection interface. It prevents us from storing an ordered collection of objects in a data structure of the list type. It can have duplicate values.

ArrayList, LinkedList, Vector, and Stack are classes that implement the List interface.

The List Interface must be instantiated using:

  • List <data-type> list1= new ArrayList();
  • List <data-type> list2= new LinkedList();
  • List <data-type> list3= new Vector();
  • List <data-type> list4= new Stack();

It is possible to add, remove, and access list elements using a variety of methods in the List interface.

ArrayList

ArrayList class implements the List interface. To hold the identical element of several data kinds, it employs a dynamic array. The non-synchronized ArrayList class retains the insertion order. The elements stored in the ArrayList

Image of Arraylist Class

Output:

Out put of arraylist

 

Linked List

It implements the collection interface and stores the elements in a double-linked list that is internally linked and has room for duplicate elements. It maintains the insertion order and it is not synchronized. In the Linked list, the manipulation is fast because shifting is not required.

image of Linked list

Output:

Output of linked list

Vector

It uses a dynamic array to store the data elements and is quite similar to an Array list. It is synchronized and contains many methods that are not the part of collection framework.

Image of Vector

Output:

image of vector output

Stack

It is the subclass of Vector. It implements the last in and first out data structure and contains all the methods of the Vector class and it provides its methods like Boolean peek(), and Boolean push(object o) which define its properties.

image of Stack

Output:

Output of Stack

Queue Interface

It maintains the first in and first out principle (FIFO). To hold the items that are about to be processed, a list that is ordered is employed. The Queue interface is implemented by a number of classes, including PriorityQueue, Deque, and ArrayDeque.

Queue interface can be instantiated as:

  • Queue<String> q1 = new PriorityQueue();
  • Queue<String> q2 = new ArrayDeque();

Priority Queue

The Priority Queue class implements the Queue interface, it holds the elements or objects which are to be processed by their priorities. It does not allow the null values to be stored in the queue.

Image of priority queue

Output:

Image of priority queue Output

Deque Interface

It extends the Queue interface and the user can add and remove the elements from both sides. Deque stands for a double-ended queue which enables the user to perform the operations at both ends. It can be instantiated as:

  • Deque d = new ArrayDeque();

ArrayDeque

This class implements the Deque interface. Contrary to queues, it makes it easier for us to use Deques, which allow users to remove or add pieces from either end. ArrayDeque is faster than ArrayList and Stack does not have capacity restrictions.

Arraydequeue image

Output:

output image of Arrayqueue

Set Interface

In Java Set Interface is present in java.util package. It extends the collection interface. We cannot keep duplicate things since they represent an unordered set of elements. One null value at most can be kept in Set. Set is implemented by HashSet, TreeSet, and LinkedHashSet.

It can be instantiated as:

  • Set<data-type> s1 = new HashSet<data-type>();
  • Set<data-type> s2 = new TreeSet<data-type>();
  • Set<date-type> s3 = new LinkedHashSet<data-type>();

HashSet

This class implements the Set interface. It represents the database’s hash table storage collection. Hashing is used to store the elements in the HashSet and it contains unique items.

Image of HashSet Interface

Output:

Output of hashset image

LinkedHashSet

This class represents the LinkedList implementation of the Set Interface. It extends the HashSet class and implements the Set interface and it also contains unique elements like HashSet maintains the insertion order and allows null elements.

Image of LinkedHashset

Output:

image of output of Linkedhashset

TreeSet

This class implements the Set interface that uses a tree for storage and also contains unique elements like HashSet. The access and retrieval time of TreeSet is quite fast. The elements in TreeSet are stored in increasing order.

Image of TreeSet

Output:

output image of Treeset

SortedSet Interface

This is the alternative to the Set interface that provides a total ordering of its elements. It is present in Java.util package and extends the Set interface present in the collection framework the elements of the SortedSet are arranged in a sorted manner and it provides additional methods that restrain the ordering of the elements. It instantiated as:

  • SortedSet<data-type> set = new TreeSet()

 

Orginal Source Link:- https://www.devstringx.com/java-collections-framework


Comments