Java String contains()

The Java String contains() method allows you to determine whether a specific character sequence is present within a given string. It returns a boolean value, indicating whether or not the specified sequence of characters is found within the string.

When using the contains() method, you provide a character sequence as an argument, which is typically represented as a CharSequence object. The method then evaluates the string and determines if it contains the exact sequence of characters specified in the argument. If the sequence is found, the method returns true; otherwise, it returns false.

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

By utilizing the contains() method, you can easily check if a string contains a specific substring or character sequence. This can be useful in various scenarios, such as searching for keywords or patterns within text, validating user input, or performing conditional operations based on the presence or absence of certain characters or substrings within a string.

String hashCode()

The hashCode() method is available in every class and is responsible for generating a unique hash value for an instance of the class. The hashCode() method for the String class specifically computes the hash code value for a given string.

The hash code value of a Java String is calculated by considering the contents of the string. The calculation takes into account each character in the string and assigns a numerical value to it. The hash code value is computed using an algorithm that ensures a balance between uniqueness and efficiency.

The computed hash code value for a string is a 32-bit signed integer, which means it can have both positive and negative values. The hash code value is primarily used for efficient storage and retrieval of objects in data structures such as hash tables.

It's important to note that the hash code value of a string can vary depending on the specific characters and their order within the string. Different strings will typically have different hash code values, but there is a possibility of hash code collisions, where different strings produce the same hash code value. However, modern hash code algorithms strive to minimize the likelihood of collisions to maintain the integrity of hash-based data structures.

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

It is essential to understand that the hashCode() method provided by Java's String class is not intended to be a cryptographic hash function. Its purpose is to fulfill the contract defined by the java.lang.Object class, which requires generating a hash value that facilitates efficient storage and retrieval of objects in data structures like hash tables.

The hashCode() method in Java's String class operates on a 32-bit hash value, which means that there is a limited number of possible hash codes. Consequently, there is a possibility of hash collisions, where different strings yield the same hash code. This is a natural consequence of mapping a potentially infinite set of strings to a finite set of hash code values.

Conclusion

Due to the likelihood of hash collisions, it is crucial to design algorithms that can handle such situations appropriately. This can involve strategies like using additional equality checks to confirm string equivalence or employing alternative data structures to mitigate the impact of collisions.