In Java, collections are also known as containers. It is an object that forms a natural group of multiple elements in a single unit. Collections are used to aggregate multiple data items and access them efficiently.
Collection frameworks contain interfaces to access abstract data types, implementations of the interfaces to make reusable data structures, and algorithms (methods) that can be used on different implementations of data structures.
The Collection framework in Java is a set of classes and interfaces that provides a standardized way to manage groups of objects. It provides a set of high-level interfaces, implementations, and algorithms that allow you to manipulate collections of objects in a consistent and efficient way. The Collection framework includes several interfaces, such as List, Set, Queue, Map, and their respective implementations.
Using the Collection framework, you can easily manipulate groups of objects, sort them, search them, filter them, and perform various other operations on them. The framework also provides algorithms for sorting and searching elements in a collection. The Collection framework is a key component of Java and is widely used in Java programming.
The framework is designed to be extensible, allowing developers to create their own collection classes that can be used with existing algorithms and interfaces. Additionally, the framework provides support for concurrency, allowing multiple threads to access and modify a collection at the same time. Overall, the Collection framework provides a powerful and flexible toolset for managing collections of objects in Java.
Benefits
- Reduction of programming effort by
useful algorithms and data structures
- High-quality and high-performance
implementations of data structures
- Interoperability among different APIs
The Collection
interface is the root of the collection hierarchy. Direct implementation of this interface is not provided by the Java platform, but the direct implementation of the sub-interfaces are provided.
The main difference between set
and List
interfaces are: a set
can not contain duplicate elements but a List
can.
Both Queue
and Deque
are used to store multiple elements prior to processing.
Whereas a Map
contains values with respect to keys.
Different types of implementations
In Java platform there are three different types of implementations of Set
interface: HashSet
, TreeSet
, and LinkedHashSet
.
HashSet
is implemented based on the concept of Hash Table
, whereas TreeSet
is implemented based on the concept of Red-Black Tree
. LinkedHashSet
is implemented based on the concept of Hash Table with Linked List
.
import java.util.*;
public class FindDuplicates {
public static void main(String[] args) {
Set<String> set = new HashSet<String>();
for (String a : args)
set.add(a);
System.out.println("Number of distinct words: " + set.size());
System.out.println("The words are : ");
for (String a : set)
System.out.println(" " + a);
}
}
import java.util.*;
public class FindDuplicates {
public static void main(String[] args) {
Set<String> set = new TreeSet<String>();
for (String a : args)
set.add(a);
System.out.println("Number of distinct words: " + set.size());
System.out.println("The words are : ");
for (String a : set)
System.out.println(" " + a);
}
}
import java.util.*;
public class FindDuplicates {
public static void main(String[] args) {
Set<String> set = new LinkedHashSet<String>();
for (String a : args)
set.add(a);
System.out.println("Number of distinct words: " + set.size());
System.out.println("The words are : ");
for (String a : set)
System.out.println(" " + a);
}
}
There are two types of implentations of List
interface in Java. Those are ArrayList
and LinkedList
.
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
public class ShuffleElements {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
for (String a : args)
list.add(a);
Collections.shuffle(list, new Random());
System.out.println(list);
}
}
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;
public class ShuffleElements {
public static void main(String[] args) {
List<String> list = new LinkedList<String>();
for (String a : args)
list.add(a);
Collections.shuffle(list, new Random());
System.out.println(list);
}
}
According to the basic concept of data structure, Queue
stores the elements in order of their arrival (according to FIFO).
import java.util.LinkedList;
import java.util.Queue;
public class MyQueue {
public static void main(String[] args) throws InterruptedException {
int n = Integer.parseInt(args[0]);
Queue<Integer> queue = new LinkedList<Integer>();
for (int i = n; i >= 0; i--)
queue.add(i);
while (!queue.isEmpty()) {
System.out.println(queue.remove());
}
}
}
Deque
is double-ended-queue, which is nothing but a linear collection of elements. Now, try to write a simple program using Deque
.
The Java platform contains three Map implementations: HashMap
, TreeMap
, and LinkedHashMap
.
import java.util.HashMap;
import java.util.Map;
public class MyMap {
public static void main(String[] args) {
Map<String, Integer> map = new HashMap<String, Integer>();
for (String a : args) {
Integer frequency = map.get(a);
map.put(a, (frequency == null) ? 1 : frequency + 1);
}
System.out.println("Number of distinct words: " + map.size());
System.out.println(map);
}
}
import java.util.Map;
import java.util.TreeMap;
public class MyMap {
public static void main(String[] args) {
Map<String, Integer> map = new TreeMap<String, Integer>();
for (String a : args) {
Integer frequency = map.get(a);
map.put(a, (frequency == null) ? 1 : frequency + 1);
}
System.out.println("Number of distinct words: " + map.size());
System.out.println(map);
}
}
import java.util.LinkedHashMap;
import java.util.Map;
public class MyMap {
public static void main(String[] args) {
Map<String, Integer> map = new LinkedHashMap<String, Integer>();
for (String a : args) {
Integer frequency = map.get(a);
map.put(a, (frequency == null) ? 1 : frequency + 1);
}
System.out.println("Number of distinct words: " + map.size());
System.out.println(map);
}
}
The Java Collection framework is an architecture to store and manipulate a group of similar objects. It provides required operations like searching, sorting, insertion, deletion, updation etc.
Java Collection framework provides many interfaces such as Set, List, Queue, Deque, etc.
It has many classe implementations such as ArrayList, Vector, LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet, etc.
The relationship among classes and interfaces are shown in the following figure.
Happy Exploring!
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.