Skip to main content

Collection Framework java

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()){

                     System.out.print(itr.next());

}

 

Output:- [hello, hii, bye, bye]// its follow insertion order

             Hellohiibyebye



Vector: -

=============

Vector is a Synchronized only one different between Array list and           vector.

  1. Vector<String> v=new Vector<String>();  
  2. v.add("Ayush");  
  3. v.add("Amit");  
  4. v.add("Ashish");  
  5. 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

Popular posts from this blog

Angular directive

 1.                        ๐Ÿ‘‡๐Ÿ’ช  Angular Directive                   ================================================  An Angular directive is a special kind of component that adds behavior to an HTML element  or an Angular component. Directives can manipulate the DOM (Document Object Model),  modify the appearance and behavior of an element, or create new components dynamically. ==================================================================  There are three types of Angular directives: 1. Component Directive:  This is the most commonly used type of directive, and it is responsible  for defining custom elements that act as reusable components. Component directives have their  own template and styles and can receive data and events through input and output bindings. Exp:-Here's an example of a component directive in Angular: import { C...

ngModel in Angular

 ngModel :-                                                             ================== ngModel is a directive in angular that allow two way data binding. it bind th value of an HTML control (such as input, select, or textarea) to a prperty on the component and updates that property when the user changes th evalue in the control. To use "ngModel" in Angular, you need to import the formModule in your module And add it to the "imports" array. Then you can add the "ngModel" directive to an HTML controlin your template and  bind it to a property on ypur component using square brackets. Here is an example of using "ngModel" in a simple input form: Example:- <!-- component.html --> <input [(ngModel)]="name"> <!-- component.ts --> export class MyComponent {   name: string; } ================================== E...