Saturday, 10 October 2020

Examples of anyMatch(), allMatch() and noneMatch() methods of stream API in Java

          Java 8 has many features, but stream API is one of the important feature to simplify the code. In the current post, I will give some examples of  anyMatch(), allMatch() and noneMatch.

anyMatch() - any element of a stream match with predicate then will return true else false.

allMatch() - all elements of a stream should match with predicate then will return true else false.

noneMatch() - all elements of stream should not match with predicate then will return true else 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 see difference between CrudRepository and JpaRepository in Spring Data JPA. These two CrudRepository and JpaRepository are the interfaces of the Spring Data Core library.

      The JpaRepository extends CrudRepository and it inherits some of the methods available in the CrudRepository like findOne, get and so on. And JpaRepository also extends PagingAndSorting Repository uses for Pagination and Sorting purpose.

    The CrudRepository and PagingAndSortingRepository are the base interfaces of Spring Core library and it has below functionalities,
  • CrudRepository - Provides CRUD Operations.
  • PagingAndSortingRepository - Provides methods to do pagination and sorting records.
  • JpaRepository - Provides some JPA related methods such as flushing the persistence context and deleting records in a batch.

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.