Skip to main content

Posts

Showing posts with the label Java8

Java 8 Stream API Filter() Method with Examples | Programming Blog

Java 8 Stream Filter with Lambda Expression Java 8 Stream Filter() method with Examples What is Filter in Java Stream?      Simply, as a name suggest it filters data based on given predicate (boolean) Condition. The condition is applied to all element of Stream, and those who satisfied the condition moves to next stage and those who does not pass filtered out. For example, If you want to filter list of Integer based on greater than some value or if you want to get even or odd data then you can easily filtered out. Stream Filter() method is Intermediate Operation. If you want to learn about difference between Intermediate and Terminal operation check out:- Difference between Intermediate and Terminal Operation The benefit of using filter() method is lazy evaluation. Means no data comparison is performed unless you call a terminal operation like forEach() or Collect() . We will use Lambda expression for filter elements based on given Predicate. Learn more about Lambda Expr...

How to Sort object property with null values using Java 8 Lambda expression

Sort custom object property with null values using Java 8 lambda expression What is lambda expression? The expression through which we can represent an anonymous function. learn more about lambda expression : Lambda Expression in Java 8 with examples   Lets see how we can sort custom object property without and with lambda expression. Example 1 : Sort custom object without lambda expression using Collections.sort() Programming.java public class Programming {     private int id;     private String name;          public Programming(int id, String name) {         this.id = id;         this.name = name;     }     public int getId() {         return id;     }     public void setId(int id) {         this.id = id;     }    ...

A Guide to Iterator in Java | hasNext(), next(), remove(), forEachRemaining() Methods

What is Iterator in Java? Java Iterator is an Interface that is used to iterate over a collection of java object one by one.  When we want to access collection elements, add/remove or process the elements. In order to do all this processing through a Java program, we should be able to traverse through the collection that we are using. This is where the iterator is used. Java Iterator is a collection framework interface and it is part of the java.util package. In order to use an Iterator, we need to get the iterator object using the iterator() method of the collection interface. Java Iterator supports only Forward Direction Iteration. So it is also know as Uni-Directional Cursor.  Java Iterator consists 4 methods : hasNext() next() remove() forEachRemaining() Syntax : List<String> list = new ArrayList<>(); Iterator<String> iterator = list.iterator(); 1. hasNext() Method in Iterator The hasNext() method is used for checking if there's at least one element ...

Java Lambda Expressions HackerRank solution with Explanation

Lambda Expressions in Java What is Lambda Expression? Lambda expression is just anonymous function or we can say nameless function.  Lambda Expression in Java 8 with examples   Problem Description : Write the following methods that return a lambda expression performing a specified action: PerformOperation isOdd(): The lambda expression must return true if a number is odd or false if it is even. PerformOperation isPrime(): The lambda expression must return  if a number is prime or  if it is composite. PerformOperation isPalindrome(): The lambda expression must return  if a number is a palindrome or  if it is not. See full description on HackerRank : Java Lambda Expressions Probelm Description   Sample Input and Output : Input 1 4 2 5 3 898 1 3 2 12 Output EVEN PRIME PALINDROME ODD COMPOSITE The first integer specifies the condition to check for (1 for Odd/Even, 2 for Prime, or 3 for Palindrome). The second integer denotes the number to be checked. So l...

Migratory Birds HackerRank Solution in Java

Java Solution for Migratory Birds HackerRank Problem | Find Lowest Maximum frequently Numbers in Java List or Array Given an array of bird sightings where every element represents a bird type id, determine the id of the most frequently sighted type. If more than 1 type has been spotted that maximum amount, return the smallest of their ids. Example 1 : arr = [1, 1, 2, 2, 3] output = 1 There are two each of types 1 and 2, and one sighting of type 3.  Pick the lower of the two types seen twice: type 1. Example 2 : arr = [1, 2, 3, 4, 5, 4, 3, 2, 1, 3, 4] output = 3 The different types of birds occur in the following frequencies: Type 1 : 2 bird  Type 2 : 2 bird  Type 3 : 3 bird  Type 4 : 3 birds Type 5 : 1 bird  In this example we have two type of frequent birds : 3 and 4. From both 3 is lower than 4 so answer will be 3. Means we have to simply find out maximum frequent number from given list or array which value is lower than other same numbers. So lets see sol...

Subarray Division HackerRank solution in Java with examples

Java solution for Subarray Division hackerRank Problem Problem Description : Two children, Lily and Ron, want to share a chocolate bar. Each of the squares has an integer on it.   Lily decides to share a contiguous segment of the bar selected such that:  The length of the segment matches Ron's birth month, and, The sum of the integers on the squares is equal to his birth day. Determine how many ways she can divide the chocolate. Example 1 : s = [2, 2, 3, 1, 2] d = 4 m = 2 Output : 2 Lily wants to find segments summing to Ron's birth day, d = 4 with a length equaling his birth month, m = 2 In this case, there are two segments meeting her criteria: [2, 2] and [1, 3] Example 2 : Input : s = [1, 2, 1, 3, 2] d = 3 m = 2 Output : 2 Explanation : Lily wants to give Ron m = 2 squares summing to d = 3. The following two segments meet the criteria: 1 + 2 = 3 and 2 + 1 = 3 Example 3 : Input : s = [4, 5, 4, 2, 4, 5, 2, 3, 2, 1, 1, 5, 4] d = 15 m = 4 Output : 3 Explanation : Lily wa...

Apple and Orange Hackerrank solution in Java | Java 8 solution

Java 8 solution for Apple and Orange HackerRank problem  Sam's house has an apple tree and an orange tree that yield an abundance of fruit. Using the information given below, determine the number of apples and oranges that land on Sam's house.  In the diagram below: The red region denotes the house, where s is the start point, and t is the endpoint. The apple tree is to the left of the house, and the orange tree is to its right.  Assume the trees are located on a single point, where the apple tree is at point a , and the orange tree is at point b . When a fruit falls from its tree, it lands d units of distance from its tree of origin along the x-axis. *A negative value of d means the fruit fell d units to the tree's left, and a positive value of d means it falls d units to the tree's right.   Given the value of d for m apples and n oranges, determine how many apples and oranges will fall on Sam's house (i.e., in the inclusive range [s, t])? For example, Sam...

How to find All Disappeared Numbers in an Java Array | Java LeetCode Solution

Find All Numbers that are not present in Java Array from 1 to n Problem Description : Given an array nums of n integers where nums[i] is in the range [1, n], return an array of all the integers in the range [1, n] that do not appear in nums array. Example 1 : Input: nums = [1, 3, 5, 1, 3] Output: [2, 4] Example 2 : Input: nums = [2, 4, 2, 4] Output: [1, 3] Example 3 : Input: nums = [4, 3, 2, 7, 8, 2, 3, 1] Output: [5, 6] Example 4 : Input: nums = [1, 1] Output: [2] In given problem, we have to return List of Integers that are not present in given array range [1 to n]. Lets see solution and its explanation step by step. Solution 1 : Using in-place array (Without use if extra space) import java.util.ArrayList; import java.util.List;  class Solution {     public List<Integer> findDisappearedNumbers(int[] nums) {                  List<Integer> list = new ArrayList<Integer>(); ...

Supplier in Java 8 with Examples | get() method

What is supplier in Java 8? As a name suggest, Supplier is a Functional Interface which does not take any arguments but produces a value. It is part of java.util.function package and introduces in Java 8. Suppliers are useful when we don’t need to supply any value and obtain a result at the same time. Supplier is contains only one abstract method : get() Return type is T - the type of results supplied by this supplier. Lest see simple example of supplier interface. Example 1 : Print random number using Supplier Interface import java.util.function.Supplier; public class SupplierDemo {     public static void main(String[] args) {         Supplier<Double> supplier1 = () -> Math.random(); Supplier<Integer> supplier2 = () -> (int)(Math.random()*10); System.out.println("Random value from 0 to 1 : "+ supplier1.get()); System.out.println("Random value from 0 to 1 : "+ supplier1.get()); System.out.println("Random value ...

BiPredicate in Java 8 with examples | test(), and(), or() and negate() methods

What is BiPredicate? BiPredicate is same as  Predicate , but it accepts two arguments. BiPredicate is Functional interface which accepts two arguments and return boolean value. BiPredicate contains following methods :  test() : abstarct method and() : default method or() : default method negate() : default method So lets see examples of BiPredciate Example 1 : Check even number using BiPredicate import java.util.function.BiPredicate; public class BiPredicateDemo {     public static void main(String[] args) {                 BiPredicate<Integer, Integer> isEven = (number1, number2) -> {             return (number1 + number2) % 2 == 0;         };                 System.out.println(isEven.test(5, 6));         System.out.println(isEven...