0

i have a small of a problem. I have to String Lists with the same size and a string to compare. meaning i want to compare the string with the first list to get the index of the compared string in the list and then get another string from the other list on this index.

private String getStringOnIndex(List<String> list1, List<String> list2,String elem) 
{
                String elem2;

        for (int i = 0; i<list1.size();i++) {

            if(list1.get(i).equals(elem)){
                elem2 = list2.get(i);
                return elem2;

            } else {
                return "nope";
            }
        }

}

Is it wrong to compare two strings like that. or should i use the operator ==. I know the style with to string lists is not nice but its just a temporary solution. thx for any help :)

vicR
  • 789
  • 4
  • 23
  • 52
  • Also, be aware that the line: list1.get(i).equals(elem) can cause NullPointerException – Jarle Hansen Apr 05 '13 at 07:44
  • 2
    Not answering the question, but can't you simply do: - `list2.get(list1.indexOf(elem));` if I understand that this is what you need? Of course, you need to do the test beforehand, that `list1.contains(elem);` – Rohit Jain Apr 05 '13 at 07:44
  • @RohitJain: Plus some checks for "not found" of course. – A.H. Apr 05 '13 at 07:45
  • You have a bad loop condition, see my answer.... – Waji Apr 05 '13 at 10:03

7 Answers7

10

To answer your specific question on "string comparison", what you are doing is correct. String content comparison should be done using equals(). Using operator == is only checking for the equality of the reference, not the content.

For the work you are doing now, it looks like a key-value lookup to me. You may consider some redesign and, instead of storing 2 lists, make a Map<String, String>

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
1

== operator in Java compares the Object References, to compare strings you should use equals().

A.H.
  • 63,967
  • 15
  • 92
  • 126
fardjad
  • 20,031
  • 6
  • 53
  • 68
1
if (list1.get(i).matches(elem))....
drew moore
  • 31,565
  • 17
  • 75
  • 112
1

always use equals method to compare two string. If you are comparing references then use == operator. Here discussion can be useful How do I compare strings in Java?

Community
  • 1
  • 1
AmitG
  • 10,365
  • 5
  • 31
  • 52
0

Use the equals() method to compare the value of the object,where as == operator will compare object reference.

commit
  • 4,777
  • 15
  • 43
  • 70
Vasu
  • 149
  • 7
0

== operator work fine only if the String variable is not instanciated new keyword. Suppose:

String s1 = "abc";
String s2 = "abc";

Then s1==s2 works good.

If:

String s1 = new String("abc");
String s2 = new String("abc");

Then you must use equals method to compare the values.

Finally, it is always better to use equals method to compare the String values.

G.S
  • 10,413
  • 7
  • 36
  • 52
0

The problem is not equals (that is the correct method), you must remove else expression, if not you will stop always at first iteration, solution:

private String getStringOnIndex(List<String> list1, List<String> list2,String elem) 
{
    String elem2;

    for (int i = 0; i<list1.size();i++) {
        if(list1.get(i).equals(elem)){
            elem2 = list2.get(i);
            return elem2;
        }
    }
    return "nope";

}
Waji
  • 71
  • 3