-3

I have a Array List with HashMap

deviceDataCollection = new ArrayList<HashMap<String,String>>();

I have to retrieve some data from this Array e compare using an IF-STATMENT, like the code below:

String DeviceType = deviceDataCollection.get(position).get("var").toString();
//it returns a string: lamp 

if (DeviceType == "lamp")
{
       // do something
}

The problem is: I can't get enter the IF-STATEMENT. I'm sure the values are exact the same.

I don't know what to do.

Thanks.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    Refer to my [answer](http://stackoverflow.com/a/16329952/2024761) here! `if ("lamp".equals(DeviceType))` is what you need! – Rahul May 02 '13 at 03:48

5 Answers5

0

When comparing Strings use equals()

So in your conditional it should be DeviceType.equals("lamp")

shyamal
  • 826
  • 10
  • 16
0

When you compare reference types in Java (and Android) you need to use the .equals() method:

if (DeviceType.equals("lamp"))
{
       // do something
}

== determines equality by when something is in memory, .equals() will consider its value.

Hunter McMillen
  • 59,865
  • 24
  • 119
  • 170
0

Use equals("lamp") or equalsIgnoreCase("lamp") method to co0mpare two Strings.

if (DeviceType .equals("lamp"))
 {
   // do something
  }
Deepak
  • 327
  • 1
  • 7
0

Use

    if ("lamp".equals(DeviceType))
    {
             //do something
    }

The operator, ==, tests to see if two object reference variables refer to the exact same instance of an object.

The method, .equals(), tests to see if the two objects being compared to each other are equivalent -- but they need not be the exact same instance of the same object.

http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

Check the equals method in the link above

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Try this:

if (DeviceType.equals("lamp"))
{
   // do something
}

Strings are compared using equals function and not ==.

Mihir Shah
  • 996
  • 10
  • 19