devxlogo

Java

Image

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

Image

Palindrome Validation Using Java

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

Image

How to Use Scanner to Receive Command Line Input

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

Image

Using valueOf() Instead of toString()

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

Image

Java Collections Performance, MAP

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)

Image

New Way to Iterate a List in Java 8

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”);

Image

Java Collections Performance, SET

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

Image

Making a File Read-only Using Java

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