Thursday, May 2, 2013

[android help] Problems with ArrayList<HashMap<String,String>> and IF-STATEMENT

java - Problems with ArrayList<HashMap<String,String>> and IF-STATEMENT - Stack Overflow







Tell me more ×

Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

















I have a Array List with HashMap



deviceDataCollection = new ArrayList>();


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.





























When comparing Strings use equals()


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























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.






















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



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





















Try this:



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


Strings are compared using equals function and not ==.






















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























lang-java






.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...