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.");
}
}
Explanations
-
import java.util.stream.IntStream;: Imports theIntStreamclass from thejava.util.streampackage, which provides methods for processing sequences of primitive integer values using functional-style operations. -
class Main {: Declares a public class namedMainto 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 usingIntStream.of(num), then uses the terminal operationanyMatch()with a lambda expression (n -> n == toFind) to check if any element in the stream matches the target value, storing the resulting boolean (trueorfalse) in thefoundvariable. -
if(found): Evaluates whether the boolean flagfoundis true. -
System.out.println(toFind + " is found.");: Executes iffoundis 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;
Reference :
https://www.programiz.com/java-programming/examples/array-contains-value
Top comments (0)