Java contains doesn't work as expected because "someString" != "someString" - java

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");
}
}
}
}

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

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");
}
}
}

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

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?)

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

Related

How to use java empty HashSet in if-statement?

public static void main(String[] args) {
Map<String, HashSet<String>> test = new HashMap<String, HashSet<String>>();
test.put("1", new HashSet<String>());
System.out.println(test);
System.out.println(test.get("1"));
if(test.get("1") == null){
System.out.println("Hello world");
}
}
The first println gets me {1=[]}
The second one gets me []
I am trying to print out "Hello world" but the if statement isn't going through.
Is the empty HashSet, [] not equal to null?
How do I use the empty HashSet in this if statement?
There is a difference between null, which means "nothing at all," and an empty HashSet. An empty HashSet is an actual HashSet, but one that just coincidentally happens to not have any elements in it. This is similar to how null is not the same as the empty string "", which is a string that has no characters in it.
To check if the HashSet is empty, use the isEmpty method:
if(test.get("1").isEmpty()){
System.out.println("Hello world");
}
Hope this helps!
Is the empty HashSet, [] not equal to null?
Correct, it is not. This is precisely the reason your code behaves the way it does.
To check for both null and empty set, use the following construct:
HashSet<String> set = test.get("1");
if (set == null || set.isEmpty()) {
...
}
The empty HashSet isn't a null. Add a test by using the HashSet.size()
if (test.get("1") == null || test.get("1").size() == 0) {
or use HashSet.isEmpty(),
if (test.get("1") == null || test.get("1").isEmpty()) {
Alternatively, you could comment out
// test.put("1", new HashSet<String>());
System.out.println(test);
Then test.get("1") is null.
You should use the Set_Obj.isEmpty() method. This returns a boolean value checking if the set has any element in it (true).

Can I compare against multiple strings with the equals() method?

Is it possible to get multiple strings with .equals?
if(something.equals("String1 String2 String3")){
System.out.println(Something);
}
What I mean is:
if(choose.equals("DO IT")){
sysout blah blah blah
}
else if(choose.equals("DONT DO IT")){
...
}
No, but an alternative for many strings is to put the strings in a collection and do something like:
Set<String> strings = new HashSet<>();
strings.add("A");
strings.add("B");
strings.add("C");
if (strings.contains("D")) {
// ...
}
which is perhaps a little more concise. It's also null-safe wrt. the string you're looking to compare, which is often very useful.
Note further with Java 7 the switch statement works with strings, and that's useful if you wish to tie different actions to different strings.
If something is "String1 String2 String3" then it is equal.
If you mean contains, you can do
List<String> valid = Arrays.asList(string1, string2, string3);
if (valid.contains(something))
No you cannot. equals() takes only one object at a time.
As an alternative, you can try something like
if(something.equals("String1") || something.equals("String2") ||
something.equals("String3")) {
System.out.println(Something);
}
If you mean "can I test a string being equal to several strings in one operation", use regex:
if (something.matches("String1|String2|String3")) {
System.out.println(Something);
}
The pipe char | means "OR" in regex.
Note that in java (unlike many other languages) matches() must match the whole string - ie this is an "equals" comparison, not a "contains" comparison.
You can use a regex given the strings you match don't contain special regex characters, or are escaped.
Example:
Pattern p = Pattern.compile("^(String1|String2|String3)$");
if(p.matcher(something).find()) {
//do something
}
Or you can store the strings in a set/list and query the set:
Example:
HashSet<String> possible = new HashSet<String>();
possible.add("String1");
possible.add("String2");
possible.add("String3");
if(possible.contains(Something)) {
//do something
}
No, but you can use || to test multiple strings for equality:
if(something.equals("String1") || something.equals("String2") || something.equals("String3"))){
System.out.println(Something);
}
If you have gone through the javadocs it says
public boolean equals(Object obj); :
Indicates whether some other object is "equal to" this one.
It does not says that some other object is "equal to" these Objects.
Using equals() you can compare an Object with some other Object. It does not allow you to compare at once an Object with many other Objects. However if you want to compare an Object with many other Objects then you will need equals() for each comparasion
Well, if you want to check if there are any in such a string that don't match (aka all must match, albeit that doesn't really seem to make sense to me), then
String initString = "String1 String2 String3";
String[] splitStrings = initString.split(" ");
boolean match = true;
for(String string : splitStrings)
{
if(!string.equals(something))
{
match = false;
break;
}
}
if(match == true)
{
//did match all of them
}
else
{
//there was one that was not matched
}
If you want a "matches at least one" then it's just
String initString = "String1 String2 String3";
String[] splitStrings = initString.split(" ");
boolean match = false;
for(String string : splitStrings)
{
if(string.equals(something))
{
match = true;
break;
}
}
if(match == true)
{
//did match at least one of them
}
else
{
//didn't match any of them
}
But to be honest, Java 8 makes this simpler:
String something = "whatever";
String initString = "String1 String2 String3";
String[] splitStrings = initString.split(" ");
boolean matchAll = Arrays.stream(splitStrings).allMatch((x) -> x.equals(something));
boolean matchAny = Arrays.stream(splitStrings).anyMatch((x) -> x.equals(something));

