Collections: In a simplest Java application, there will be more that object of same or different type. At a point of time you may have to hold group of objects somewhere. For example, A list of employees (A list of objects of Employee class), or a list of students etc. Java creator knew that very well. So, the created a set of classes and interfaces that helps you to deal with multiple objects at one place. This set of interfaces or classes is called Java Collection Framework. It can be understood into two separate parts:
Collections, and another is Map. Map is a different type of collection, we will cover it later.
Collection Framework: In figure 1.0, complete classes/interfaces hierarchy is shown. You can see, there is an interface Iterable at the top. At many places you will find that, Collection interface is the top most interface in hierarchy but that’s wrong. You can verify it in you JDK code base.Figure 1.0: Collections Hierarchy
Yellow nodes are interfaces
Blues are Abstract classes
Pinks are Concrete Implementation Classes
Collection: Collection interface, provides a very basic and common functionality contract. It has lots of APIs declared. Some very important APIs a listed here.
size(): This method tells, how many elements are present in the collection, return int (number of elements).
isEmpty(): Checks if collection has element or not (return Boolean).
contains(Object):Checks if specified collection is present in the collection or not (return Boolean).
iterator(): Returns Iterator’s object to traverse the collection
add(E): Add element to the collection
remove(Object): Remove element from the collection
containsAll(Collection):Check if all the element of a collection exists in the collection.
addAll(Collection):Add all elements of a collection to the collection
removeAll(Collection): Remove all elements of a collection from the collection
retainAll(Collection): Retain all element of a collection and remove others from the collection
clear(): Remove all elements from the collection
These are not the all but important APIs available in Collection interface.
Java Collection Framework provides, four basic type of collections
1. List
2. Queue
3. Set
4. Map (Map is not a part of above hierarchy we will discuss this separately)
List: List is a very simple and straightforward collection. It just makes a list of objects you add and preserve the order. Preserving order means, you can get the object in same order in which they inserted or added into the list.
Features:
· Preserve the insertion order
· Allow duplicate objects
· Automatically grows
· Allows null values
· Allow indexed access (ArrayList)
ArrayList and LinkedList are very common implementations that are generally used.
ArrayList: - An implementation that stores elements in a backing array. The array’s size will be automatically expanded if there isn’t enough room when adding new elements into the list. It’s possible to set the default size by specifying an initial capacity when creating a new ArrayList. Basically, an ArrayList offers constant time for the following operations: size, isEmpty, get, set, iterator, and listIterator; amortized constant time for the add operation; and linear time for other operations. Therefore, this implementation can be considered if we want fast, random access of the elements.
LinkedList: - An implementation that stores elements in a doubly-linked list data structure. It offers constant time for adding and removing elements at the end of the list; and linear time for operations at other positions in the list. Therefore, we can consider using a LinkedList if fast adding and removing elements at the end of the list is required.
Programmin Example:
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class ListExample {
public static void main(String[] args) {
List<String> arrayList = new ArrayList<String>();
arrayList.add("ArrayList Element3");
arrayList.add("ArrayList Element1");
arrayList.add("ArrayList Element2");
List<String> linkedList = new LinkedList<>();
linkedList.add("LinkedList Element3");
linkedList.add("LinkedList Element1");
linkedList.add("LinkedList Element2");
System.out.println(arrayList);
System.out.println(linkedList);
arrayList.remove("ArrayList Element3");
linkedList.remove("LinkedList Element2");
System.out.println("After remove=> " + arrayList);
System.out.println("After remove=> " + linkedList);
System.out.println(arrayList.size());
System.out.println(linkedList.contains("LinkedList Element1"));
}
}
Queue: Queue is another collection provided by Java Collection Framework. Queue is programming implementation of any real-life queue scenario. For example, people buying ticket for a show, People left the queue from front and new people join the queue from back.
LinkedList: this class implements both List and Deque interface, it has characteristics and behaviors of list and queue both.
PriorityQueue: This queue orders elements according to their natural ordering, or by a Comparator provided at construction time. Consider using a PriorityQueue when you want to take advantages of natural ordering and fast adding elements to the tail and fast removing elements at the head of the queue.
ArrayDeque: a simple implementation of the Deque interface. Consider using an ArrayDeque when you want to utilize features of a double ended queue without list-based ones (simpler than a LinkedList).
Programming Example:
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<Student> priorityQueue = new PriorityQueue<>(10, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
return s1.getRollnum() - s2.getRollnum();
}
});
priorityQueue.add(new Student("Eric", 1000));
priorityQueue.add(new Student("Deran", 100));
priorityQueue.add(new Student("Iron Man", 140));
priorityQueue.add(new Student("Hulk", 10));
System.out.println(priorityQueue);
ArrayDeque<Student> arrayDeque = new ArrayDeque<>();
arrayDeque.add(new Student("Eric", 1000));
arrayDeque.add(new Student("Deran", 100));
arrayDeque.add(new Student("Iron Man", 140));
arrayDeque.add(new Student("Hulk", 10));
System.out.println(arrayDeque);
}
}
class Student {
public Student(String name, int rollnum) {
this.name = name;
this.rollnum = rollnum;
}
private String name;
private int rollnum;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getRollnum() {
return rollnum;
}
public void setRollnum(int rollnum) {
this.rollnum = rollnum;
}
@Override
public String toString() {
return "Student [name=" + name + ", rollnum=" + rollnum + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + rollnum;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Student other = (Student) obj;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (rollnum != other.rollnum)
return false;
return true;
}
}
Set: Set is a programming representation of algebraic Set. It does not allow duplicates. Some implementations do not allow nulls, and some keeps the elements in sorted order.
There are following implementations available for Set interface.
TreeSet: TreeSet is an implementation class for Set interface has following properties
· Does not allow duplicates
· Does not allow null values
· Does not keep insertion order
· Keep the elements in natural ordering
HashSet: HashSet has following basic properties
· Does not allow duplicates
· Allow one null values
· Does not keep insertion order
LinkedHashSet: LinkedHashSet has following properties
· Does not allow duplicates
· Allow any number of null values
· Keep insertion order
Programming Example:
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;
public class SetExample {
public static void main(String[] args) {
Set<String> hashSet = new HashSet<>();
hashSet.add("Hello HeshSet");
hashSet.add("Hello HeshSet");
System.out.println(hashSet);
Set<String> treeSet = new TreeSet<>();
treeSet.add("Hello TreeSet");
treeSet.add("Hello TreeSet");
treeSet.add("Abc TreeSet");
System.out.println(treeSet);
Set<String> linkedHashSet = new LinkedHashSet<>();
linkedHashSet.add("Hello LinkedHashSet");
linkedHashSet.add("Hello LinkedHashSet");
System.out.println(linkedHashSet);
hashSet.remove("Hello HeshSet");
System.out.println("hashSet after removing: " + hashSet);
}
}
using the key. You can think it as programming representation of function in Algebra. Map is top level interface it this hierarchy that provides common functionality contract. There are following implementation of Map interface are available in Java.
HashMap: this implementation uses a hash table as the underlying data structure. It implements all the Map operations and allows null values and one null key. This class is roughly equivalent to Hashtable - a legacy data structure before Java Collections Framework, but it is not synchronized and permits nulls. HashMap does not guarantee the order of its key-value elements. Therefore, consider using a HashMap when order does not matter, and nulls are acceptable. For a better understanding of HashMap please refer https://www.ktechlabs.com/2018/05/java-hashmap-internal-implementation.html.
LinkedHashMap: this implementation uses a hash table and a linked list as the underlying data structures, thus the order of a LinkedHashMap is predictable, with insertion-order as the default order. This implementation also allows nulls like HashMap. So, consider using a LinkedHashMap when you want a Map with its key-value pairs are sorted by their insertion order.
TreeMap: this implementation uses a red-black tree as the underlying data structure. A TreeMap is sorted according to the natural ordering of its keys, or by a Comparator provided at creation time. This implementation does not allow nulls. So, consider using a TreeMap when you want a Map sorts its key-value pairs by the natural order of the keys (e.g. alphabetic order or numeric order), or by a custom order you specify.
Programming Example:
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
public class MapExample {
public static void main(String[] args) {
Map<String, Integer> hashMap = new HashMap<>();
hashMap.put("ONE", 1);
hashMap.put("TWO", 2);
hashMap.put("TWO", 2);
Map<Student, String> treeMap = new TreeMap<>(new Comparator<Student>() {
@Override
public int compare(Student o1, Student o2) {
return o2.getRollnum() - o1.getRollnum();
}
});
treeMap.put(new Student("Eric", 1000), "Eric");
treeMap.put(new Student("Deran", 100), "Deran");
treeMap.put(new Student("Iron Man", 140), "Iron Man");
treeMap.put(new Student("Hulk", 10), "Hulk");
Map<Student, String> linkedHashMap = new LinkedHashMap<>();
linkedHashMap.put(new Student("Eric", 1000), "Eric");
linkedHashMap.put(new Student("Deran", 100), "Deran");
linkedHashMap.put(new Student("Iron Man", 140), "Iron Man");
linkedHashMap.put(new Student("Hulk", 10), "Hulk");
System.out.println(hashMap);
System.out.println(treeMap);
System.out.println(linkedHashMap);
}
}
Note: All programming examples are just for your reference, you can perform lots of other operations for your learning and practice.
Note: All programming examples are just for your reference, you can perform lots of other operations for your learning and practice.
No comments:
Post a Comment