Java examples on java.util
- How do I get date, month, year values from the current date?
- How do I calculate difference in days between two dates?
- How do I convert an array into a collection object?
- How do I add or subtract a date?
- How do I read file using Scanner class?
- How do I use StringTokenizer to split a string?
- How do I read user input from console using Scanner class?
- How do I sort items of an ArrayList?
- How do I convert array to Set?
- How do I convert Set into List?
- How do I get the current month name?
- How do I sort array values in descending order?
- How do I convert milliseconds value to date?
- How do I get the last day of a month?
- How do I generate UUID / GUID?
- How do I sort items in a Set?
- How do I remove duplicate element from array?
- How do I Convert Collection to ArrayList?
- How do I know if an ArrayList contains a specified item?
- How do I use ResourceBundle for i18n?
- How do I convert string of time to time object?
- How do I get day of week?
- How do I add hours, minutes or seconds to a date?
- How do I convert time between timezone?
- How do I read / write data in Windows registry?
- How do I convert a collection object into an array?
- How do I use the HashMap class?
- How do I know the minimum and maximum number in array?
- How do I shuffle elements of an array?
- How do I create an empty collection object?
- How do I create a scheduled task using timer?
- How do I generate a random array of numbers?
- How do I get number of days in a month?
- How do I convert Set into array?
- How do I convert Properties into Map?
- How do I split a string using Scanner class?
- How do I check if a string is a valid date?
- How do I create a repeated sequence of character?
- How do I convert LinkedList to array?
- How do I get currency symbol?
- How do I get the last date of a month?
- How do I sort array values in case insensitive order?
- How do I convert ResourceBundle to Properties?
- How do I search collection element?
- How do I compare two dates?
- How do I sort an Enumeration?
- How do I get a list of country names?
- How do I read a configuration file using java.util.Properties?
- How do I use ArrayList class?
- How do I sort an array of objects?
- How do I convert day of the year to date?
- How do I check if a year is a leap year?
- How do I sort string of numbers in ascending order?
- How do I convert ResourceBundle to Map?
- How do I compare if two arrays are equal?
- How do I get all available timezones?
- How do I know the size of ArrayList?
- How do I sort elements of an array?
- How do I Convert Array to Collection?
- How do I search specific value in an array?
- How do I convert day of year to day of month?
- How do I sort LinkedList elements?
- How do I create a queue using LinkedList class?
- How do I convert string date to long value?
- How do I set a default Locale?
- How do I use the Stack class in Java?
- How do I get the last item of SortedSet?
- How do I get all available currency codes?
- How do I get the first item of SortedSet?
- How do I reverse the order of LinkedList elements?
- How do I trim the capacity of an ArrayList?
- How do I change date formatting symbols?
- How do I create Deque using ArrayDeque class?
- How do I use the SortedMap interface?
- How do I know if a date is before another date?
- How do I know if a date is after another date?
- How do I rotate elements of a collection?
- How do I get the first and the last element of a LinkedList?
- How do I insert an item into LinkedList?
- How do I count occurrence of a day between two dates?
- How do I create a LinkedList?
- How do I remove an item from LinkedList?
- How do I retrieve LinkedList objects using ListIterator?
- How do I retrieve particular object from LinkedList?
- How do I add item at the beginning or the end of LinkedList?
- How do I convert a Vector into an array?
- How do I remove the first and last item from LinkedList?
- How do I retrieve LinkedList objects using Iterator?
- How do I get all availables Locale's language codes?
- How do I remove elements from Deque?
- How do I create a Locale object using a variant?
- How do I create a last in first out Deque?
- How do I get host's default timezone id?
- How do I read Vector object using Enumeration?
- How do I create a Hashtable and iterates its contents?
- How do I get timezone ids by their milliseconds offset?
- How do I remove the first or the last found element from Deque?
- How do I validate input when using Scanner?
- How do I add elements into a Deque?
- How do I clear the content of an array?
- How do I reverse the order of array elements?
- How to get random key-value pair from Hashtable?
How do I use the Stack class in Java?
Stack is an extension of the java.util.Vector class that provided a LIFO (last-in-first-out) data structure. This class provide the usual method such as push and pop. The peek method is used the get the top element of the stack without removing the item.
package org.kodejava.example.util;
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
//
// We stored some values in the stack object.
//
for (int i = 0; i < 10; i++) {
stack.push(i);
System.out.print(i + " ");
}
System.out.println("");
//
// Searching for an item in the stack. The position returned
// as the distance from the top of the stack. Here we search
// for the 3 number in the stack which is in the 7th row of
// the stack.
//
int position = stack.search(3);
System.out.println("Search result position: " + position);
//
// The current top value of the stack
//
System.out.println("Stack top: " + stack.peek());
//
// Here we popping out all the stack object items.
//
while (!stack.empty()) {
System.out.print(stack.pop() + " ");
}
}
}