Collections in Java
The Collection in Java is a framework that provides an architecture to store and manipulate the group of objects.
Java Collections can achieve all the operations that you perform on a data such as searching, sorting, insertion, manipulation, and deletion.
Java Collection means a single unit of objects. Java Collection framework provides many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).
What is Collection?
A Collection represents a single unit of objects, i.e., a group.
What is a framework?
A framework provides a ready-made structure of classes and interfaces for building software applications efficiently. It simplifies adding new features by offering reusable components that perform similar tasks, eliminating the need to create a framework from scratch for each new project. This approach enhances object-oriented design, making development quicker, more consistent, and reliable.
=> It provides readymade architecture.
=> It represents a set of classes and interfaces.
=> It is optional.
What is Collection framework?
The Collection Framework in Java is a set of classes and interfaces that provides a standardized way to store, manipulate, and retrieve groups of objects.
The Collection framework represents a unified architecture for storing and manipulating a group of objects. It enhances code efficiency and readability by offering various data structures, including arrays, linked lists, trees, and hash tables, tailored to different programming needs. It has:
=> Interfaces and its implementations, i.e., classes
=> Algorithm
why framework?
The Collection Framework is called a framework because it provides a well-defined structure (a set of interfaces, classes, and methods) to work with different types of collections (like lists, sets, and maps) in a standardized way.
=> Reusability
=> Quality
=> Speed
=> Maintenance
=> Reduces Effort to Design New APIs
List Interface
List interface is the child interface of Collection interface. It inhibits a list type data structure in which we can store the ordered collection of objects. It can have duplicate values.
List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.
List <data-type> list1= new ArrayList();
List <data-type> list2 = new LinkedList();
List <data-type> list3 = new Vector();
List <data-type> list4 = new Stack();
ArrayList
ArrayList is a resizable array implementation of the List interface in Java. It allows dynamic storage of elements, meaning its size grows automatically as elements are added.
The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate element of different data types. The ArrayList class maintains the insertion order and is non-synchronized. The elements stored in the ArrayList class can be randomly accessed.
LinkedList
LinkedList is a part of the Collection Framework and implements the List and Deque interfaces. It is a doubly linked list that allows efficient insertions and deletions.
LinkedList implements the Collection interface. It uses a doubly linked list internally to store the elements. It can store the duplicate elements. It maintains the insertion order and is not synchronized. In LinkedList, the manipulation is fast because no shifting is required.
package collections;
import java.util.ArrayList;
import java.util.Iterator;
public class coll_arr_list {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(10);
al.add(12);
al.add(121);
al.add(20);
// System.out.println(al);
ArrayList al2 = new ArrayList();
al2.add(11);
al2.add(15);
al2.add(71);
al2.add(80);
al.addAll(al2);// add two arraylist
// al.addAll(2,al2);//add after second index
// System.out.println(al);
// System.out.println(al2);
// System.out.println(al);
// al.remove(2);//remove
// System.out.println(al);
// System.out.println(al.subList(2, 4));
// System.out.println(al2.contains(11));//true or false
// System.out.println(al.get(3));//to get index based value
// for(Object ob:al)
// {
// System.out.println(ob);
// }
Iterator it = al.iterator();
while (it.hasNext()) {
Object ob = it.next();
Integer no = (Integer) ob;// converting to integer
if (no % 20 == 0)
System.out.println(no);
}
}
}
package collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
public class String_name_Arraylist {
public static void main(String[] args) {
String name = "neelakanadan";
LinkedHashSet hs = new LinkedHashSet();//Linked--order//hashset--no duplicate
for (int i = 0; i < name.length(); i++) {
boolean add = hs.add(name.charAt(i));
if (add == false)// only duplicate
System.out.println(name.charAt(i));
}
System.out.println(hs);
}
}
Output:
with duplicate
e
a
n
a
a
n
without duplicate
[n, e, l, a, k, d]
Top comments (0)