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
}
}
No comments:
Post a Comment