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
}
}
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
}
}
Subscribe to:
Comments (Atom)
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...
-
Interface: only know the customer requirement and don't know the implementation than go for interface. Advantage: 100% abstrac...
-
Now a days in interview interviewer always ask questions related to Collection and map And they also ask why map interface is not child of...
-
If you want to share or update the same data then the activity must be synchronized to avoid the error.in java using synchron...