Collections In Java
Collections In Java
import java.util.ArrayList;
import java.util.List;
list.addAll(newList); // this will add all the elements in older list (add
new list in older list)
System.out.println(list);
}
}
import java.util.ArrayList;
import java.util.List;
System.out.println(list);
package Learn_Stack;
import java.util.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());
}
}
package Learn_Queue;
import java.util.Queue;
import java.util.LinkedList;
// 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.
//
-----------------------------------------------------------------------------------
-------
// peek()- (using for check which elements are ready for removing) -
Returns the
// head of the queue. returns null 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());
}