2

I don’t know what is better to use , while I need to return boolean, searching for a match , using : contains or equals. Here are some examples.

List<String> names = new ArrayList<>();
names.add("John");
names.add("Laura");
names.add("Nick");

Equals :

boolean check;
for(String name : names)
{
 if("Laura".equals(name))
    { 
  check = true;
    }
}

Contains :

boolean check;
check = names.contains("Laura");
  1. What is actual difference ?
  2. What worth using ?

Thx for response in advance

vs97
  • 5,765
  • 3
  • 28
  • 41
Gipsy King
  • 186
  • 2
  • 2
  • 14
  • 2
    You should make sure you use straight quotes `"` in your code, not weird quotes `“”` that will not be accepted by the compiler. – khelwood Feb 24 '19 at 19:06
  • For your use case, provided that the second and shorter version gives you what you want, prefer it. Both snippets will get you the same result, the first is just a little more cumbersome. – Ole V.V. Feb 24 '19 at 19:08
  • difference: you probably didn't know how `contains` work! worth using: second one if you want to know if the list contains the given name (easier to read/understand) – user85421 Feb 24 '19 at 19:11
  • @khelwood sorry I’m using my phone to post questions, so the only quotes is “ that I can use – Gipsy King Feb 24 '19 at 20:58

2 Answers2

6

The String equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true. For your scenario, you would look through every String in your List, compare it with the String you want and return true if they are the same.

The ArrayList.contains(Object) method returns true if this List contains the specified element. The actual implementation of the contains method in java.util.ArrayList is the following:

/**
* Returns true iff element is in this ArrayList.
*
* @param e the element whose inclusion in the List is being tested
* @return true if the list contains e
*/
public boolean contains(Object e)
{
   return indexOf(e) != -1;
}

The indexOf() method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.

Whatever approach you decide to take, either is acceptable. In specifically your case, since you have a hard-coded value that you are checking, in my opinion it would look neater to use the contains method, but that is not a single correct answer.

vs97
  • 5,765
  • 3
  • 28
  • 41
  • 1
    Interesting. It reminds us that contains() is actually faster than the first method described, as contains() short-circuits when it finds the element searched, while the first method doesn't. – kumesana Feb 24 '19 at 19:36
0

In Java, the String equals() method compares the two given strings based on the data/content of the string. If all the contents of both the strings are the same then it returns true. If all characters are not matched then it returns false.

contains method -> returns true if the substring is present in the main string

For ex:

    String a = "Jarvis123";
    String b = "Jarvis";

    System.out.println(a.contains(b));  // will return true
    System.out.println(a.equals(b));    // will return false
Rithesh B
  • 198
  • 1
  • 9