[JAVA] Difference between pop () and peek () in stack

Java.util.Stack pop () and peek () operation check memo

There was no blog about behavior confirmation, so a memo.

Below you can see what you can see on the API.

public E pop()

Removes the first object on the stack and returns that object as the value of the function Return value: The object at the top of the stack (the last item in the Vector object). Exception: EmptyStackException-if this stack is empty (https://docs.oracle.com/javase/jp/8/docs/api/java/util/Stack.html#pop--)

public E peek()

Fetch the object at the top of the stack. The object is not removed from the stack at this time. Return value: The object at the top of the stack (the last item in the Vector object). Exception: EmptyStackException-if this stack is empty (https://docs.oracle.com/javase/jp/8/docs/api/java/util/Stack.html#peek--)

For peek.java


public static void main(String[] args) {
  try {
    Stack<String> stack = new Stack();
    stack.push("Good Morning!");
    stack.push("Hello!");
    stack.peek();
    stack.stream().forEach(System.out::println);
    // Good Morning!
    // Hello!
  } catch (EmptyStackException e) {
    System.out.println("stack is empty");
  }
}

In the case of pop.java


public static void main(String[] args) {
  try {
    Stack<String> stack = new Stack();
    stack.push("Good Morning!");
    stack.push("Hello!");
    stack.pop();
    stack.stream().forEach(System.out::println);
    // Good Morning!
  } catch (EmptyStackException e) {
    System.out.println("stack is empty");
  }
}

If pop, it is pushed from the stack, and peek is not pushed from the stack. If you look at peek's API, it will return a return value, so you can pop while verifying the value.

Pop while verifying.java


public static void main(String[] args) {
  try {
    Stack<String> stack = new Stack();
    stack.push("Good Morning!");
    stack.push("Hello!");
    if (stack.peek().equals("Hello!")) {
      stack.pop();
    }
    System.out.println(stack.peek());
    // Good Morning!
  } catch (EmptyStackException e) {
    System.out.println("stack is empty");
  }
}

Recommended Posts

Difference between pop () and peek () in stack
Difference between final and Immutable in Java
Difference between getText () and getAttribute () in Selenium
Difference between "|| =" and "instance_variable_defined?" In Ruby memoization
Difference between EMPTY_ELEMENTDATA and DEFAULTCAPACITY_EMPTY_ELEMENTDATA in ArrayList
Difference between int and Integer in Java
Difference between vh and%
Difference between i ++ and ++ i
Difference between next () and nextLine () in Java Scanner
Difference between product and variant
Difference between redirect_to and render
[Java] Difference between == and equals
Rails: Difference between resources and resources
Difference between puts and print
Difference between redirect_to and render
About the difference between classes and instances in Ruby
Difference between new and create in Rais action controller
Difference between CUI and GUI
[Java] Difference between static final and final in member variables
Difference between variables and instance variables
Difference between mockito-core and mockito-all
Difference between class and instance
Difference between bundle and bundle install
Difference between ArrayList and LinkedList
Difference between render and redirect_to
Difference between List and ArrayList
Difference between .bashrc and .bash_profile
Difference between StringBuilder and StringBuffer
Difference between render and redirect_to
Difference between render and redirect_to
About the difference between "(double quotation)" and "single quotation" in Ruby
Jersey --What is Difference Between bind and bindAsContract in HK2?
Difference between element 0, null and empty string (check in list)
[Java] Difference between Stack Overflow Error and Out Of Memory Error
Is short-circuit evaluation really fast? Difference between && and & in Java
[Ruby] Difference between get and post
Difference between instance method and class method
Difference between render method and redirect_to
Difference between interface and abstract class
Difference between == operator and equals method
[Terminal] Difference between irb and pry
JavaServlet: Difference between executeQuery and executeUpdate
Difference between == operator and eqals method
Rough difference between RSpec and minitest
[Rails] Difference between find and find_by
Understand the difference between each_with_index and each.with_index
Difference between instance variable and class variable
[JAVA] Difference between abstract and interface
Difference between Thymeleaf @RestController and @Controller
Difference between Stream map and flatMap
[Java] Difference between array and ArrayList
Difference between primitive type and reference type
Difference between string.getByte () and Hex.decodeHex (string.toCharaArray ())
[Java] Difference between Closeable and AutoCloseable
[Java] Difference between StringBuffer and StringBuilder
[Java] Difference between length, length () and size ()
[rails] Difference between redirect_to and render
[Android] Difference between finish (); and return;
Difference between byCharWrapping and byWordWrapping of UI Label in Japanese display
Note: Difference between Ruby "p" and "puts"
[Memo] Difference between bundle install and update