List to String using toString() in java

I'm having ArrayList Contains of String. I would like to check whether the character is present in the arraylist. I'm using the following code.
if(list.toString.contains(char))
{
// enter code here
}
Can i use this toString() method. What is the drawback?
It would be a really bad idea to use List.toString() and search that. Your code should probably look something like this :
Iterator it = list.getIterator();
char searchChar = 'S';
while (it.hasNext())
{
String s = (String) it.next();
if ( s.contains(searchChar) )
{
//Found the char!
}
}
No you cannot go ahead with arraylist.toString(), as it will not provide string representation of contents in String.
Better approach is to iterate over list and check, as below.
for(String detail:listString){
if(detail.contains('X')) //replace 'X' with your character
{
// do somethng
}
}
Try this,
Arrays.toString(inputList.toArray()).contains(searchValue);
list.toString() gives you a string representation of a list and thus it contains more characters then just the concatenated list elements
[stringElement1, stringElement2, ... ]
Therefore your approach will not work if the character you are looking for is , , , [ or ].
And keep in mind that this string representation is implementation specific. It might not work for other list implementations than ArrayList
I would recommend to write a method linke this:
private boolean listElementContains(List<String> list, String subString){
for(String element : list){
if(element.contains(subString)){
return true;
}
}
return false;
}
You can call toString() on any Java Object. List is an Object which contains (you guessed it) a list of other Objects. Therefore, you can also call toString() on each Object contained within the List. You should read about inheritance in Java.
In your particular case, you have a List of Strings. What you actually want to do is check each String in the List to see if the String contains a particular character. Topics you may want to read about include iteration, for loops, and for each loops.
If I understand this correctly, your code would look like this:
List<String> strings = new ArrayList<>();
//add strings to list
for (String string : strings) {
//Look for some character c
if (string.indexOf(c) >= 0) {
return true;
}
}
return false;
On the matter of list.toString, that simply returns a representation of the object as a string; it has nothing to do with the contents. Think of it like a label on a box of stuff that says "Junk." The box is labeled Junk, but you have no idea what's in it.
What's nearly certain is that toString will return a nonsense label for the object in memory. So to get at what's inside, you need to loop through the contents as shown above.
if(list.toString.contains(char))
String's contains() method won't take char as param, instead check with indexOf
Your code works, with little modifications.
A small example here:
List<String> list= new ArrayList<>();
list.add("test");
list.add("test2");
if (list.toString().indexOf('t') > -1) // True
{
System.out.println("yes there");
}
Note:
As a workaround, Make an char array and add your char in to that array and then use contains method.

Null pointer exception while checking if a string is present in a string array in android?

