Java 8

Q: What are the new features of java 8 ?

Ans:

1. Stream API:
Question: Write a Java 8 function that takes a list of integers and returns a list of even integers sorted in descending order.
Follow-Up: How would you modify the function to return only the first 3 even integers?
Example Solution:

import java.util.List;
import java.util.stream.Collectors;

public class Example {
    public List<Integer> getEvenNumbersDescending(List<Integer> numbers) {
        return numbers.stream()
                      .filter(n -> n % 2 == 0)
                      .sorted((a, b) -> b - a)  // Sorting in descending order
                      .collect(Collectors.toList());
    }
    
    public List<Integer> getFirstThreeEvenNumbersDescending(List<Integer> numbers) {
        return numbers.stream()
                      .filter(n -> n % 2 == 0)
                      .sorted((a, b) -> b - a)  // Sorting in descending order
                      .limit(3)                 // Limiting to the first 3 results
                      .collect(Collectors.toList());
    }
}

2. Functional Interfaces:
Question: Create a custom functional interface that takes two integers and returns their sum. Then, use it in a lambda expression.
Example Solution:

@FunctionalInterface
interface Adder {
    int add(int a, int b);
}

public class Example {
    public static void main(String[] args) {
        Adder adder = (a, b) -> a + b;
        System.out.println("Sum: " + adder.add(5, 3));  // Output: Sum: 8
    }
}

3. Optional:
Question: Write a method that finds the first string in a list that starts with the letter "A". If no such string is found, return "Not found" using Optional.
Example Solution:

import java.util.List;
import java.util.Optional;

public class Example {
    public String findFirstStringStartingWithA(List<String> strings) {
        return strings.stream()
                      .filter(s -> s.startsWith("A"))
                      .findFirst()
                      .orElse("Not found");
    }
}

4. Map and Reduce:
Question: Given a list of integers, write a method to find the product of all integers using Java 8 streams.
Example Solution:

import java.util.List;

public class Example {
    public int findProduct(List<Integer> numbers) {
        return numbers.stream()
                      .reduce(1, (a, b) -> a * b);  // Multiply all elements
    }
}

5. Default and Static Methods in Interfaces:
Question: Create an interface with a default method that returns a greeting message. Then, implement this interface in a class and demonstrate the usage of the default method.
Example Solution:

interface Greeter {
    default String greet() {
        return "Hello!";
    }
}

public class Example implements Greeter {
    public static void main(String[] args) {
        Example example = new Example();
        System.out.println(example.greet());  // Output: Hello!
    }
}

6. Lambda Expressions and Method References:
Question: Given a list of strings, write a method that converts all strings to uppercase using method references.
Example Solution:

import java.util.List;
import java.util.stream.Collectors;

public class Example {
    public List<String> convertToUpper(List<String> strings) {
        return strings.stream()
                      .map(String::toUpperCase)  // Using method reference
                      .collect(Collectors.toList());
    }
}

7. Parallel Streams:
Question: What are parallel streams, and how would you use them to find the sum of a large list of integers?
Example Answer: Parallel streams are a feature in Java 8 that allow you to process data in parallel, potentially improving performance on large datasets by utilizing multiple CPU cores. However, they should be used carefully due to possible thread-safety issues.

Example Solution:

import java.util.List;

public class Example {
    public int sumWithParallelStream(List<Integer> numbers) {
        return numbers.parallelStream()
                      .mapToInt(Integer::intValue)
                      .sum();
    }
}

8. Collectors Grouping:
Question: Write a method that groups a list of strings by their length using Collectors.groupingBy().
Example Solution:

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class Example {
    public Map<Integer, List<String>> groupByLength(List<String> strings) {
        return strings.stream()
                      .collect(Collectors.groupingBy(String::length));
    }
}

 Q: What distinguishes a collection from a stream?

A Collection contains its elements, whereas a Stream does not, and this is the primary distinction between the two types of data structures. 

Unlike other views, Stream operates on a view whose elements are kept in a collection or array, but any changes made to Stream do not affect the original collection.

 

Q: What is the function map() used for? You use it, why?

In Java, functional map operations are performed using the map() function. This indicates that it can apply a function to change one type of object into another.

Use map(), for instance, to change a List of String into a List of Integers if you have one already.

