
Testing for String Equality
if (name.compareTo(“Jerry”) == 0) …if (name == “Jerry”) …if (name.equals(“Jerry”)) …if (“”.equals(name)) … All the above comparisons are correct, but they are not great. The compareTo method is overkill and

if (name.compareTo(“Jerry”) == 0) …if (name == “Jerry”) …if (name.equals(“Jerry”)) …if (“”.equals(name)) … All the above comparisons are correct, but they are not great. The compareTo method is overkill and

We are well aware of validating a Palindrome string. The emphasize here is usage of StringBuffer/Builder and the reverse method to get the string reversed instead of using an interactive

Lambda expressions have simplified the way we write code. For example, let’s see how to use a method from an interface. This works with Java 1.8 only. Expected output: Welcome

Scanner is a simple and easy-to-use utility provided by Java. import java.util.Scanner;public class ScannerDemo{ public static void main(String args[]) { Scanner scanner = new Scanner(System.in); //Initializing the Scanner with the

The valueOf() method is useful when you do not know the value contained and it might throw a NullPointerException. It returns null and you can handle the same in your code

Improve Java performance with MAP. Add Contains keys Next Data StructureHashMap O(1) O(1) O(h/n) Hash TableEnumMap O(1) O(1) O(1) ArrayLinkedHashMap O(1) O(1) O(1) Hash Table+Linked ListTreeMap O(log n) O(log n)

We can see that the method println is referred by System.out using :: import java.util.*;public class NewForEach { public static void main(String args[]) { List empNames = new ArrayList(); empNames.add(“Andrew”);

Java collections – performance (Time Complexity) A set is a collection that contains no duplicate elements. Add Next Contains Data StructureHashSet O(1) O(h/n) O(1) Hash TableEnumSet O(1) O(1) O(1) Bit

Files have multiple properties and they are interesting to use. In fact, your own file system can be developed using them. Create a file named PATH.txt and try this program