I have an application which requires to check if a String is present in an array of String type, before adding it so as to avoid duplication. To do this, I wrote the following function:
public boolean arrayHas(String[] arr, String str)
{
for(int i=0;i<arr.length;i++)
{
if(arr[i].equals(str))
return true;
}
return false;
}
To invoke this function, I'm using:
if(!arrayHas(contacts,str))
{
contacts[i] = str;
i++;
}
contacts and str are declared as follows
public static String contacts[] = new String[]{};
String str = "";
Bundle bun = getIntent().getExtras();
str = bun.getString("key");
Elements are added to 'contacts' only through the main code, it is empty at the beginning. I tried adding a toast to display the value of 'str' received through the intent and it works fine. But I'm getting a NullPointerException in the 'if' statement in the arrayHas function. Could someone help me out?
Seems that you haven't initialized the array with elements. So all of them are NULL.
In you arrayHas function check if the element you are comparing with is a null or not.
if(arr[i] != null && arr[i].equals(str) )
{
// do your operation
}
Also before calling arrayHas function in
if(arrayHas(contacts,str)) { }
put a check if contacts is null or not.
Two issues:
First: add a null check in if as:
if(arr[i] != null && arr[i].equals(str))
because that position may not have assigned with a valid string yet e.g. in the very beginning, no assignment is made and arr[0] is null so comparison will result into NullPointerException.
Second: I think you want to check the not ie. ! condition in this check:
if(!arrayHas(contacts,str))
{
contacts[i] = str;
i++;
}
If you want to avoid duplication, use a java.util.Set<String>, it will take care of it for you. You can always uso toArray() if you really need an array later on.
If you care about the order of your elements, you can also use a java.util.List, and check the presence of the element with list.contains(str)
Use this instead :
String contacts[] = new String[10];
String str = "somethiung";
if(Arrays.asList(contacts).contains(str))
{
contacts[i] = str;
i++;
}
Arrays.asList(.).contains(.) gives you a much better way to test if a string is present in an array.
By the way make sure that contacts and str are properly initialized.
Just a suggestion to code style. Try to defensive programming., ie. you are checking whether the string str is present in arr in that case always do the check in reverse ie., str.equals(arr[i]), so that unnecessary NPEs wont be raised.
In this case an NPE could be raised at 2 points, if arr is null .length and .equals will throw NPE's. From this its evident that, either arr is null , or arr[i] is null.
Find the method where arr is filled with data, there something is going wrong.
thanks
You must not have initialized your contacts[] but it might be the case like str[0]=null but str[1]="something";
in that case change
arr[i].equals(str) to `str.equals(arr[i])` as str is less likely to be null
- I think you are trying to find whether a String is a present in the Array of Not.
- First use Collections cause that will be much more flexible in comparision to Array.
- Use Collections.contains() method to find the String if present of not.
For example if you are using List:
List<String> list = new ArrayList<String>();
list.contains("string_to_be_found");

replace null or empty data in string builder with some message

I have a string builder and it contains some data.I want to ensure whenever there is a null or empty("") data in it, i want to replace it with some message e.g not available. This is a huge data and i cannot go and replace each and every String.
Following is a snippet of code :
Stringbuilder sb = new Stringbuilder();
String a = "10";
String b = 13;
sb.append("entity.id=").append(a).append("entity.value=").append(b);
sb.toString;
So whenever entity.id or entity."ANYTHING" equals "null" or is empty, it should be replaced with a message like entity.id= not available
Without knowing your exact requirements, this may not be perfect, but is one option:
public void myCustomAppend(Appendable a, CharSequence cs){
if(cs == null || cs.length() == 0){
a.append("(not available)");
}else{
a.append(cs);
}
}
myCustomAppend(sb, "entity.id=");
myCustomAppend(sb, a);
myCustomAppend(sb, "entity.value=");
myCustomAppend(sb, b);
Some improvements to this could include creating it as a custom object with it's own state - preventing the need to keep passing-in the same reference to the StringBuilder, as well as allowing for successive calls to be chained (as you had them in the original question).
Create a function, for example...
public String checkString(String str) {
if(str == null || str.isEmpty())
return "N/A";
return str;
}
Call it on a String you want to append.
sb.append("entity.id=").append(checkString(a)).append("entity.value=").append(checkString(b));
Consider using Guava's Joiner class.
List<String> values = newArrayList("value1", "value2", a, b, c);
String result = Joinger.on(" ").useForNull("null").join(values);
You can also use "omitNull" instead of "useForNull"
Seeing what you are doing, you might also want to look at MoreObjects.ToStringHelper
Guava docs
check the strings if they are empty, before you append it. If the string is empty you can append a your message instead of the content of the data strings. ;)

Categories