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

Java - Stack Example

The document provides examples and explanations of common Java collection classes including Stack, Queue, Vector, HashSet, and ArrayList. It includes code samples demonstrating how to use each class' methods to add elements, retrieve sizes, iterate through elements, and more. Key points covered include the LIFO nature of Stacks, FIFO nature of Queues, automatic resizing of Vectors and ArrayLists, no fixed size requirement for any of the classes, and how Generics ensure type safety.

Uploaded by

niko.chu7100
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views

Java - Stack Example

The document provides examples and explanations of common Java collection classes including Stack, Queue, Vector, HashSet, and ArrayList. It includes code samples demonstrating how to use each class' methods to add elements, retrieve sizes, iterate through elements, and more. Key points covered include the LIFO nature of Stacks, FIFO nature of Queues, automatic resizing of Vectors and ArrayLists, no fixed size requirement for any of the classes, and how Generics ensure type safety.

Uploaded by

niko.chu7100
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Java Stack Example

Stack class is in java.util package of java. Stack works like last in first out policy. Stack does not require any fix dimension like String array and int array. Stack contains many useful methods. To add element in Stack, we can use push() method of Stack class. To get value from Stack, Stack provides pop() method and Stack size() method. Size() method returns total number of elements in Stack. peek() method looks at the object at the top of this stack without removing it from the stack Stack Example
import java.util.Iterator; import java.util.Stack; public class StackExample { public static void main(String[] args) { Stack<String> sk=new Stack<String>(); sk.push("a"); sk.push("c"); sk.push("e"); sk.push("d"); Iterator it=sk.iterator(); System.out.println("Size before pop() :"+sk.size()); while(it.hasNext()) { String iValue=(String)it.next(); System.out.println("Iterator value :"+iValue); } // get and remove last element from stack String value =(String)sk.pop(); System.out.println("value :"+value); System.out.println("Size After pop() :"+sk.size()); } }

Output Size before pop() :4 Iterator value :a Iterator value :c Iterator value :e Iterator value :d value :d Size After pop() :3

Java Queue Example


Queue interface is in java.util package of java. Queue works like first in first out (FIFO) policy. Queue does not require any fix dimension like String array and int array. Queue contains many useful methods. To add element in Queue, we can use add() method of Queue interface. To get value from Queue, Queue provides poll() and peek() method and Queue size() method. Size() method returns total number of elements in Queue. peek() method looks at the object at the head of this Queue without removing it from the Queue Queue Example
import java.util.Iterator; import java.util.LinkedList; import java.util.Queue; public class QueueExample { public static void main(String[] args) { Queue<String> qe=new LinkedList<String>(); qe.add("b"); qe.add("a"); qe.add("c"); qe.add("e"); qe.add("d"); Iterator it=qe.iterator(); System.out.println("Initial Size of Queue :"+qe.size()); while(it.hasNext()) { String iteratorValue=(String)it.next(); System.out.println("Queue Next Value :"+iteratorValue); } // get value and does not remove element from queue System.out.println("Queue peek :"+qe.peek()); // get first value and remove that object from queue System.out.println("Queue poll :"+qe.poll()); System.out.println("Final Size of Queue :"+qe.size()); } }

Output Initial Size of Queue :5 Queue Next Value :b Queue Next Value :a Queue Next Value :c Queue Next Value :e Queue Next Value :d Queue peek :b

Queue poll :b Final Size of Queue :4

Java Vector Example


Vector class is in java.util package of java. Vector is dynamic array which can grow automatically according to the required need. Vector does not require any fix dimension like String array and int array. Vector contains many useful methods. To add element in Vector, we can use add() method of vector class. To add elements at fix position, we have to use add(index, object) method. To get value from Vector, Vector provides get() method and Vector size() method. Size() method returns total number of elements in Vector. Vector is synchronized, ArrayList is not Vector Example
import java.util.Vector; public class VectorExample { public static void main(String[] args) { Vector<String> vc=new Vector<String>(); // <E> Element type of Vector e.g. String, Integer, Object ...

// add vector elements vc.add("Vector Object 1"); vc.add("Vector Object 2"); vc.add("Vector Object 3"); vc.add("Vector Object 4"); vc.add("Vector Object 5"); // add vector element at index vc.add(3, "Element at fix position"); // vc.size() inform number of elements in Vector System.out.println("Vector Size :"+vc.size()); // get elements of Vector for(int i=0;i<vc.size();i++) { System.out.println("Vector Element "+i+" :"+vc.get(i)); } } }

Java HashSet Example


HashSet class is in java.util package of java. HashSet does not require any fix dimension like String array and int array. HashSet contains many useful methods. To add element in HashSet, we can use add() method of HashSet class. To get value from HashSet, HashSet provides iterator () method and HashSet size() method. size() method returns total number of elements in HashSet. HashSet Example
import java.util.HashSet; import java.util.Iterator; public class HashSetExample { public static void main(String[] args) { HashSet<String> hs=new HashSet<String>(); // duplicate element is not permitted hs.add("b"); hs.add("a"); hs.add("c"); hs.add("d"); hs.add("d"); Iterator it=hs.iterator(); while(it.hasNext()) { String value =(String)it.next(); System.out.println("Value :"+value); } //find size of hashSet System.out.println("Size :"+hs.size()); // Remove element from hashSet : hs.remove("d"); // To remove all object from hashSet hs.clear(); } }

Output Value :d Value :b Value :c Value :a Size :4

Java ArrayList Example


ArrayList class is in java.util package of java. ArrayList is dynamic array which can grow automatically according to the required need. ArrayList does not require any fix dimension like String array and int array. ArrayList contains many useful methods. To add element in ArrayList, we can use add() method of ArrayList class. To add elements at fix position, we have to use add(index, object) method. To get value from ArrayList, ArrayList provides get() method and ArrayList size() method. Size() method returns total number of elements in ArrayList. Example of ArrayList in java
import java.util.ArrayList; public class ArrayListExample { public static void main(String[] args) { ArrayList<String> arlist=new ArrayList<String>(); //<E> it is return type of ArrayList arlist.add("First Element"); // adding element in ArrayList arlist.add("Second Element"); arlist.add("Third Element"); arlist.add("forth Element"); arlist.add("fifth Element"); // add element with index for fix order arlist.add(2, "Fixed Order of Element"); // arlist.size() inform number of elements in ArrayList System.out.println("ArrayList Size :"+arlist.size()); // get elements of ArrayList for(int i=0;i<arlist.size();i++) { System.out.println("ArrayList Element "+i+" :"+arlist.get(i)); } } }

output ArrayList Size :6 ArrayList Element 0 :First Element ArrayList Element 1 :Second Element ArrayList Element 2 :Fixed Order of Element ArrayList Element 3 :Third Element ArrayList Element 4 :forth Element ArrayList Element 5 :fifth Element

Generics
Using generics allows you to define classes that can work with any defined type. This is very similar to the use of templates in C++. When you use collections like the vector and the stack you do not have to do any typecasting to get the correct value. This ensures that you cannot make silly errors with getting items from a collection. Without generics we had to use typecasting to get the correct data out of structures. The other problem is we are going to have problems if try to add the wrong data type. Generics ensure that we use the right data type and that we do not have any typecasting to do. If we use the wrong data type then the program will not compile. Example:
Vector<Integer> v = new Vector<Integer>();

The vector is a class that uses generics. The above line declares a vector that can only function on integer objects. If we try to add a String object we will get an error. Try this:
v.add("james");

When I do this I get a message that says: Quote

cannot find symbol symbol: method add(java.lang.String) location: class java.util.Vector<java.util.Integer>;

This occurs because when I created the Vector it was compiled in a way that it can only use Integers. Anything else crashes at compilation. So if you use the wrong data type you will get an error at compile time instead of running time. Making it easier to find your error.
Creating generic classes

We can make our own generic classes. To do this we are going to make our own Stack class it is going to be a very simple Stack. If you don't know, the Stack is a structure that has restricted access. You can only access the top item of the Stack. The stack provides a first-in, last-out order meaning you can only access the top item of the stack. See my tutorial here: http://forum.codecal...447-stacks.html to see more information on the stack. Are stack class is only going to push items onto the stack and remove items from the stack. Our stack will use a Vector as the underlying structure so it will be able to resize. Note that we cannot use an array as the underlying structure? Why? This is because generic classes are compiled at runtime and the parameters are replaced with the appropriate type. However, arrays are initalized with a type at declaration and thus they cannot be generic. So

our underlying structure will be a vector. Creating a stack is really easy. All we need is a member variable that keeps track of the index of the top item. We store all the items in a private vector but only provide a method that can access the top item of the stack. The use of the private keyword in front of the Vector is what makes this structure possible. If we didn't include it then we would just be creating a wrapper of the Vector and what is the purpose of that?
Creating the stack

To define that our stack is generic we use angle brackets after the class name and in those brackets we place the names of different parameters. These parameters will define what type of data the stack holds. So the class skeleton looks like this:
public class Stack<E> { }

Now what instance members do we need? We need a vector, and a counter for the index of the top. That is all. The vector will be of type E indicating that it holds a user defined data type and the index variable will be an integer set to -1. So add these just after the class declaration.
private Vector<E> stack; // holds the contents of the stack private int top; // the index of the top item in the stack. -1 indicates empty.

Now to declare the constructor.


public Stack() { this.stack = new Vector<Integer>(); this.top = -1; }

We create a new Vector that is empty, this is going to hold the stack. We will initalize the top variable to -1 which indicates that the stack is empty. Now the next two methods we will define are push and pop. The push method adds an item to the top of the stack and the pop method removes an item from the stack and returns it. The push method:
public void push(E obj) { stack.add(obj); top++; }

We use the add method in the vector to add the parameter to the stack. The parameter is of type E which will be replaced with the type defined by the programmer when it is used. We increase the top variable by 1 so we have the index of the top item stored. The pop method is very similar to this, it returns type E and returns the item at the top of the stack. However we have to remove this item from the stack so we need to store it in a temporary variable. We can't just do

return stack.get(top);

Why? We have to do two other things, decrease the top variable by 1 so it points to the new top of the stack. We also have to remove the item from the Vector. If we don't we will run into problems if we add a new item later. We will see this item again which is not what we want. The problem with this I illustrated in my tutorial about Vectors. We have to copy the vector contents into a new array every time we erase the end item. This can take a really long time for big stacks. We will only remove an item from the Stack if it is not empty. The pop method returns null if the stack is empty. So the pop method looks like this:
public E pop() { if (top == -1) { return null; // stack is empty } E temp = stack.get(top); stack.remove(top); top--; return temp; }

This covers everything the Stack needs to do but it might be useful to define two methods that check if the stack is empty and how many items are in the stack. The stack is empty if top = -1. So the empty method looks like this:
public boolean isEmpty(){ return top == -1; }

The number of items in the stack then is top+1. If the stack is empty (top = -1) and the number of items is 0.
public int size() { return top+1; }

So the full class looks like this:


import java.util.Vector; public class Stack<E> { private Vector<E> stack; private int top = -1; // index of the top item of the stack public Stack() { stack = new Vector<E>(); } public void push(E obj) { stack.add(obj); top++; } public E pop() { if (top == -1) { return null; // stack is empty } E temp = stack.get(top); stack.remove(top);

top--; return temp; } public boolean isEmpty(){ return top == -1; } public int size() { return top+1; } }

Now to test this class you can use this code:


public class generics { /** * @param args the command line arguments */ public static void main(String[] args) { Stack<Integer> st = new Stack<Integer>(); st.push(5); st.push(4); st.push(3); st.push(9); if (st.size() == 0) { System.out.println("Stack is empty."); } else { System.out.println("Stack contains " + st.size() + " items."); } while (!st.isEmpty()) { System.out.println(st.pop()); } if (st.size() == 0) { System.out.println("Stack is empty."); } else { System.out.println("Stack contains " + st.size() + " items."); } st.push(7); System.out.println("Size: " + st.size()); System.out.println(st.pop()); } }

Here's an example of a priority queue sorting by string length:


// Test.java import java.util.Comparator;

import java.util.PriorityQueue; public class Test { public static void main(String[] args) { Comparator<String> comparator = new StringLengthComparator(); PriorityQueue<String> queue = new PriorityQueue<String>(10, comparator); queue.add("short"); queue.add("very long indeed"); queue.add("medium"); while (queue.size() != 0) { System.out.println(queue.remove()); } } } // StringLengthComparator.java import java.util.Comparator; public class StringLengthComparator implements Comparator<String> { @Override public int compare(String x, String y) { // Assume neither string is null. Real code should // probably be more robust if (x.length() < y.length()) { return -1; } if (x.length() > y.length()) { return 1; } return 0; } }

subSet public SortedSet subSet(Object fromElement, Object toElement) Returns a view of the portion of this sorted set whose elements range from fromElement, inclusive, to toElement, exclusive.

headSet public SortedSet headSet(Object toElement) Returns a view of the portion of this sorted set whose elements are strictly less than toElement. The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations. tailSet public SortedSet tailSet(Object fromElement) Returns a view of the portion of this sorted set whose elements are greater than or equal to fromElement. The returned sorted set is backed by this sorted set, so changes in the returned sorted set are reflected in this sorted set, and vice-versa. The returned sorted set supports all optional set operations.

Important "Backed Collection" Methods for TreeSet and TreeMap Method Description headSet(e, b*) Returns a subset ending at element e and exclusive of e headMap(k, b*) Returns a submap ending at key k and exclusive of key k tailSet(e, b*) Returns a subset starting at and inclusive of element e tailMap(k, b*) Returns a submap starting at and inclusive of key k subSet(s, b*, e, b*) Returns a subset starting at element s and ending just before element e subMap(s, b*, e, b*) Returns a submap starting at key s and ending just before key s

You might also like