import java.util.Stack;
import java.util.Collections;
import java.util.Comparator;
import static java.lang.System.out;
class SearchList
{
public static void main(String...alt)
{
Stack v=new Stack();
v.push(new StringBuffer("ravi"));
v.push(new StringBuffer("altaf"));
v.push(new StringBuffer("kanta"));
v.push(new StringBuffer("zahid"));
v.push(new StringBuffer("nani"));
out.println("Before Sorting : "+v);
Collections.sort(v,new MyComparator());
out.println("After Sorting : "+v);
int result1=Collections.binarySearch(v,"nani",new MyComparator());
int result2=Collections.binarySearch(v,"NANI",new MyComparator());
out.println("nani is available in index : "+result1);
out.println("NANI is not available in Vector So it insertion point is : "+result2);
//NANI is less than nani(ASCII value of upper case letter is less than ASCII value of lower case letter(but sorting is performed in Descending order)
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=o1.toString();
String s2=o2.toString();
return s2.compareTo(s1); //Descending order
}
}
import java.util.Collections;
import java.util.Comparator;
import static java.lang.System.out;
class SearchList
{
public static void main(String...alt)
{
Stack v=new Stack();
v.push(new StringBuffer("ravi"));
v.push(new StringBuffer("altaf"));
v.push(new StringBuffer("kanta"));
v.push(new StringBuffer("zahid"));
v.push(new StringBuffer("nani"));
out.println("Before Sorting : "+v);
Collections.sort(v,new MyComparator());
out.println("After Sorting : "+v);
int result1=Collections.binarySearch(v,"nani",new MyComparator());
int result2=Collections.binarySearch(v,"NANI",new MyComparator());
out.println("nani is available in index : "+result1);
out.println("NANI is not available in Vector So it insertion point is : "+result2);
//NANI is less than nani(ASCII value of upper case letter is less than ASCII value of lower case letter(but sorting is performed in Descending order)
}
}
class MyComparator implements Comparator
{
public int compare(Object o1,Object o2)
{
String s1=o1.toString();
String s2=o2.toString();
return s2.compareTo(s1); //Descending order
}
}
0 Comment to "Java Program to push StringBuffer object elements in Stack(List) and search an element."
Post a Comment