It will apply to all elements of the List and give you a List of Integer if you only supply a function to convert String to Integer, such as parseInt() or map(). The map can change one object into another.


Q: What distinguishes a Java functional interface from a conventional interface?

Ans: While the functional interface in Java can only contain one abstract method, the normal interface can contain any number of abstract methods.

They wrap a function in an interface, which is why they are termed functional interfaces. The one abstract method on the interface serves as the function's representation.


Q: Print odd/even numbers from Array and List with java Stream

Ans:

public static void main(String[] args) 
    {
        List<Integer> numbers = Arrays.asList(1, 4, 8, 40, 11, 22, 33, 99);

       List<Integer> oddNumbers = numbers.stream().filter(o -> o % 2 != 0).collect(Collectors.toList());

        List<Integer> evenNumbers = numbers.stream().filter(o -> o % 2 = 0).collect(Collectors.toList());

        System.out.println(oddNumbers);

        System.out.println(evenNumbers);

    }


Q: How to use stream in Map ?

Ans:
Stream of Keys
If you're only interested in the keys, you can create a stream from the keySet() of the Map.

map.keySet().stream().forEach(key -> System.out.println("Key: " + key));
Stream of Values
Similarly, you can create a stream from the values() of the Map if you're only interested in the values.

map.values().stream().forEach(value -> System.out.println("Value: " + value));



Q: Print odd/even numbers from a Set with java Stream

Ans:
    public static void main(String[] args) 
    {
        Set<Integer> oddNumbers = numbers.stream().filter(o -> o % 2 != 0).collect(Collectors.toSet());
        System.out.println(oddNumbers);
    }

Q: What does the Java 8 StringJoiner Class mean? How can we use the StringJoiner Class to join several Strings?

StringJoiner is a new class that was added to the java.util package in Java 8. With the help of this class, we may combine numerous strings that have been separated by delimiters and add prefixes and suffixes to them.

Using the StringJoiner Class, we will learn how to join several Strings in the program below. The delimiter between the two strings in this instance is ",". After that, we combined five distinct strings by adding them together using the add() method. The String Joiner was then printed.

import java.util.StringJoiner;

 public class Java8 {
    public static void main(String[] args) {
 
        StringJoiner stj = new StringJoiner(" ");
        // Separated the elements with a space in between.
         
        stj.add("Saket");
        stj.add("John");
        stj.add("Franklin");
        stj.add("Ricky");
        stj.add("Trevor");
         
        // Added elements into StringJoiner “stj”
         
        System.out.println(stj);  o/p - Saket John Franklin Ricky Trevor
    }
}


Q: How does a lambda expression relate to a functional interface? What is a lambda expression in Java?

A function type without a name is a lambda expression. Results and parameters may or may not be present. Since it lacks type information, it is referred to as an anonymous function. It is carried out as needed. It helps with data extraction, filtering, and iteration from collections.

Lambda expressions can only be used with the single abstract method of the Functional Interface since they are equivalent to anonymous functions. From the signature of the abstract method of the functional interface, it will deduce the return type, type, and several parameters.

Q: Write some sample program to use stream functions

Ans:

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static jdk.nashorn.internal.objects.NativeArray.forEach;

public class Java8Demo {

    @Test
    public void removespecialchar() {

        String str = "t.,/r}-y";
        str = str.replaceAll("[^a-zA-Z0-9]", "");
        System.out.println(str);
    }
======================================================

    @Test
    public void oddeven() {

        List<Integer> numbers = Arrays.asList(1, 4, 8, 40, 11, 22, 33, 99);

        List<Integer> evennumbers =numbers.stream().filter(n->n%2==0).collect(Collectors.toList());
        numbers.stream().filter(n->n%2==0).forEach(System.out::println);
        System.out.println(evennumbers);
    }
======================================================
    @Test
    public void flatmap() {

        // Creating a List of Lists
        List<List<String>> listOfLists = Arrays.asList(
                Arrays.asList("A", "B"),
                Arrays.asList("C", "D"),
                Arrays.asList("E", "F")
        );

        // Using Stream flatMap(Function mapper)
        listOfLists.stream().flatMap(list -> list.stream()).forEach(System.out::println);
    }
======================================================

    @Test
    public void flatmap2()
    {
        // Creating a List of Strings
        List<String> list = Arrays.asList("Geeks", "GFG", "GeeksforGeeks", "gfg");

        // Using Stream flatMap(Function mapper)
        list.stream().flatMap(str -> Stream.of(str.charAt(2))).forEach(System.out::println);
    }

=======================================================
    @Test
    public void charcount()
    {
        // Creating a List of Strings
        List<String> list = Arrays.asList("A", "A", "C", "D","E","D");

        // Using distinct function
        list.stream().distinct().forEach(System.out::println);
        list.stream().limit(3).forEach(System.out::print);
       long num =  list.stream().filter(n->n.startsWith("A")).count();
       System.out.println(" "+num);
    }

=======================================================
    @Test
    public void minmax()
    {
        // Creating a List of Integer
        List<Integer> list = Arrays.asList(2,3,9,3,1,5);

        // Using min function
       Optional<Integer> min = list.stream().min( (a1, a2)->{ return a1.compareTo(a2); } );
       System.out.println(min.get());

    }

=======================================================
    @Test
    public void max()
    {
        // Creating a List of Integer
        List<Integer> list = Arrays.asList(2,3,9,3,1,5);

        // Using min function
        Object[] a = list.stream().toArray();
        System.out.println(a.length);
        for(Object b :a)
            System.out.println(b);

    }

=======================================================
    @Test
    public void sorting()
    {
        // Creating a List of Integer
        List<Integer> list = Arrays.asList(2,3,9,1,1,3,1,5);

        // Using sorted
        List<Integer> a = list.stream().sorted().collect(Collectors.toList());
        System.out.println(a);

        List<Integer> b = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
        System.out.println(b);

    }
}


Q: A Stream API: What Is It? Why is the Stream API necessary?

Ans:
Java 8 now has a new functionality called Stream API. Stream API is a unique class used for handling items from sources like collections.

The Stream API is required because: 
  • Processing is straightforward since it offers aggregate operations.
  • Programming in the functional style is supported.
  • The processing speed is accelerated. It is, therefore, suitable for greater performance.
  • Parallel operations are possible.

Q: Can you convert an array to Stream? How? 

Yes, you can convert an array to Stream in Java. The Stream class provides a factory method to create a Stream from an array, like Stream .of(T ...) which 
accepts a variable argument, that means you can also pass an array to it as shown in the following example:

import java.util.*;
import java.util.stream.*;
class Main {
    public static void main(String[] args) {
        
    String[] languages = {"Java", "Python", "JavaScript"};
    Stream<String> numbers = Stream.of(languages);
    numbers.forEach(System.out::println);
  }
}

Output:
Java
Python
JavaScript

===========================================================
import java.util.*;
import java.util.stream.*;
class Main {
    public static void main(String[] args) {
        
    int[] languages = {1,2,5,6,8};
    IntStream numbers = Arrays.stream(languages);
    numbers.forEach(System.out::println);
  }
}

Q: Operation on map with stream API 

Ans:

        Map<String, Integer> map = new HashMap<>();
        map.put("Alice", 30);
        map.put("Bob", 25);
        map.put("Charlie", 35);

        // Iterate over the Map using Stream API
        map.entrySet().stream().forEach(entry -> System.out.println("Key: " + entry.getKey() + ", Value: " + entry.getValue()));
==========================================================================

        Map<String, Integer> map = new HashMap<>();
        map.put("Alice", 30);
        map.put("Bob", 25);
        map.put("Charlie", 35);

        // Filter and transform entries using Streams
        Map<String, Integer> filteredMap = map.entrySet().stream()
            .filter(entry -> entry.getValue() >= 30)  // Filter entries
            .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); // Collect to new Map

        // Print the filtered Map
        filteredMap.forEach((key, value) -> 
            System.out.println("Key: " + key + ", Value: " + value)
        );
===========================================================================

        Map<String, Integer> map = new HashMap<>();
        map.put("Alice", 30);
        map.put("Bob", 25);
        map.put("Charlie", 35);

        // Iterate over keys using Stream API
        map.keySet().stream()
            .forEach(key -> 
                System.out.println("Key: " + key)
            );
===========================================================================
        Map<String, Integer> map = new HashMap<>();
        map.put("Alice", 30);
        map.put("Bob", 25);
        map.put("Charlie", 35);

        // Iterate over values using Stream API
        map.values().stream()
            .forEach(value -> 
                System.out.println("Value: " + value)
            );
============================================================================

Q: What are the build-in functions present in Stream API ?

Ans:
1. filter()
Purpose: Filters elements of the stream based on a given predicate.
Lambda Expression Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6);
List<Integer> evenNumbers = numbers.stream()
                                   .filter(n -> n % 2 == 0)
                                   .collect(Collectors.toList());
// Output: [2, 4, 6]

2. map()
Purpose: Applies a function to each element and returns a stream of the results.
Lambda Expression Example:

List<String> names = Arrays.asList("John", "Jane", "Jack");
List<Integer> nameLengths = names.stream()
                                 .map(name -> name.length())
                                 .collect(Collectors.toList());
// Output: [4, 4, 4]

3. flatMap()
Purpose: Flattens a stream of collections into a single stream.
Lambda Expression Example:

List<List<String>> names = Arrays.asList(
    Arrays.asList("John", "Jane"),
    Arrays.asList("Jack", "Jill")
);
List<String> flatList = names.stream()
                             .flatMap(list -> list.stream())
                             .collect(Collectors.toList());
// Output: [John, Jane, Jack, Jill]

4. reduce()
Purpose: Reduces the elements of a stream to a single value.
Lambda Expression Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4);
int sum = numbers.stream()
                 .reduce(0, (a, b) -> a + b);
// Output: 10

5. collect()
Purpose: Collects the elements of a stream into a collection.
Lambda Expression Example:
java
Copy code
List<String> names = Arrays.asList("John", "Jane", "Jack");
List<String> uppercaseNames = names.stream()
                                   .map(name -> name.toUpperCase())
                                   .collect(Collectors.toList());
// Output: [JOHN, JANE, JACK]

6. forEach()
Purpose: Performs an action for each element of the stream.
Lambda Expression Example:
java
Copy code
List<String> names = Arrays.asList("John", "Jane", "Jack");
names.stream().forEach(name -> System.out.println(name));
// Output: John, Jane, Jack

7. sorted()
Purpose: Sorts the elements of the stream.
Lambda Expression Example:

List<String> names = Arrays.asList("John", "Jane", "Jack");
List<String> sortedNames = names.stream()
                                .sorted((a, b) -> a.compareTo(b))
                                .collect(Collectors.toList());
// Output: [Jack, Jane, John]

8. distinct()
Purpose: Removes duplicate elements from the stream.
Lambda Expression Example:
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 2, 3, 4, 4, 5);
List<Integer> distinctNumbers = numbers.stream()
                                       .distinct()
                                       .collect(Collectors.toList());
// Output: [1, 2, 3, 4, 5]

9. limit()
Purpose: Limits the number of elements in the stream.
Lambda Expression Example:
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> limitedNumbers = numbers.stream()
                                      .limit(3)
                                      .collect(Collectors.toList());
// Output: [1, 2, 3]

10. skip()
Purpose: Skips a certain number of elements in the stream.
Lambda Expression Example:
java
Copy code
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> skippedNumbers = numbers.stream()
                                      .skip(2)
                                      .collect(Collectors.toList());
// Output: [3, 4, 5]

11. anyMatch(), allMatch(), noneMatch()
Purpose: Checks if any, all, or none of the elements match a given predicate.
Lambda Expression Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
boolean hasEven = numbers.stream().anyMatch(n -> n % 2 == 0); // true
boolean allEven = numbers.stream().allMatch(n -> n % 2 == 0); // false
boolean noneNegative = numbers.stream().noneMatch(n -> n < 0); // true

12. findFirst() and findAny()
Purpose: Finds the first or any element in the stream.
Lambda Expression Example:

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Optional<Integer> first = numbers.stream().findFirst(); // Output: Optional[1]
Optional<Integer> any = numbers.stream().findAny();     // Output: Optional[1] or any other element


Q: Write a program to print prime number with java stream

