0% found this document useful (0 votes)
0 views

Collections In Java

The document provides examples of how to use various collection classes in Java, including ArrayList, Stack, and Queue. It demonstrates adding and removing elements from an ArrayList, using a Stack to manage elements in a last-in-first-out manner, and implementing a Queue with LinkedList for first-in-first-out operations. Each section includes code snippets that illustrate the functionality of these collections.

Uploaded by

Rohit Rathore
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Collections In Java

The document provides examples of how to use various collection classes in Java, including ArrayList, Stack, and Queue. It demonstrates adding and removing elements from an ArrayList, using a Stack to manage elements in a last-in-first-out manner, and implementing a Queue with LinkedList for first-in-first-out operations. Each section includes code snippets that illustrate the functionality of these collections.

Uploaded by

Rohit Rathore
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

Collections In Java ------------------

1 Add Elements In ArrayList


-----------------------------------------------------------------------------------
---------------------
package ArrayListDemo;

import java.util.ArrayList;
import java.util.List;

public class DemoArrayList {


public static void main(String[] args) {

// ArrayList<String> student = new ArrayList<>();


// student.add("Rohit");
// student.add("Rakesh");
// student.add("Raghu");
// student.add("Amit");
//
// System.out.println(student);

List<Integer> list = new ArrayList<>();


list.add(1);
list.add(2);
list.add(3);
System.out.println(list);

list.add(4); // this will add 4 at the end of the List


System.out.println(list);

list.add(1,50); // this will add 50 at index no 1 and other continue


System.out.println(list);

List<Integer> newList = new ArrayList<Integer>();


newList.add(150);
newList.add(160);

list.addAll(newList); // this will add all the elements in older list (add
new list in older list)
System.out.println(list);
}
}

2 Remove Elements From ArrayList


-----------------------------------------------------------------------------------
------
package ArrayListDemo;

import java.util.ArrayList;
import java.util.List;

public class Remove_Elements_ArrayList {


public static void main(String[] args) {

List<Integer> list = new ArrayList<>();


list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
list.add(60);
list.add(70);
list.add(80);

System.out.println(list);

for(int i=0; i<list.size(); i++) {


System.out.println("The Elements Is : "+ list.get(i)); // Also we can
use multiply like list.get(i)*2;
}

for (Integer elemet: list) {


System.out.println("ForEach Element is : "+ elemet);
}

// list.set(2, 1000); // Replace index 2 value 30 to 1000 ;


// System.out.println(list);
//
// System.out.println(list.contains(1000)); // Return True Or False ;
// System.out.println(list.contains(500));

// list.remove(1); // remove index 1 value 20


// System.out.println(list);
//
// list.remove(Integer.valueOf(30)); // remove 30 from the list
// System.out.println(list);
//
// list.clear();
// System.out.println(list);
}
}

3 // Stack in collections ---------------------------------------

package Learn_Stack;
import java.util.Stack;

public class StackeDemo {


public static void main(String []args) {
Stack<String> animals = new Stack<>();

animals.push("Lion");
animals.push("Dog");
animals.push("Horse");
animals.push("Cat");

System.out.println("Stack : "+animals);

System.out.println(animals.peek());

animals.pop();
System.out.println(animals);
System.out.println(animals.peek());

}
}

.4 // Queue using LinkedListf in Collections ----------------------------------

package Learn_Queue;

import java.util.Queue;
import java.util.LinkedList;

public class DemoQueue {


public static void main(String[] args) {

// FIFO First In First Out

// methods ------------
//-------------------------------------------------------------------------
-----------------
// add() - Insert the specified element into the queue. if the task is
// successful, add() returns true, if not it throws an exception.

// offer (using for add new elements) - Insert the specified element
into the
// queue. if the task is successful, offre() returns true , if not it
returns
// false.
//
-----------------------------------------------------------------------------------
-------

// element() - Returns the head of the queue. throws an exception if


the queue
// is empty.

// peek()- (using for check which elements are ready for removing) -
Returns the
// head of the queue. returns null if the queue is empty.
//
-----------------------------------------------------------------------------------
-------

// remove() - Returns and removes the head of the queue. throws an


exception if
// the queue is empty.

// poll() - (using for remove elements) - REturns and removes the head
of the
// queue. returns null iif the queue is empty.

//
-----------------------------------------------------------------------------------
-------
Queue<Integer> queue = new LinkedList<>();

queue.offer(125);
queue.offer(126);
queue.offer(130);

System.out.println(queue);

queue.poll();
System.out.println(queue);

System.out.println(queue.peek());

queue.offer(10);
queue.offer(11);
queue.offer(13);
queue.offer(14);
queue.offer(15);

System.out.println(queue);

System.out.println(queue.peek());
}

You might also like