Java Tutorial

                                                                      Java 


                    

                                                      

1. Lambdas and Functional Interfaces:

Java 8 introduced lambda expressions and functional interfaces, allowing you to write more concise and expressive code. Here's an example:


```java

// Functional interface with a single abstract method

@FunctionalInterface

interface Calculator {

    int calculate(int a, int b);

}


public class LambdaExample {

    public static void main(String[] args) {

        // Lambda expression using the Calculator functional interface

        Calculator addition = (a, b) -> a + b;

        System.out.println(addition.calculate(2, 3)); // Output: 5


        Calculator subtraction = (a, b) -> a - b;

        System.out.println(subtraction.calculate(5, 3)); // Output: 2

    }

}

```


2. Streams and Stream API:

Java 8 also introduced the Stream API, which provides a functional programming approach for processing collections of data. Here's an example:


```java

import java.util.Arrays;

import java.util.List;


public class StreamExample {

    public static void main(String[] args) {

        List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);


        // Use stream to filter and map elements

        List<Integer> squaredEvenNumbers = numbers.stream()

                .filter(n -> n % 2 == 0)

                .map(n -> n * n)

                .collect(Collectors.toList());


        System.out.println(squaredEvenNumbers); // Output: [4, 16]

    }

}

```


3. Multithreading and Concurrency:

Java provides robust support for multithreading and concurrency. Here's an example using threads and synchronized blocks:


```java

public class Counter {

    private int count = 0;


    public synchronized void increment() {

        count++;

    }


    public static void main(String[] args) throws InterruptedException {

        Counter counter = new Counter();


        Runnable task = () -> {

            for (int i = 0; i < 1000; i++) {

                counter.increment();

            }

        };


        Thread thread1 = new Thread(task);

        Thread thread2 = new Thread(task);


        thread1.start();

        thread2.start();


        thread1.join();

        thread2.join();


        System.out.println(counter.count); // Output: 2000

    }

}

```


4. Generics:

Generics allow you to create reusable code that can work with different types. Here's an example using a generic class and method:


```java

public class Box<T> {

    private T content;


    public void add(T item) {

        content = item;

    }


    public T get() {

        return content;

    }


    public static void main(String[] args) {

        Box<String> stringBox = new Box<>();

        stringBox.add("Hello");

        String content = stringBox.get();

        System.out.println(content); // Output: Hello


        Box<Integer> integerBox = new Box<>();

        integerBox.add(42);

        int value = integerBox.get();

        System.out.println(value); // Output: 42

    }

}

```


5. Optional Class:

The Optional class helps avoid null pointer exceptions by providing a way to represent optional values. Here's an example:


```java

import java.util.Optional;


public class OptionalExample {

    public static void main(String[] args) {

        Optional<String> optionalValue = Optional.of("Hello");


        // Check if value is present

        if (optionalValue.isPresent()) {

            String value = optionalValue.get();

            System.out.println(value); // Output: Hello

        }


        // Use


 a default value if the optional value is empty

        String defaultValue = optionalValue.orElse("Default");

        System.out.println(defaultValue); // Output: Hello


        // Transform the optional value

        Optional<Integer> transformedValue = optionalValue.map(String::length);

        System.out.println(transformedValue.orElse(0)); // Output: 5

    }

}

```


These examples cover some advanced features of Java. 

Keep exploring the vast Java ecosystem and the official documentation to deepen your knowledge and leverage the full potential of the language.

Comments

Popular posts from this blog

Data Analytics - Overview

New Energy Solutions - Overview

Docker - Overview