Java: Comparison between Vector value and String Value

If you want to compare the String value stored  in a Vector with another  String value then its for you. I have given here a better Comparison solution to compare the value in

 a Vector with another String.
Here is the code example:
import java.util.Vector;
public class MainClass {

 public MainClass() {

 Vector vec=new Vector();

 vec.add(“Jhony”);

 String name=”Jhony”;

 /*********************Better Way***************************/

  if(vec.get(0).toString().trim().equalsIgnoreCase(name))

  {

   System.out.println(“First Try Name Match”); 

  }

  else

  {

   System.out.println(“First Try Name Not Matched”); 

  }

   /*********************End of: Better Way***************************/

 /*********************Not so Better Way***************************/

  if(vec.get(0).toString()==name)

  {

  System.out.println(“Second Try Name Match”);

  }

  else

  {

  System.out.println(“Second Try Name Not Matched”);

  }

  /****************End of : Not so Better Way**********************/

  }

public static void main(String[] args) {

MainClass x=new MainClass();

  }

}
 
 
 
 

 

One Response to Java: Comparison between Vector value and String Value

  1. Roland says:

    It is better to just use the Vector contains method

    vec.contains(name)

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: