What is the name of the collection interface used to represent elements in a sequence?
Table of contents Show
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 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.
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 interface
List Interface
ArrayList
How to create ArrayListArrayList For example, 1. This is way is to store values of the same datatype
2. This is way is to store values of different datatype
Methods in ArrayList:
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:
Vector
How to create a list using vectorVector < DataType> VariableName = new Vector
Methods 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()
Stack
Set 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
Method 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.
LinkedHashSet
for example
TreeSet
Queue 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.
Map Interface
Key must be unique while duplicates values are allowed.
HashMap
HashMap < dataType, dataType> m = new HashMap < dataType,dataType>(); For example,
LinkedHashMap
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 ListIterator
Difference between Comparable and Comparator
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 the 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 of these interfaces handle sequences?Explanation: Collection interfaces defines core methods that all the collections like set, map, arrays etc will have. 2. Which of these interface handle sequences? Explanation: None.
Which of the following is an interface in the collection framework?This framework provides many interfaces (Queue, Set, List, Deque) and classes ( PriorityQueue, HashSet, ArrayList, Vector, LinkedList, LinkedHashSet).
What are the interfaces in the collections API?Collection Interfaces. util. Set.. util. SortedSet.. util. NavigableSet.. util. Queue.. util. concurrent. BlockingQueue.. util. concurrent. TransferQueue.. util. Deque.. util. concurrent. BlockingDeque.. |