Ans:

 public static void main(String[] args) {
        int number = 6; // Replace with the number you want to check
        boolean isPrime = isPrime(number);
        System.out.println(number + " is prime: " + isPrime);
    }

    public static boolean isPrime(int number) {
        // Edge cases
        if (number <= 1) return false;
        if (number == 2) return true; // 2 is the only even prime number
        if (number % 2 == 0) return false; // Eliminate even numbers greater than 2

        // Check divisibility from 3 to sqrt(number) using stream
        return IntStream.rangeClosed(3, (int) Math.sqrt(number))
                        .filter(i -> i % 2 != 0) // Only check odd numbers
                        .noneMatch(i -> number % i == 0);
    }
  • We use IntStream.rangeClosed to create a stream of integers from 3 up to the square root of the number. This is because a larger factor of the number must be paired with a smaller factor that would have already been checked.
  • We filter out even numbers since we’ve already handled divisibility by 2.
  • We use noneMatch to check if there is any number in the range that divides the given number without a remainder. If there is no such number, the number is prime.
===================================================

 Q: Before Java8 code vs after java 8 code:


1. Reversing a String
Traditional Approach (Pre-Java 8):

public class ReverseString {
    public static void main(String[] args) {
        String input = "hello";
        String reversed = "";
        for (int i = input.length() - 1; i >= 0; i--) {
            reversed += input.charAt(i);
        }
        System.out.println(reversed);  // Output: "olleh"
    }
}

Java 8 Approach (Using StringBuilder):

public class ReverseStringJava8 {
    public static void main(String[] args) {
        String input = "hello";
        String reversed = new StringBuilder(input).reverse().toString();
        System.out.println(reversed);  // Output: "olleh"
    }
}
Comparison: Java 8 simplifies this task by using StringBuilder with the reverse() method. The traditional approach used a loop and string concatenation, which is less efficient.
===========================================

2. Check if String is a Palindrome
Traditional Approach:

public class Palindrome {
    public static void main(String[] args) {
        String input = "madam";
        String reversed = "";
        for (int i = input.length() - 1; i >= 0; i--) {
            reversed += input.charAt(i);
        }
        if (input.equals(reversed)) {
            System.out.println("Palindrome");
        } else {
            System.out.println("Not Palindrome");
        }
    }
}
Java 8 Approach (Using StringBuilder):

public class PalindromeJava8 {
    public static void main(String[] args) {
        String input = "madam";
        if (input.equals(new StringBuilder(input).reverse().toString())) {
            System.out.println("Palindrome");
        } else {
            System.out.println("Not Palindrome");
        }
    }
}
Comparison: In Java 8, you can reverse the string using StringBuilder in one line, whereas the traditional approach involved manually reversing the string with a loop.

===========================================

3. Count Vowels in a String
Traditional Approach:

public class CountVowels {
    public static void main(String[] args) {
        String input = "hello";
        int count = 0;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if ("aeiou".indexOf(c) != -1) {
                count++;
            }
        }
        System.out.println("Vowel count: " + count);
    }
}
Java 8 Approach (Using Streams):


public class CountVowelsJava8 {
    public static void main(String[] args) {
        String input = "hello";
        long count = input.chars()
                          .mapToObj(c -> (char) c)
                          .filter(c -> "aeiou".indexOf(c) != -1)
                          .count();
        System.out.println("Vowel count: " + count);
    }
}
Comparison: In Java 8, you can leverage streams to process each character and filter only vowels, making the code more concise and functional. The traditional approach involved manually checking each character in the loop.

================================================

4. Find the First Non-Repeated Character
Traditional Approach:

public class FirstNonRepeatedChar {
    public static void main(String[] args) {
        String input = "swiss";
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (input.indexOf(c) == input.lastIndexOf(c)) {
                System.out.println("First non-repeated character: " + c);
                break;
            }
        }
    }
}
Java 8 Approach (Using Collectors):

import java.util.*;
import java.util.stream.*;

public class FirstNonRepeatedCharJava8 {
    public static void main(String[] args) {
        String input = "swiss";
        Optional<Character> result = input.chars()
                                          .mapToObj(c -> (char) c)
                                          .collect(Collectors.groupingBy(c -> c, LinkedHashMap::new, Collectors.counting()))
                                          .entrySet()
                                          .stream()
                                          .filter(entry -> entry.getValue() == 1)
                                          .map(Map.Entry::getKey)
                                          .findFirst();
        result.ifPresent(c -> System.out.println("First non-repeated character: " + c));
    }
}
Comparison: The traditional method uses indexOf() and lastIndexOf() to check character repetition, while the Java 8 approach uses streams to group characters and find the first one with a count of 1.

