Java program to create a treeset and stack.
below code is used to create objects for treeset and stack:
import java.io.*;
import java.util.*;
import java.util.Scanner;
class StackTree
{
public static void main(String args[])
throws IOException
{
Scanner sc=new Scanner(System.in);
TreeSet ts=new TreeSet();
Stack st=new Stack();
int cnt,n;
String str=null;
System.out.println("\nenter the total no of items in treeset and stack:");
n=sc.nextInt();
for(cnt=1;cnt<=n;cnt++)
{
System.out.println("enter the "+cnt+"String:");
str=sc.next();
ts.add(str);
st.push(str);
}
System.out.println("\n\t**************TREESET***********\t");
System.out.println("\n\t The item on the tress are:\t"+ts);
System.out.println("\n\t The first item on the tree is:\t"+ts.first());
System.out.println("\n\t The last item on the tree is:\t"+ts.last());
System.out.println("\n\tThe total item on the tree is:\t"+ts.size());
System.out.println("\n\tEnter the elemsnt you want to delete:");
String del=sc.next();
ts.remove(del);
System.out.println("\n\tThe item on the tree are:\t"+ts);
System.out.println("\n\t*************STACK***************\t");
System.out.println("\n\tThe item on the stack are:\t"+st);
System.out.println("\n\top element is:"+st.peek());
System.out.println("Popped element is:"+st.pop());
System.out.println("Elements in stack after deletion:"+st);
System.out.println("enter element to search:");
String item=sc.next();
int loc=st.search(item);
if(loc>=0)
System.out.println("element found at:"+loc);
else
System.out.println("element not found:");
}
}
Thank You.