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 that there are two ways to create/get session in Hibernate. There are two methods openSession() and getCurrentSession() methods to create/get session object from SessionFactory class. The openSession() method always creates new session each time but getCurrentSession() method of sessionFactory returns the session object from context, if session is not present in the context then will create new session object.

We can see few more differences as below,


openSession():-

  • It will create new Session object each time when you call openSession() method.
  • You need to explicitly flush and close session objects.
  • In single threaded environment , It is slower than getCurrentSession() method.
  • No need to configure any property to call this method.


getCurrentSession():-

  • It creates a new Session if session doesn't exists , else it will use same session which is in current hibernate context.
  • No need to flush and close session objects, it will be automatically taken care by Hibernate internally.
  • In single threaded environment , it is faster than getOpenSession() method.
  • Need to configure additional property “hibernate.current_session_context_class” to call getCurrentSession() method, otherwise it will throw an exception.

Related Posts:--