Wednesday, 7 March 2018

How to find Min and Max value from stack

Its very simple.

1. Sort the value in stack using Collections.sort()
2.then find the min by using Collections.min()
3.find the max bu using Collections.max() method


package ListExample;

import java.util.Collections;
import java.util.Stack;

public class StackExample {

public static void main(String[] args) {
Stack stack = new Stack();

stack.push(12);
stack.push(12);
stack.push(9);
stack.push(25);
stack.push(55);
stack.push(65);
System.out.println(stack);//printing without sorting
Collections.sort(stack);//Sorting the stack

System.out.println("Max number is "+Collections.max(stack));//Printing Max number in stack

System.out.println("Min number is "+Collections.min(stack));//Return Minimum number in stack


}

}


Output:

[12, 12, 9, 25, 55, 65]
Max number is 65
Max number is 9

No comments:

Post a Comment

session-per-request in hibernate

The most common pattern in a multi-user client/server application is session-per-request . In this model, a request from the client is sent...