Skip to main content

Program for a Non-Repeating Characters


1)Write a program for non repeating Characters?

  class Char
              {
                   public static void  main(String arsg[])
                    {
                           public static char FirstNonRepatingChar(String word)
                              {


                                Set<characters>nonrepeating = new HashSet<>();  
                                List<characters>nonrepeating = new ArrayList<>();
                                   for(int i=0; i<word.length();i++)
                                         {
                                            char letter = word.charAt(i); 
                                              if(repeating.contains(letter))
                                                      {
                                                         continue;
                                                     }
                                 if(nonrepeating.conntains(letter))   //checking character
                                    {
                                      nonrepeating.remove((character)letter); //removing character
                                      repeating.add(letter);
                                   }
                             else
                              {
                                  nonrepeating.add(letter);
                            }
                 }
    return nonrepeating.get(0);
     }
  }
}


2) Write a program to insert string object into the Treeset reverse of alphabetical order



package treeset;

import java.util.TreeSet;



public class TreeSetDemo
{
 public static void main(String args[])
 {
  TreeSet t= new TreeSet(new MyComparator());
  t.add("Rajesh");
  System.out.println(t);
 
 class MyComparator implements comparator
 {
  public int compare (Object obj1, object obj2) //comparing objects
  {
   String s1= obj1.toString();
   String s2=(String)obj2;             //assiging the values 
   return-s1.CompareTo(s2);
  }
 }

}




Comments

Popular posts from this blog

Commonly asked Top 26 Java Interview Questions

1.what is a transient variable? A transient variable is a variable that may not be serialized. If you don't want to serialize any objects then we have to use Transient Keyword. 2. Why do threads block on I/O? Threads block on i/o (that enters the waiting state) so that other threads may execute while the i/o Operation is performed. So your blocked thread of execution blocks only that: the thread of execution. 3. How are Observer and Observable used? Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects(refer 1.4). 4. What is synchronization and why is it important?                      With respect to multithreading, synchronization is the capability to control the...

Top Most Important Multithreading interview question and answers

1) What is Thread in java? Threads consumes CPU in best possible manner, hence enables multi processing. Multi threading reduces idle time of CPU which improves performance of application. Thread are light weight process. It belongs to java.lang package. It can run mutiple threads simutaneously .                                                               2) How to implements threads in java?         By   implementing java.lang.Runnable interface or extending java.lang.Thread class and then extending run method.   Thread creation by implementing java.lang.Runnable interface.      And then create Thread object by calling constructor and passing reference of Runnable interface i.e. runnable object          Thread threa...

Decode of the image file from hibernate and struts

Create a java file for the decoding the image Image.java package com.img.action; import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.imageio.ImageIO; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.img.dao.ConnectionDAO; import com.img.pojo.Film_TablePojo; import com.img.pojo.imagePojo; import com.opensymphony.xwork2.ActionSupport; public class Show extends ActionSupport { List<Object> values = new ArrayList<Object>();  public List<Object> getValues() { return values; } public void setValues(List<Object> values) { this.values = values; } /*HttpServletRequest request;     public void setServletRequest(HttpS...