
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 33676 Articles for Programming

3K+ Views
The Stack is a data structure that is used to store elements in a Last In First Out (LIFO) manner. In Java, a stack is represented by the java.util.Stack class. It provides methods to create and manipulate a stack. Get an Element of a Stack without Removing it The pop() method of the Stack class removes the element at the top of the stack and returns it. But there is no direct way to access an element without removing it. However, we can see/view the top element of a Stack in Java using the peek() method. This method returns the ... Read More

4K+ Views
The method java.util.Stack.empty() is used to check if a stack is empty or not. This method requires no parameters. It returns true if the stack is empty and false if the stack is not empty.A program that demonstrates this is given as follows −Example Live Demoimport java.util.Stack; public class Demo { public static void main (String args[]) { Stack stack = new Stack(); stack.push("Amy"); stack.push("John"); stack.push("Mary"); System.out.println("The stack elements are: " + stack); System.out.println("The stack is empty? " + stack.empty()); ... Read More

214 Views
The isEmpty() method is used in Java to check whether a NavigableMap is empty or not.First, create a NavigableMap and add elements to it −NavigableMap n = new TreeMap(); n.put(5, "Tom"); n.put(9, "John"); n.put(14, "Jamie"); n.put(1, "Tim"); n.put(4, "Jackie"); n.put(15, "Kurt"); n.put(19, "Tiger"); n.put(24, "Jacob");Now, check whether the Map is empty or not −System.out.println("Map is empty? " + n.isEmpty());The following is an example to implement isEmpty() method and check whether the Map is empty −Example Live Demoimport java.util.*; public class Demo { public static void main(String[] args) { NavigableMap n = new TreeMap(); n.put(5, ... Read More

2K+ Views
To create a Sorted Set, firstly create a Set −Set s = new HashSet();Add elements to the above set −int a[] = {77, 23, 4, 66, 99, 112, 45, 56, 39, 89}; Set s = new HashSet(); try { for(int i = 0; i < 5; i++) { s.add(a[i]); }After that, use TreeSet class to sort −TreeSet sorted = new TreeSet(s);Get the last element, using the last() method −System.out.println("Last element of the sorted set = "+ (Integer)sorted.last());The following is the code to get the last element from a Sorted Set in Java −Example Live Demoimport java.util.*; public class Demo ... Read More

2K+ Views
Fetch and remove the first element in Queue using the poll() method.Create a queue −Queue q = new LinkedList();Add some elements −q.add("abc"); q.add("def"); q.add("ghi"); q.add("jkl"); q.add("mno"); q.add("pqr"); q.add("stu"); q.add("vwx");Now, remove the first element −q.poll()The following is an example to implement the poll() method −Example Live Demoimport java.util.LinkedList; import java.util.Queue; public class Demo { public static void main(String[] args) { Queue q = new LinkedList(); q.add("abc"); q.add("def"); q.add("ghi"); q.add("jkl"); q.add("mno"); q.add("pqr"); q.add("stu"); q.add("vwx"); ... Read More

3K+ Views
To remove an element from a Queue, use the remove() method.First, set a Queue and insert some elements −Queue q = new LinkedList(); q.offer("abc"); q.offer("def"); q.offer("ghi"); q.offer("jkl"); q.offer("mno"); q.offer("pqr"); q.offer("stu"); q.offer("vwx");Remove the first element −System.out.println("Queue head = " + q.element()); System.out.println("Removing element from queue = " + q.remove());The following is an example −Example Live Demoimport java.util.LinkedList; import java.util.Queue; public class Demo { public static void main(String[] args) { Queue q = new LinkedList(); q.offer("abc"); q.offer("def"); q.offer("ghi"); q.offer("jkl"); q.offer("mno"); q.offer("pqr"); ... Read More

757 Views
The element() method in Java Queues is used to return the element at the front of the container and does not remove it.The following is an example. Firstly, create a Queue and add some elements −Queue q = new LinkedList(); q.offer("abc"); q.offer("def"); q.offer("ghi"); q.offer("jkl"); q.offer("mno"); q.offer("pqr"); q.offer("stu"); q.offer("vwx");Now use the element() method as shown below −System.out.println("Queue head = " + q.element());The following is the complete example −Example Live Demoimport java.util.LinkedList; import java.util.Queue; public class Demo { public static void main(String[] args) { Queue q = new LinkedList(); q.offer("abc"); q.offer("def"); ... Read More

2K+ Views
An element in an Java LinkedList can be replaced using the java.util.ArrayList.set() method. This method has two parameters i.e the index at which the LinkedList element is to be replaced and the element that it should be replaced with. ArrayList.set() method returns the element that was at the position specified at the index previously.A program that demonstrates this is given as follows −Example Live Demoimport java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Pear"); l.add("Apple"); l.add("Mango"); l.add("Guava"); ... Read More

7K+ Views
A LinkedList can be converted into an ArrayList by creating an ArrayList such that the parameterized constructor of the ArrayList initialises it with the elements of the LinkedList.A program that demonstrates this is given as follows −Example Live Demoimport java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class Demo { public static void main(String[] args) { LinkedList l = new LinkedList(); l.add("Orange"); l.add("Apple"); l.add("Peach"); l.add("Guava"); l.add("Pear"); List aList = new ArrayList(l); System.out.println("The ArrayList elements are: "); ... Read More

278 Views
To create a queue using LinkedList class, try the following −Queue q = new LinkedList(); q.offer("abc"); q.offer("def"); q.offer("ghi"); q.offer("jkl"); q.offer("mno"); q.offer("pqr"); q.offer("stu"); q.offer("vwx");After that to print the elements, you need to use check a condition for Queue as shown below −Object ob; while ((ob = q.poll()) != null) { System.out.println(ob); }The following is an example −Example Live Demoimport java.util.LinkedList; import java.util.Queue; public class Demo { public static void main(String[] args) { Queue q = new LinkedList(); q.offer("abc"); q.offer("def"); q.offer("ghi"); q.offer("jkl"); q.offer("mno"); ... Read More