Java.util - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.util Stack

Java.util Stack

shape Description

The java.util.Stack class merits a little clarification all alone. In the content on the List interface the Stack class is recorded as a usage. The average utilization of a Stack is not as a List however. A Stack is an information structure where user add components to the highest point of the stack, furthermore expel components from the top once more. This is likewise alluded to as the LIFO guideline. Conversely, a Queue uses a FIFO guideline. Stack's are truly helpful for a few sorts of information preparing, for example in the event that user is parsing a XML document utilizing either StAX or SAX .

Java XML SAX Parser

shape Description

SAX is incredible environment to work in. SAX stands for Simple API for XML. SAX is an API(Application Programming Interface) which provides a programmatic event-based parsing XML document i.e. it establishes an arena to write programs to the parser. SAX is created by a group of developers but not W3C whose first version 1.0 was released in May 1998. SAX provides an event-based parser that will expose XML document data for use in a program.

How SAX Parser works?

SAX Parser reads the input from the top to bottom. When a certain "event" occur, it invokes call back method that was provided. As a case, to remove the titles of data articles from a weblog nourish, the start tag() procedure is alluded to as and is checked if "name" component call is available or no more. all things considered, transfer its content. at the point when gotten the occasion name "end tag", investigate if it's far the last detail of "title". at that point overlook all comparably considers until both info closes or another "start tag" with a name of "name" comes nearby. As it is event based, SAX parser doesn't create any DOM tree structure in memory. SAX parser just checks for an event and call particular call back method in which whatever functionality required, can be implemented and that is how the SAX Parser operates.

shape Example

The following is an example. DemoStack.java [java]package util; import java.util.Stack; public class DemoStack { public static void main(String args[]) { // creating stack Stack st = new Stack(); // populating stack st.push("file"); st.push("Source"); st.push("code"); // checking stack System.out.println("Is stack empty: "+st.empty()); }} [/java] The goal of empty() strategy tests if this stack is void. Now compile the code then resutl will be as follows. [java]Is stack empty: false[/java] DemoStack.java [java]package util; import java.util.Stack; public class DemoStack { public static void main(String args[]) { // creating stack Stack st = new Stack(); // populating stack st.push("file"); st.push("Source"); st.push("code"); // removing top object System.out.println("Removed object is: "+st.pop()); // elements after remove System.out.println("Elements after remove: "+st); }} [/java] The pop() technique is utilized to expel the question at the highest point of this stack. when compile the code result will be as follows. [java]Removed object is: code Elements after remove: [file, Source] [/java]

Summary

shape Key Points

  • The push() technique pushes a thing onto the highest point of this stack.
  • The pop() technique removes a thing onto the highest point of this stack.