DEV Community

Cover image for One Commit at a Time Series - 5
Annapoorani Kadhiravan
Annapoorani Kadhiravan

Posted on

One Commit at a Time Series - 5

JAVA

### Check if an array contains the given value using Stream

import java.util.stream.IntStream;

class Main {
  public static void main(String[] args) {

    int[] num = {1, 2, 3, 4, 5};
    int toFind = 7;

    boolean found = IntStream.of(num).anyMatch(n -> n == toFind);

    if(found)
      System.out.println(toFind + " is found.");
    else
      System.out.println(toFind + " is not found.");

  }
}

Enter fullscreen mode Exit fullscreen mode

Explanations

  • import java.util.stream.IntStream;: Imports the IntStream class from the java.util.stream package, which provides methods for processing sequences of primitive integer values using functional-style operations.
  • class Main {: Declares a public class named Main to house the Java application.
  • public static void main(String[] args) {: The standard entry point method where the Java Virtual Machine begins program execution.
  • int[] num = {1, 2, 3, 4, 5};: Declares and initializes an integer array containing the sequence of numbers from 1 to 5.
  • int toFind = 7;: Defines the target integer value (7) that we want to search for in the array.
  • boolean found = IntStream.of(num).anyMatch(n -> n == toFind);: Converts the array into a sequential stream using IntStream.of(num), then uses the terminal operation anyMatch() with a lambda expression (n -> n == toFind) to check if any element in the stream matches the target value, storing the resulting boolean (true or false) in the found variable.
  • if(found): Evaluates whether the boolean flag found is true.
  • System.out.println(toFind + " is found.");: Executes if found is true, printing a confirmation message.
  • else: Executes if the match condition evaluates to false.
  • System.out.println(toFind + " is not found.");: Prints a message stating that the target value does not exist in the array (which executes in this example since 7 is not in the array).

SQL

DQL – Data Query Language - Focused on retrieving data.

SELECT → Fetch data from tables.

Clauses like WHERE, GROUP BY, HAVING, ORDER BY refine queries.

Example:

SELECT Name, Age FROM Students WHERE Age > 20;
Enter fullscreen mode Exit fullscreen mode

Reference :
https://www.programiz.com/java-programming/examples/array-contains-value

Top comments (0)