devxlogo

Java

Java Collections Performance, LISTS

Java collections – performance (Time Complexity) A list is an ordered collection of elements. Add Remove Get Contains Data StructureArrayList O(1) O(n) O(1) O(n) ArrayCopyonWriteArrayList O(n) O(n) O(1) O(n) ArrayLinkedList

Java BlockingQueue

BlockingQueue in Java is more advanced Queue. It performs all functionalities of a Queue in addition to certain qualities. It waits for the Queue to be non empty before retrieving an element using take() method and holds on for a placeholder to be available before it can add an element to the Queue using the put() method. BlockingQueue is an Interface and can be used as below. We are trying to initialize an ArrayBlockingQueue with size of 5. BlockingQueue intQueue = new ArrayBlockingQueue(5); intQueue.put() will add the data and wait for a space if it is full. intQueue.take() will remove the element at the head of the Queue.

Calling Java Methods Using Lambda Expressions

To call Java methods using Lambda expressions you can use: – Car::makeCar //Static method of a class- Car::drive // Method of an instance- Car::new //Constructor of a class- Car::drive //

UUID in Java

/*A UUID is a 36 character string, 32 alphanumeric characters and 4 hypens123e4567-e89b-12d3-a456-426655440000Java supports generation and usage of this using the java.util.UUID class*/public class JavaUUID{ public static void main(String args[])

Comparison of Arrays

public class CompareArrays{ public static void main(String args[]) { compareArrays(); } private static void compareArrays() { int[] intArray1 = new int[5]; int[] intArray2 = new int[5]; //Changing the value so

Timer in Java

import java.util.*;public class TimerTaskDemo extends TimerTask{ public static void main(String args[]) { //Creating a TimerTask and a Timer that together helps achieving the required result TimerTask timeTaskDemo = new TimerTaskDemo();

Increasing the Size of an Array

public class ArraySize{ public static void main(String args[]) { changeArraySize(); } private static void changeArraySize() { int[] intArray = new int[5]; System.out.println(“Before change: size(): ” + intArray.length); // Increasing the

Understanding the Time Taken to Execute a Task

There are situations when we need to understand the time taken for executing a section of code. We are also aware that System.currentTimeMillis() provides a good estimate of time. System.nanoTime()

Variable Argument Method

The following class describes a method that can accept a variable argument instead of a fixed argument list. This can be handy where the developer is not sure of the