Collections In Java ------------------
1 Add Elements In ArrayList
-----------------------------------------------------------------------------------
---------------------
package ArrayListDemo;
import [Link];
import [Link];
public class DemoArrayList {
public static void main(String[] args) {
// ArrayList<String> student = new ArrayList<>();
// [Link]("Rohit");
// [Link]("Rakesh");
// [Link]("Raghu");
// [Link]("Amit");
//
// [Link](student);
List<Integer> list = new ArrayList<>();
[Link](1);
[Link](2);
[Link](3);
[Link](list);
[Link](4); // this will add 4 at the end of the List
[Link](list);
[Link](1,50); // this will add 50 at index no 1 and other continue
[Link](list);
List<Integer> newList = new ArrayList<Integer>();
[Link](150);
[Link](160);
[Link](newList); // this will add all the elements in older list (add
new list in older list)
[Link](list);
}
}
2 Remove Elements From ArrayList
-----------------------------------------------------------------------------------
------
package ArrayListDemo;
import [Link];
import [Link];
public class Remove_Elements_ArrayList {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
[Link](10);
[Link](20);
[Link](30);
[Link](40);
[Link](50);
[Link](60);
[Link](70);
[Link](80);
[Link](list);
for(int i=0; i<[Link](); i++) {
[Link]("The Elements Is : "+ [Link](i)); // Also we can
use multiply like [Link](i)*2;
}
for (Integer elemet: list) {
[Link]("ForEach Element is : "+ elemet);
}
// [Link](2, 1000); // Replace index 2 value 30 to 1000 ;
// [Link](list);
//
// [Link]([Link](1000)); // Return True Or False ;
// [Link]([Link](500));
// [Link](1); // remove index 1 value 20
// [Link](list);
//
// [Link]([Link](30)); // remove 30 from the list
// [Link](list);
//
// [Link]();
// [Link](list);
}
}
3 // Stack in collections ---------------------------------------
package Learn_Stack;
import [Link];
public class StackeDemo {
public static void main(String []args) {
Stack<String> animals = new Stack<>();
[Link]("Lion");
[Link]("Dog");
[Link]("Horse");
[Link]("Cat");
[Link]("Stack : "+animals);
[Link]([Link]());
[Link]();
[Link](animals);
[Link]([Link]());
}
}
.4 // Queue using LinkedListf in Collections ----------------------------------
package Learn_Queue;
import [Link];
import [Link];
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<>();
[Link](125);
[Link](126);
[Link](130);
[Link](queue);
[Link]();
[Link](queue);
[Link]([Link]());
[Link](10);
[Link](11);
[Link](13);
[Link](14);
[Link](15);
[Link](queue);
[Link]([Link]());
}