Collection:
================
A collection is an object that can hold references to other objects. The collection interfaces declare the operations
that can be performed on each type of collection.
Collection
is an Interface, and its store group of object.
Framework: - Framework is a api its use to provide a readymade
functionality.
It represents
a set of classes and interfaces.
Collection Hierarchy:
List: -
1. List Follow Insertion order
2. List Store Duplicate data
3. List Size not limit
4. List store data index base
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 cave dublicate values
Exp :- List
<String>list1 =new ArrayList<>();
List <Integer>list2=new
LinkedList<>();
List<String>list3=new
Vactor<>();
List<Float>list4=new
Stack<>();
=====================================
1.Array List: -Array list is store duplicate data Its store index base value
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 it is a not synchronized. The element stored in the ArrayList class can be randomly accessed.
ArrayList<String>list1=new
ArrayList<String>();
list1.add(“Ankit”);
List1.add(“Ankit”);//dublicate
data accept
list1.add(“Sai”);
list1.add(“chakra”);
System.out.print(list1);
Traversing
list through iterator
Iterator
itr=list1.Iterator();
While(itr.hasNext()){
System.out.print(itr.next());
}
Output: -
[Ankit, Ankit, Sai, chakra ]//follow
insertion order
Output:- AnkitAnkitSaichackra
LinkedList:-
LinkedList implements the collection interface. It uses a doubly linked list internally to store the elements. It can be store the duplicate elements. It maintains the insertion order and it’s not synchronized. In linked list the manipulation is fast because no shifting is required.
Exam:-
LinkedList<String>l1=new
LinkedList<String>();
l1.add(“hello”);
l1.add(hii”);
l1.add(“bye”);
l1.add(“bye”);//its dublicate
System.out.print(l1);
Iterator itr=l1.Iterator();
While(itr.hasNext()){
![]()
}
Output:- [hello, hii, bye, bye]// its follow insertion order
Hellohiibyebye
Vector: -
=============
Vector is a Synchronized
only one different between Array list and vector.
- Vector<String> v=new Vector<String>();
- v.add("Ayush");
- v.add("Amit");
- v.add("Ashish");
- v.add("Garima");
Stack: -
The Stack is the subclass of vector.it implements the last in
first out (LIFO) data structure. The Stack contains all of the method of vector
class and also provides its methods like Boolean push (), Boolean peek (),
Boolean push (object o).
Exam: -
Stack<String>s1=new Stack<String>();
S1.push(“ank”);
S1.push(“ank”);
S1.push(“Stack”);
S1.push(“Stack”);
System.out.print(s1);
S1.pop();
System.out.print(s1);
Output:-
[ank, ank, Stack, Stack]
[ank, ank, Stack]
Set: -
=====
Set is a
child interface of collection interface
Set not
follow insertion order
Set not
accept duplicate value
Set not
store index base value
Set is not
synchronized
We can store
at most one null value in set. Set is implemented by HashSet ,LinkedHashSet ,and TreeSet.
Set<String>s1=new
HashSet<String>();
Set<String>s1=new
LinkedHashSet<String>();
Set<String>s1=new
TreeSet<String>();
1.HashSet :-
========
Hash Set is not accept duplicate value
Its accept
only one null value
HashSet
class implements set interface. It represents the collection that uses a
hashtable for storage. Hashing is used to store the elements in the HashSet. It
contains unique items.
HashSet<String>H1=new
HashSet <String> ();
H1.add(“Ankit”);
H1.add(“amna”);
H1.add(“hashset”);
//not accept duplicate value
H1.add(null);//its
accept only one null
System.out.print(H1);
[Ankit,
null, hashset, amna] //insertion order not follow
LinkedHashSet
:-
==============
LinkedHashSet
class represents the LinkedList implementation of Set interface. It extends the
HashSet class and implements Set interface. Like HashSet It also contains
unique elements. It maintains the insertion order and permits null elements
LinkedHashSet<String>L1=new
LinkedHashSet<String>();
L1.add(“aman”);
L1.add(“aman”);//not
accepted duplicate value
L1.add(“ram”);
L1.add (“liked
list);
System.out.print(L1);
[aman, ram,
liked list]//its follow insertion order
Tree Set: -
=========
Java Treeset
class implements the Set interface that uses a tree for storage. Like HashSet
TreeSet also contains unique elements. However, the access and retrieval time
of Treeset is quite fast the element in tree Set Stored in ascending order
TreeSet<Integer>T1=new
TreeSet<Integer>();
T1.add (23);
T1.add (32);
T1.add (10);
T1.add (5);
T1.add (40);
T1.add (32)
;//not accept duplicate value
System.out.print(T1);
Output:
[5, 10, 23,
32, 40]// its follow ascending order

Comments
Post a Comment