=============================================

5. Remove Whitespaces from a String
Traditional Approach:

public class RemoveWhitespaces {
    public static void main(String[] args) {
        String input = " hello world ";
        String result = input.replaceAll("\\s", "");
        System.out.println(result);  // Output: "helloworld"
    }
}
Java 8 Approach (Using replaceAll with Regex):

public class RemoveWhitespacesJava8 {
    public static void main(String[] args) {
        String input = " hello world ";
        String result = input.replaceAll("\\s", "");
        System.out.println(result);  // Output: "helloworld"
    }
}
Comparison: The logic remains the same in both approaches. Java 8 doesn't add much to this case since replaceAll() is available in both versions.

==================================================

6. Convert String to Uppercase
Traditional Approach:

public class ConvertToUpper {
    public static void main(String[] args) {
        String input = "hello";
        String result = input.toUpperCase();
        System.out.println(result);  // Output: "HELLO"
    }
}
Java 8 Approach (Using toUpperCase):

public class ConvertToUpperJava8 {
    public static void main(String[] args) {
        String input = "hello";
        String result = input.toUpperCase();
        System.out.println(result);  // Output: "HELLO"
    }
}
Comparison: Both approaches use the toUpperCase() method, and there is no difference in functionality between Java and Java 8 for this task.

===========================================
7. Check if a String is Empty
Traditional Approach:

public class CheckEmpty {
    public static void main(String[] args) {
        String input = "";
        if (input.length() == 0) {
            System.out.println("String is empty");
        } else {
            System.out.println("String is not empty");
        }
    }
}
Java 8 Approach (Using isEmpty):

public class CheckEmptyJava8 {
    public static void main(String[] args) {
        String input = "";
        if (input.isEmpty()) {
            System.out.println("String is empty");
        } else {
            System.out.println("String is not empty");
        }
    }
}
Comparison: In Java 8, you can use isEmpty() directly instead of checking the length. This is more concise and readable.

=================================================

8. Join Strings
Traditional Approach:

public class JoinStrings {
    public static void main(String[] args) {
        String[] words = {"hello", "world"};
        String result = "";
        for (String word : words) {
            result += word + " ";
        }
        System.out.println(result.trim());
    }
}
Java 8 Approach (Using String.join):

public class JoinStringsJava8 {
    public static void main(String[] args) {
        String[] words = {"hello", "world"};
        String result = String.join(" ", words);
        System.out.println(result);  // Output: "hello world"
    }
}
Comparison: In Java 8, String.join() provides a simple and efficient way to join an array of strings with a delimiter.

==================================================

9. Convert String to List of Characters
Traditional Approach:

import java.util.*;

public class StringToList {
    public static void main(String[] args) {
        String input = "hello";
        List<Character> list = new ArrayList<>();
        for (int i = 0; i < input.length(); i++) {
            list.add(input.charAt(i));
        }
        System.out.println(list);  // Output: [h, e, l, l, o]
    }
}
Java 8 Approach (Using Streams):


import java.util.*;
import java.util.stream.*;

public class StringToListJava8 {
    public static void main(String[] args) {
        String input = "hello";
        List<Character> list = input.chars()
                                    .mapToObj(c -> (char) c)
                                    .collect(Collectors.toList());
        System.out.println(list);  // Output: [h, e, l, l, o]
    }
}
Comparison: The Java 8 version uses streams to convert the string into a list of characters, making the code more concise.

===============================================

10. Check if String Contains Substring
Traditional Approach:


public class ContainsSubstring {
    public static void main(String[] args) {
        String input = "hello world";
        if (input.contains("world")) {
            System.out.println("Substring found");
        } else {
            System.out.println("Substring not found");
        }
    }
}
Java 8 Approach (Using contains):

public class ContainsSubstringJava8 {
    public static void main(String[] args) {
        String input = "hello world";
        if (input.contains("world")) {
            System.out.println("Substring found");
        } else {
            System.out.println("Substring not found");
        }
    }
}
Comparison: Both approaches use the contains() method, and there is no significant difference between Java and Java 8 in this case.
================================================

Comments