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. Show
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 javaJava 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 −
Collection Framework HierarchyLet us see the hierarchy of the collection framework: Hierarchy of Collection FrameworkWhat 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,
This is the way of store multiple values of the same datatype. But there are certain limitations
In the above example array is created with a size of five which means the array store only five data values. If the size of the array is five and the user store only four values then memory is wasted. To overcome this limitation, the Collection Framework was used. In the collection framework, there are classes and interfaces are defined which are List, Queue, Set, etc. Sr.noArrayCollection Framework1Fixed-size (not growable)Growable in nature2If the size is 10 and only 5 elements store then it is a waste of memory.It adjusts size according to elements.3Arrays can hold only homogeneous data elements.Collection can hold homogeneous as well as heterogeneous data elements.4Memory management is poor.Memory management is effective.Also Read: Strings in Java Difference between collection and collectionsThe collection in java is the root interface of the collection framework and provide several classes and interfaces to represent a group of individual objects as a single unit. List, Set, and Queue are the main child interfaces of the collection interface. The Map interface is also part of the java collection framework but it does not inherit the collection interface. The map interface is preferred when values are stored in the form of keys and value pairs. Map Interface implemented using following classes:-
Methods present in the collection interfaceSr.noMethodDescription1add(Object o)To insert an element in the collection.2addAll(Collection c)To insert another collection in the present collection.3remove(Object o)To remove an element in the collection.4removeAll(Collection c)To remove another collection from the present collection if another is inserted.5retain(collection c)To remove all the collection elements that are not contained in the specified collection.6clear()It removes all the elements from the collection.7isEmpty()It checks collection is empty or not and provides true or false.8size()It gives the total number of elements present in the collection in form of a numeric value.9equals(collection c)It is used to check if the two collections are the same or not.10toArray(collection c)It converts collection into an array.11contains(Object o)It is used for searching. If an element is present in the collection it returns true or false.12contains(collection c)It is used for searching. If elements of another collection are present in the collection or not. If present returns true or false.List Interface
ArrayList
How to create ArrayListArrayList For example,
2. This is way is to store values of different datatype
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
Ways to reading elements from any list
LinkedList
Consider LinkedList contains 3 elements, LinkedList element is not stored at the consecutive address they stored at any address but they internally connected using the address of previous and next element address. PA :-Previous Element address NA:- Next Element Address index:0,1,2,…. How to create a LinkedListLinkedList For example,
Methods in LinkedList: Some methods in LinkedList are the same as ArrayList. Refer program no. 4, 5, 6, 7. change is to replace ArrayList with LinkedList. Other methods in LinkedList are:
0Vector
How to create a list using vectorVector < DataType> VariableName = new Vector
1Methods in vector: Some methods in Vector is same as Arraylist. Refer program no.4, 5, 6, 7 . change is replace ArrayList to Vector. Another methods are: addElement() firstElement() lastElement() 2Stack
3Set Interface
Set Interface is implemented by using LinkedHashSet and HashSet class. Hashset
There are three different ways to create HashSet:
Here, HashSet default capacity to store elements is 16 with a default load factor/fill ratio of 0.75. Load factor is if HashSet stores 75% element then it creates a new HashSet with increased capacity. 2. Hashset hs = new Hashset ( 100); Here 100 is an initial capacity and the default load factor is 0.75. 3. Hashset hs = new Hashset ( 100,(foot) 0.90); Here capacity is 100 with a load factor of 0.90. The load factor may be decided by the user but it should be >=0.75. 4. HashSet 4Method in HashSet Some methods are common in HashSet and Arraylist refer to program no. 4, 5, 6, 7. In HashSet get() and set() method not present because forget and set method index is required and in HashSet elements stores at a random address Problem Statement:- Write a program to remove duplicate elements. 5LinkedHashSet
for example 6
TreeSet
7Queue Interface
ArrayDequeue is faster than LinkedList. Methods in Queue : add() :- It used to insert data into queue. If data is not inserted successfully it throws an exception. offer():- It is used to insert data into the queue. If data is not inserted successfully it returns false. element():-It returns head elements from the queue. If Queue is empty it will throw exception NoSuchElementException. peek():- It returns head elements from the queue. . If Queue is empty it will return Null. remove():- It removes an element from the queue. If Queue is empty it will throw exception NoSuchElementException. poll():- It removes the element from the removing. If Queue is empty it will return Null. 8Map Interface
Key must be unique while duplicates values are allowed.
HashMap
HashMap < dataType, dataType> m = new HashMap < dataType,dataType>(); For example, 9 0LinkedHashMap
Hashtable
How to create HashMap There are three ways: Hashtable t = new Hashtable();
Here Hashtable is created with some capacity and the load factor is decided by the user. It should be >=0.75. Note:- Methods in Hashtable are the same as Hash Map. Advantages of collections framework
Difference between Iterator and ListIteratorFeaturesListIteratorIteratorTraversal DirectionBoth, forward and backwardForwardModifyCan modify or replace elementsCannot modify or replace elementsObjects traversalList onlyMap, Set and ListAdd and Set operationsAllows both operationsNot possibleIterator’s current positionCan be determinedNot possible.Retrieve IndexYesNot possibleDifference between Comparable and ComparatorComparableComparatorComparable provides a single sorting sequence.The Comparator provides multiple sorting sequences.Comparable affects the original class.Comparator doesn’t affect the original class.Comparable provides compareTo() method to sort elements.Comparator provides compare() method to sort elements.Comparable is present in java.lang package.A Comparator is present in java. util package.Comparable interface compares “this” reference with the object specified.Comparator in Java compares two different class objects provided.We hope this blog on collection in Java was helpful! To learn more, you can take up a Java online course free with certificate. What is the name of Collection interface?The Collection interface is a member of the Java Collections Framework. It is a part of java. util package. It is one of the root interfaces of the Collection Hierarchy.
Which class in the collection framework has its implementation based on hash table?HashSet: Java HashSet class creates a collection that use a hash table for storage. Hashset only contain unique elements and it inherits the AbstractSet class and implements Set interface.
What are different types of Collection interfaces?The core collection interfaces are:. Collection : The root of the collection hierarchy. ... . Set : A collection that cannot contain duplicate elements. ... . List : An ordered collection (sometimes called a sequence). ... . Queue : A collection used to hold multiple elements prior to processing. ... . Map : An object that maps keys to values.. What is the top interface implemented by every Collection classes?Interfaces
util. Collection is the root interface of Collections Framework. It is on the top of the Collections framework hierarchy. It contains some important methods such as size(), iterator(), add(), remove(), clear() that every Collection class must implement.
|