-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.

1
  • 1
    Refer to my answer here! if ("lamp".equals(DeviceType)) is what you need! Commented May 2, 2013 at 3:48

5 Answers 5

0

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

if (DeviceType .equals("lamp")) { // do something } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It has worked perfectly.
@user2341575 did you just vote and unvote?
0

When comparing Strings use equals()

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

Comments

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.

Comments

0

Try this:

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

Strings are compared using equals function and not ==.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.