0% found this document useful (0 votes)
15 views8 pages

Collection in Jav1

The document provides an overview of the Java Collection framework, detailing various data structures such as Stack, Vector, LinkedList, and ArrayList. It explains their functionalities, methods, and provides code examples for each structure. The document highlights operations like insertion, deletion, searching, and sorting within these collections.

Uploaded by

aman8122002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views8 pages

Collection in Jav1

The document provides an overview of the Java Collection framework, detailing various data structures such as Stack, Vector, LinkedList, and ArrayList. It explains their functionalities, methods, and provides code examples for each structure. The document highlights operations like insertion, deletion, searching, and sorting within these collections.

Uploaded by

aman8122002
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Collection in Java :-

The Collection in Java is a framework that provides an architecture


to store and manipulate the group of objects.

Java Collections can achieve all the operations that you perform on a
data such as searching, sorting, insertion, manipulation, and deletion.

Stack:-

The stack is a linear data structure that is used to store the collection of
objects. It is based on Last-In-First-Out (LIFO). Java collection framework
provides many interfaces and classes to store the collection of objects. One of
them is the Stack class that provides different operations such as push, pop,
search, etc.

Stack st=new Stack();

Method Modifier Method Description


and Type

push(E item) E The method pushes (insert) an element onto


the top of the stack.

pop() E The method removes an element from the


top of the stack and returns the same
element as the value of that function.

peek() E The method looks at the top element of the


stack without removing it.

search(Object int The method searches the specified object


o) and returns the position of the object.
1. import java.util.*;
2. class demo
3. {
4. public static void main(String[] args)
5. {
6. Stack st=new Stack();
7. //pushing elements into Stack
8. st.push("Mac Book");
9. st.push("HP");
10. st.push("DELL");
11. st.push("Asus");
12. System.out.println("Stack: " + stk);
13. // Search an element
14. int location = st.search("HP");
15. System.out.println("Location of HP " + location);
16. }
17. }
Vector:-
Vector is like the dynamic array which can grow or shrink its size. Unlike
array, we can store n-number of elements in it as there is no size limit. It is a
part of Java Collection framework

1 addElement() It is used to append the specified component to the end of


this vector. It increases the vector size by one.

2 capacity() It is used to get the current capacity of this vector.

3 clear() It is used to delete all of the elements from this vector.

4 remove() It is used to remove the specified element from the vector. If


the vector does not contain the element, it is unchanged.

6 size() It is used to get the number of components in the given


vector.

7 sort() It is used to sort the list according to the order induced by


the specified Comparator.

Collections.sort(vector name);

Example:-
import java.util.*;
class vv

public static void main(String args[])

Vector v=new Vector(50);

v.addElement("ram");

v.addElement("sham");

v.addElement("gopal");

System.out.println("vector element is"+v);

System.out.println("vector capacity is"+v.capacity());

//v.clear();

System.out.println("vector size is"+v.size());

Collections.sort(v);

System.out.println("vector sorting is");

System.out.println(v);

Out put:-
D:\ajpr>javac vv.java

Note: vv.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

D:\ajpr>java vv

vector element is[ram, sham, gopal]

vector capacity is50

vector size is3

vector sorting is

[gopal, ram, sham]

LinkedList class
Java LinkedList class uses a doubly linked list to store the elements. It
provides a linked-list data structure. In the case of a doubly linked list, we
can add or remove elements from both sides.

void addFirst(E e) It is used to insert the given element at the beginning of a list.

void addLast(E e) It is used to append the given element to the end of a list.

void clear() It is used to remove all the elements from a list.


getFirst() It is used to return the first element in a list.

getLast() It is used to return the last element in a list.

Add() Add the element in linked list

1. import java.util.*;
2. class LinkedList1{
3. public static void main(String args[]){
4.
5. LinkedList al=new LinkedList();
6. al.add("Ravi");
7. al.add("Vijay");
8. al.add("Ravi");
9. al.add("Ajay");
10.
11. Iterator itr=al.iterator();
12. while(itr.hasNext()){
13. System.out.println(itr.next());
14. }
15. }
16. }

Output: Ravi
Vijay
Ravi
Ajay
ArrayList:-

Java ArrayList class uses a dynamic array for storing the elements. It is like
an array, but there is no size limit. We can add or remove elements anytime.
So, it is much more flexible than the traditional array.

ArrayList al = new ArrayList();

Method Description

void add(int index, E It is used to insert the specified element at the specified
element) position in a list.

void clear() It is used to remove all of the elements from this list.

get(int index) It is used to fetch the element from the particular


position of the list.

remove(int index) It is used to remove the element present at the specified


position in the list.

1. import java.util.*;
2. class ArrayListExample1{
3. public static void main(String args[]){
4. ArrayList list=new ArrayList();//Creating arraylist
5. list.add("Mango");//Adding object in arraylist
6. list.add("Apple");
7. list.add("Banana");
8. list.add("Grapes");
9. //Printing the arraylist object
10. System.out.println(list);
11. }
12. }

Output:
[Mango, Apple, Banana, Grapes]

You might also like