Java String contains()

Java String contains() method to check if a String contains a specified character sequence. This method returns a boolean datatype which is a result in testing if the String contains the characters specified on the method argument in CharSequence object type. The contains() method returns true if and only if this string contains the specified sequence of char values. Syntax:
public boolean contains(CharSequence sequence)
Example
class TestClass{ public static void main (String[] args){ String str = "twinkle twinkle little star"; boolean got = str.contains("little"); System.out.println("String contains 'little' : " + got); got = str.contains("java"); System.out.println("String contains 'java' : " + got); } }
Output:
String contains 'little' : true String contains 'java' : false

String hashCode()

In the Java programming language, every class implicitly or explicitly provides a hashCode() method, which digests the data stored in an instance of the class into a single hash value (a 32-bit signed integer). The String hashCode value of a Java String is computed as: escape characters Advantages::
  1. It is fast, to the extent that it probably produces hashes as the CPU can read the String from memory (i.e. you usually can't get better without skipping large parts of the String). It does just one multiply and one add per character in the String.
  2. For typical sets of random Strings, it produces well-distributed hashes over the entire int range.
It is important to note that it is not a cryptographic hash function, its only obligation is to obey the contract defined by java.lang.Object , So don't use it for that. Moreover, be aware that you likely will get hash collisions as it is producing a 32-bit hash. So you just need to design your algorithms to take that into account. Example
class TestClass{ public static void main (String[] args){ String str = new String("Java String Tutorial"); System.out.println("Hashcode is :" + str.hashCode() ); } }
Output:
Hashcode is :-188391249