Difference between Collection and Collections
Difference between Collection and Collections1). Collection is a root level Interface in Java Collection Framework or collection hierarchy. The Set, List and Queue are the some of the subInterfaces of Collection interface.
The important methods of Collection interface are add(), remove(), size(), clear() etc.
Collections is a utility class in java.util package which contains only static methods that operate on objects of type collection.
Collections class contains only static methods like sort(), min(), fill(), copy(), reverse() etc.
2). Before Java 8, interface was not allowed to contain static methods. collection is an Interface & interface can contain static methods since java 8 and Interface can also contain abstract methods and default methods.
But collections class contains only static methods.
3). Collection interface extends iterable interface
public interface Collections
and Collections class extends Object class
public class Collections extends Object
Similarities between Collection and Collections in Java
1). Both are part of the Java Collections Framework.
2). Both are present in java.util package
3). Both are added to jdk in java version 1.2
Example of Collections
import java.util.ArrayList;
import java.util.*;
public class ConsTest
{
public static void main(String args[]) throws IOException
{
ArrayList<Integer> alist = new ArrayList<Integer>();
alist.add(10);
alist.add(20);
alist.add(5);
alist.add(35);
alist.add(45);
System.out.println("maximum value" + " " + Collections.max(alist));
System.out.println("maximum value" + " " + Collections.min(alist));
ArrayList<String> list = new ArrayList<String>();
list.add("Chandan");
list.add("Chauhan");
list.add("Cha");
Collections.sort(list);
System.out.println("Sorted Strings" + " " + list);
Collections.sort(list, Collections.reverseOrder());
System.out.println("Reversed Strings" + " " + list);
ArrayList<String> slist = new ArrayList<String>(3);
slist.add("Chandan");
slist.add("Chauhan");
slist.add("Cha");
ArrayList<String> dlist = new ArrayList<String>(3);
dlist.add("a");
dlist.add("b");
dlist.add("c");
Collections.copy(dlist, slist);
System.out.println(dlist);
Collections.fill(dlist, "way2testing");
System.out.println(dlist);
}
}
Output :-
maximum value 45
maximum value 5
Sorted Strings [Cha, Chandan, Chauhan]
Reversed Strings [Chauhan, Chandan, Cha]
[Chandan, Chauhan, Cha]
[way2testing, way2testing, way2testing]
Collections in java:-
Any group of individual objects which are represented
as a single unit know as collection of objects.
Collection in java is a framework that provides an architecture to store and manipuate the group of objects.
Java collections can achieve all the operations that you perform on data like searching, sorting, insertion, manipuation etc.
"A collection represents a single unit of objects i.e. a group".
The collection interface (java.utill.Collection) and Map interface (java.util.Map) are two main "root" interface of java collection classes.
What is Framework :-
A framework is a set of classes and interfaces which provide a ready-made architecture.
List Inerface:-
It contains the index based methods to insert, update, delete and search the element.
It can have duplicate elements.
It can have null elements.
ArrayList :-
> ArrayList class implements the List Interface.
> It uses a dynamic array to store element
> It can store duplicate elements
> Arralist class maintain the insertion order and is non-synchronized.
Example :-
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class Oopstutorial
{
public static void main(String[] args)
{
ArrayList<Integer> al= new ArrayList<Integer>();
List<> al2= new LinkedList<Integer>();
al.add(20);
al.add(200);
al.add(40);
al.add(90);
al.add(15);
System.out.println("2nd position object " + al.get(1));
System.out.println("Array list" + al);
System.out.println("Maximum num is " + Collections.max(al));
al2.add(20);
al2.add(20);
al2.add(400);
al2.add(90);
al2.add(15);
System.out.println("linked list" + al2);
System.out.println("Maximum num is " + Collections.max(al2));
System.out.println("Minimum num is " + Collections.min(al));
ArrayList<String>sl = new ArrayList<String>();
sl.add("chandan");
sl.add("adhiraj");
sl.add("singh");
Collections.sort(sl);
System.out.println("sorted list is " + sl);
Collections.reverse(sl);
System.out.println("reversed list is " + sl);
Iterator<String> it = sl.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
Set Interface:-
Set Interface in java is present in java.util package
it extends the collection interface does not allow to store duplicate elements we can store at most one null value in set.
HashSet:-
Hashset class implements the Set Interface.
It represents the collection that uses a hash table for storage. It contains unique Items.
import java.util.TreeSet;
public class Oopstutorial
{
public static void main(String[] args)
{
Set<String> s = new HashSet<String>();
Set<String>ts = new TreeSet<String>();
s.add("chandan");
s.add("adhiraj");
s.add("chandan");
s.add("chauhan");
System.out.println("Set elements are " +s);
ts.add("chandan");
ts.add("adhiraj");
ts.add("chandan");
ts.add("chauhan");
System.out.println("Treeset elements are" +ts); //Elements in treeset stored in ascending order
}
}
hashMap is a Map based collection class that is used for storing Key & Value(associated with keys).
1).It is donated as HashMap.
2).Hashmap contains only unique elements.
3).Hashmap may have only one null key and multiple null values
4). Hashmap maintains no order.
Example:-
import java.util.Map;
import java.util.Set;
public class Oopstutorial
{
public static void main(String[] args)
{
Map< String, Integer > record = new HashMap< String, Integer >();
record.put("chandan", 30);
record.put("baji", 29);
record.put("adhiraj", 31);
System.out.println(record.get("chandan"));
Set<String> k = record.keySet();
for(String i : k)
{
System.out.println(i+ " "+record.get(i));
}
}
}
Output:-
baji 29
chandan 30
adhiraj 31