Java Streams

JAVA Streams Sum of Elements Given a list of integers, compute the sum of all numbers. // using sum List<Integer> arr = Arrays.asList(1, 4, 5, 6, 22, 3, 90, 89, 2, 1, 3, 4, 55, 6); int sum = arr.stream() .mapToInt(Integer::intValue) .sum(); // or int sum3 = arr.stream() .reduce((a,b) -> a + b) .get(); // or int sum2 = arr.stream() .reduce(0, (total, n) -> total + n, (total1, total2) -> total1 + total2); Average of Elements Given a list of integers, compute the average of all numbers. ...

2024-07-29 · 3 min · Ramesh

Java Concurrency Multithreading

Thread Thread Subclass Here is an example of creating a Java Thread subclass: public class MyThread extends Thread { public void run(){ System.out.println("MyThread running"); } } To create and start the above thread you can do like this: MyThread myThread = new MyThread(); myTread.start(); You can also create an anonymous subclass of Thread like this: Thread thread = new Thread(){ public void run(){ System.out.println("Thread Running"); } } thread.start(); Runnable Interface Implementation Java Class Implements Runnable public class MyRunnable implements Runnable { public void run(){ System.out.println("MyRunnable running"); } } Anonymous Implementation of Runnable Runnable myRunnable = new Runnable(){ public void run(){ System.out.println("Runnable running"); } } Java Lambda Implementation of Runnable Runnable runnable = () -> { System.out.println("Lambda Runnable running"); }; Starting a Thread With a Runnable Runnable runnable = new MyRunnable(); // or an anonymous class, or lambda... Thread thread = new Thread(runnable); thread.start(); The Java Memory Model Here is a diagram illustrating the call stack and local variables stored on the thread stacks, and objects stored on the heap: ...

2024-07-23 · 6 min · Ramesh

Asynchronous Programming in Java

Asynchronous Programming in Java Parallel vs. Concurrent vs. Asynchronous Parallel: Multiple tasks executed at the same time. For example, I can walk and talk at the same time. Concurrent: I can work on multiple tasks, but I can do only one task at a moment in time. For example, I can talk or drink water at a moment. Asynchronous: It just means non-blocking. For example, I can brew a coffee, and in the meantime, I could do something else. responsive preemptive performance For Async programming JavaScript used callback. Callbacks created a problems of its own like callback hell. Promise came to solve the problem. ...

2024-07-21 · 2 min · Ramesh

Core Java

Core Java Core Java String Concepts HashCode & Equal Methods Immutability OOPS Concepts Abstraction Encapsulation Polymorphism Inheritance String Concepts A String is a sequence of characters. How to create a String object? String literal String s1="Welcome"; String s2="Welcome";//It doesn't create a new instance New keyword String s=new String("Welcome"); //creates object and reference variable Examples:- String s1 = "Hello"; char ch[] = {'H', 'e', 'l', 'l', 'o'}; String s2 = new String(ch); String s3 = "Hello"; String s4 = new String("Hello"); String s5 = new String("Hello"); System.out.println(s1 == s2); // false System.out.println(s1 == s3); // true System.out.println(s4 == s5); // false HashCode & Equal Methods Default implementations of equals() and hashcode() methods: ...

2024-05-29 · 3 min · Ramesh