Learn how to get a sublist of an existing ArrayList. We will be using ArrayList.subList() method to get the sublist of arraylist object. 1. ArrayList.subList() method This method returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive). 1.1. subList() Method Syntax 1.2. subList() Method Parameters fromIndex – […]
Learn to add multiple items to ArrayList. There can be two usescases which require to add multiple items to an arraylist. Add all items from a collection to arraylist Add selected items from a collection to arraylist 1. Add multiple items to arraylist – ArrayList.addAll() To add all items from another collection to arraylist, use […]
Learn to remove element from ArrayList. Remove element at specifed index, or element value. Remove all elements from arraylist for spcified value example.
Java program to replace element at specified index of arraylist using ArrayList.add(int index, E element) and ArrayList.set(int index, E element) methods.
The Java iterate through ArrayList programs. Learn how to retrieve values from ArrayList in Java using for loop, while loop, iterator and stream api.
The Java ArrayList can be initialized in number of ways depending on the requirement. Learn to initialize ArrayList based on some frequently seen usecases.
The Java String intern() return the reference of a equal string literal present in string pool. String literals are automaically interned in Java.
The Java String contains() searches a substring in the given string. It returns true if substring are found in this string otherwise returns false. 1. String contains() method Use String.contains() to find is a substring is present in given string or not. Please remember that this method is case sensitive. 1.1. Method syntax The method […]
Java String toLowerCase() returns a string which is result of converting all characters in given string to lowercase. Convert string to lowercase in Java.
The Java String toUpperCase() returns a string which is result of converting all characters in given string to UPPERCASE. Read more: Java example capitalize the string 1. String toUpperCase() method Use String.toUpperCase() converts any string to UPPECASE letters. 1.1. Method syntax String toUpperCase() method is overloaded and comes in two variants. 1.2. ‘null’ is not […]
The Java String split() returns string array after it splits the string around matches of the method argument regular expression. 1. String split() method Use String.split() to split a string to string array by tokenizing with delimiter substring or regular expression. 1.1. Method syntax String split() method is overloaded and comes in two variants. 1.2. […]
The Java String replaceAll() returns a string after it replaces each substring of that matches the given regular expression with the given replacement. 1. String replaceAll() method Use String.replaceAll(String regex, String replacement) to replace all occurrences of a substring (matching argument regex) with replacement string. 1.1. Method syntax 1.2. throws PatternSyntaxException Watch out that replaceAll() […]
The Java String replaceFirst() method replaces the first substring ‘regex’ found that matches the given argument substring (or regular expression) with the given replacement substring. The substring matching process start from beginning of the string (index 0). 1. String replaceFirst(String regex, String replacement) method The String replaceFirst() method uses regular expression to find and replace […]
The Java String replace() method replaces each substring of this string that matches the literal target substring. The matching process starts at index 0.
The Java String concat() method concatenates the method argument string to the end of string object. 1. String concat(String str) method Internally, Java creates a new character array with combined length of string object and argument string, and copies all content from both strings into this new array. Finally, combiner character array is converted to […]
Java String substring() example. Learn to get substring of a string from given start index to end index. A valid index is between 0 and length of string.
Java String lastIndexOf() method. It returns the last index of specified character or string as method argument. If argument is not found it returns -1.
The Java String indexOf() method returns the index of given character or string as method argument. If argument is not found in string, method returns -1. The index counter for a string starts from zero.
Java String hashCode() method returns the hash code for the String. Hash code value is used in hashing based collections like HashMap, HashTable etc. This method must be overridden in every class which overrides equals() method. Read More: Contract between hashCode() and equals() methods 1. String hashCode() method The hash code for a String object […]
Java String endsWith() method is used to check the suffix of the string. It verifies if given string ends with argument string or not. 1. Java String endsWith(String str) method Java program to check if a string ends with suffix argument string ‘str’. Program output. String endsWith() method does not accept regular expression as argument. […]