Java contains doesn't work as expected because “someString” != “someString”

纵饮孤独 提交于 2019-12-04 03:35:08

问题


I want check whether a String value val is contained within a List of Strings lets call it stringList.

I am doing this

if(stringList.contains(val)){
  System.out.println("The value is in there");
}
else{
  System.out.println("There's no such value here");
}

But it always seems to be that the value is not included. Is this because two String values that have the same characters are not actually equal? For a "home-made" class I could implement hashCode() and equals() and fix this, what can I do for String data?

EDIT:

The way I am getting val is outlined here:

List<String> stringList = new ArrayList<String>();
    stringList.add("PDT");
stringList.add("LDT");
stringList.add("ELNE");

String myFile = "/folder/myFile";
InputStream input = new FileInputStream(myFile);
CSVReader reader = new CSVReader(new InputStreamReader(input), ',','"', 1);
String[] nextLine;
try {
    while ((nextLine = reader.readNext()) != null) {
    if (nextLine != null) {
        if (nextLine[6] != null){
          String val = nextLine[6];
            if(stringList.contains(val)){
            System.out.println("Success");
            }
        }
    }
}

回答1:


ArrayList.contains() uses Object.equals() to check for equality (hashCode() is not involved in List). This works well for strings. Probably, your string really isn't contained in the list...

You've probably overlooked some whitespace or upper/lower-case or encoding difference...




回答2:


More code please!

This works:

import java.util.*;

public class Contains {
    public static void main(String[] args) {
        List<String> stringList = new ArrayList<String>();
        stringList.add("someString");
        String val = new String("someString");
        if (stringList.contains(val)) {
            System.out.println("The value is in there");
        } else {
            System.out.println("There's no such value here");
        }
    }
}



回答3:


That doesn’t sound right: contains uses equals rather than ==, so if the string is in the list, it should be found. This can be verified in the indexOf method of the superclass AbstractList used by ArrayList.

Following your edit, make sure you trim strings before doing contains, as otherwise they may contain the newline character(s).




回答4:


Try the following, first make the check more concrete by iterating the list and checking each element separately. Than, when you hit the elements that you are expecting to be equal, This is what you are supposed to be looking at. Check to see if they are really equal. Maybe there is a case difference? (or some other elusive but plain difference like white space?)




回答5:


Try to override equals(){}, so that you can specify which property needs to compare equality .... :P



来源:https://stackoverflow.com/questions/12552048/java-contains-doesnt-work-as-expected-because-somestring-somestring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!