Saturday, 10 October 2020

Java Stream API: anyMatch(), allMatch(), and noneMatch() with Examples

          Java 8 introduced many powerful features, and the Stream API is one of the most important because it simplifies data processing and makes the code more concise and readable. In this post, we will explore the anyMatch(), allMatch(), and noneMatch() methods with examples.
  • anyMatch() – Returns true if at least one element in the stream matches the given predicate; otherwise, it returns false.

  • allMatch() – Returns true if all elements in the stream match the given predicate; otherwise, it returns false.

  • noneMatch() – Returns true if none of the elements in the stream match the given predicate; otherwise, it returns false.

Example:

package com.practice;

import java.util.ArrayList;
import java.util.List;

public class StreamMatchExample {

	public static void main(String[] args) {

		List<Employee> employeeList = new ArrayList<>();
		employeeList.add(new Employee("Mahesh", "Male", "abc@gmail.com"));
		employeeList.add(new Employee("Sathish", "Male", "abc@gmail.com"));
		employeeList.add(new Employee("Mahesh", "Male", "abc@gmail.com"));
		employeeList.add(new Employee("Pooja", "Female", "abc@gmail.com"));

		boolean allMatch = employeeList.stream().allMatch(
                               emp -> emp.getEmail().equalsIgnoreCase("abc@gmail.com"));
		System.out.println("allMatch - " + allMatch);

		boolean anyMatch = employeeList.stream().anyMatch(
                               emp -> emp.getName().equalsIgnoreCase("Mahesh"));
		System.out.println("anyMatch - " + anyMatch);

		boolean noneMatch = employeeList.stream().noneMatch(
                               emp -> emp.getName().equalsIgnoreCase("Anil"));
		System.out.println("noneMatch - " + noneMatch);
	}

}
 
Output :--
allMatch - true
anyMatch - true
noneMatch - true

Related Posts:-

Thursday, 25 June 2020

Difference Between CrudRepository and JpaRepository in Spring Data JPA

     In this post, we will discuss the differences between CrudRepository and JpaRepository in Spring Data JPA. Both CrudRepository and JpaRepository are interfaces provided by the Spring Data framework for performing database operations.

JpaRepository extends PagingAndSortingRepository, which in turn extends CrudRepository. As a result, JpaRepository inherits all the CRUD methods provided by CrudRepository, along with the pagination and sorting capabilities of PagingAndSortingRepository. In addition, JpaRepository provides several JPA-specific methods, such as flushing the persistence context and performing batch delete operations.

The three repository interfaces provide the following functionalities:

  • CrudRepository – Provides basic CRUD (Create, Read, Update, Delete) operations.

  • PagingAndSortingRepository – Adds support for pagination and sorting.

  • JpaRepository – Extends PagingAndSortingRepository and provides additional JPA-specific operations, such as flushing the persistence context, batch inserts, and batch deletes.


Difference between CrudRepository and JpaRepository


Thank you for visiting blog.


Related Posts:-

Saturday, 18 April 2020

Difference Between openSession() and getCurrentSession() Methods in Hibernate

         As we know, there are two ways to obtain a Session from the Hibernate SessionFactory: openSession() and getCurrentSession().

The openSession() method always creates and returns a new Session instance each time it is called. In contrast, the getCurrentSession() method returns the Session associated with the current context (such as the current thread or transaction). If no Session is available in the current context, Hibernate creates a new one and associates it with that context.

Let's explore a few more differences between openSession() and getCurrentSession().


openSession():-

  • The openSession() method creates a new Session instance every time it is called.

  • You must explicitly flush and close the Session after use.

  • In a single-threaded environment, openSession() is generally less efficient than getCurrentSession() because it creates a new Session for every call.

  • No additional Hibernate configuration is required to use the openSession() method.



getCurrentSession():-

  • The getCurrentSession() method returns the Session associated with the current Hibernate context. If no Session exists in the current context, Hibernate creates a new one and binds it to that context.

  • You do not need to explicitly flush or close the Session. When using transaction management, Hibernate automatically manages the session lifecycle.

  • In a single-threaded environment, getCurrentSession() is generally more efficient than openSession() because it reuses the context-bound Session instead of creating a new one for every request.

  • To use getCurrentSession(), you must configure the hibernate.current_session_context_class property (or use a framework such as Spring, which manages the current session automatically). Otherwise, calling getCurrentSession() may result in a HibernateException.


Thank you for visiting the blog.