Name the collection interface implemented by the hashtable class

The collections in java provide an architecture to store and manipulate the group of objects, interfaces and classes. A collection is a group of objects or it is a single entity that represents multiple objects.

Java collection framework consists of classes and interfaces by using these classes and interface developers can represent a group of objects in a single entity. Collection framework is present in package java. util. 

What is collections in Java?

The Collections in Java provides an architecture to store and manipulate the group of objects, interfaces and classes. This java collection is a framework. This framework has several useful functions that have tons of useful functions, making a programmer task super easy.

This framework provides many interfaces [Queue, Set, List, Deque] and classes [ PriorityQueue, HashSet, ArrayList, Vector, LinkedList, LinkedHashSet].

Framework in java

Java frameworks are the prewritten code used by developers to create applications in the java language. 

What is the Collection framework?

The Collection framework is a unified architecture for storing and manipulating a group of objects.

The collection framework was designed to meet several goals, such as −

  • The framework had to be high-performance and adapt a collection easy method.
  • The implementations for the fundamental collections were to be highly efficient.
  • The framework had to allow different types of collections to work in a similar manner.
  • The framework had to extend and/or adapt a collection easily.

Collection Framework Hierarchy

Let us see the hierarchy of the collection framework:

Hierarchy of Collection Framework

What is a need for the Collection Framework?

Suppose, A variable is created to store data and a 10 value is assigned [Example, int a =10]. Now the programmer wants to store another data of the same datatype. So, the programmer needs to create another variable and assign a new value [Example, int b= 20]. 

If the programmer wants to store 100 values then the disadvantage of this is the programmer has to create multiple variables with a unique name and it is very time-consuming also.

In this case array concept is introduced. Programmer declare an array with specific size and store elements.

For example,

int  arr[] = new int[100]; // 100 is size of array 
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
.
.
.
arr[100] = 90;

This is the way of store multiple values of the same datatype.

But there are certain limitations 

  1. Array
    Array stores the values of the same datatype i.e., Array is homogeneous but it can overcome by creating an array of object classes but this is not a good option.
Public class MultipleValues
{
Public static void main[ string[] args]
{
objects a[]- new objects [5];
a[0]=10;
a[1]=10.45;
a[2]='A';
a[3]="name";
a[4]= true;
For[ int i=0;iname =new ArrayList[];
name.add["Pinku'];
name.add["seeta"];
name.add["geeta"];
name.add["sara"];
name.add["ved'];
System.out.println[name];
}
}

2. This is way is to store values of different datatype

import java.util.*;
public class ListArraylist
{
public static void main[String[]args]
{
ArrayList name= new ArrayList[];
name.add[10];
name.add["name"];
name.add[30.66];
name.add[true];
name.add['A'];
System.out.println[name];
}
}

Methods in ArrayList:

Sr.noMethodDescription1get[object o]It prints the value at a specific index.2set[index, object o]It updates the value. In that, we need to provide an index.3add[index, object o]It adds an element at a specific index.4remove[Object o]It removes elements at specific indexes.5sort[]It sorts an array depending upon the data type.6addAll[Collection c]It is used to add another collection.7removeAll[Collection c]It is used to remove another collection.

The common methods in the elements are shown below.

toArray[] method

import java.util.*;
public class Main
{
public static void main[String[] args] {
ArrayList values=new ArrayList[];
values.add[10];
values.add[20];
values.add[30];
values.add[40];
values.add[50];
Object arr[] = values.toArray[];
System.out.println["After convert into an array"];
for[int i=0;i

Chủ Đề