Wednesday, July 14, 2021

Consumer Example in Java 8

 

Consumer Example in Java 8 


forEach in Iterable.java interface which uses Consumer: 

default void forEach(Consumer<? super T> action) 

{

    Objects.requireNonNull(action);

    for (T t : this) {

        action.accept(t);

    }

}



@FunctionalInterface
public interface Consumer<T>
Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.

This is a functional interface whose functional method is accept(Object).

Method:

void accept(T t)
  • andThen

    default Consumer<T> andThen(Consumer<? super T> after)
    Returns a composed Consumer that performs, in sequence, this operation followed by the after operation. If performing either operation throws an exception, it is relayed to the caller of the composed operation. If performing this operation throws an exception, the after operation will not be performed.
    Parameters:
    after - the operation to perform after this operation
    Returns:
    a composed Consumer that performs in sequence this operation followed by the after operation
    Throws:
    NullPointerException - if after is null

Example:

import java.util.Arrays;

import java.util.List;

import java.util.function.Consumer;


public class ConsumerEx {


public static void main(String[] args) {


List<String> list = Arrays.asList("india", "uk", "fiji");


Consumer<String> consumer = (t) -> {

System.out.println(t.toUpperCase());

};


list.forEach(consumer);


}


}


Output:

INDIA

UK

FIJI

Tuesday, January 12, 2021

Docker: Important Docker Commands Help

Docker: Important Docker Commands Help


Dockerfile:


FROM adoptopenjdk/openjdk11:latest

EXPOSE 8080

ADD "target/docker-demo-0.0.1-SNAPSHOT.jar" "docker-demo.jar"

ENTRYPOINT ["java","-jar","docker-demo.jar"]

Docker Commands:


 Build Docker Image:

docker build -t docker-demo .

 Tag Docker Image: 

docker tag docker-demo dineshnithdocker/docker-demo

Push Docker Image to Docker Hub 

docker push dineshnithdocker/docker-demo

Check docker Images present in local: 

docker images

Remove Local Docker Images:  

docker rmi docker-demo dineshnithdocker/docker-demo

 Pull Docker Images from Docker Hub and run:

docker run -p 8080:8080 dineshnithdocker/docker-demo


Wednesday, November 25, 2020

Leet Code Problems Solutions

Intersection of Two Arrays I

Given two arrays, write a function to compute their intersection.

Example 1:

Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]

Example 2:

Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Code : Solution 1 - 




public class ArrayIntersection {

	public static int[]  intersect(int[] nums1, int[] nums2) {

		if (nums1.length > nums2.length) {
			return intersect(nums2, nums1);
		}

		Map<Integer, Integer> map = new HashMap<Integer, Integer>();
		List<Integer> list = new ArrayList<Integer>();

		for (int i : nums1) {
			map.put(i, map.getOrDefault(i, 0) + 1);
		}

		for (int i : nums2) {
			int count = map.getOrDefault(i, 0);
			if (count > 0) {
				list.add(i);
				map.put(i, count - 1);
			}
		}

		int[] result = new int[list.size()];

		for (int i = 0; i < result.length; i++) {
			result[i] = list.get(i);
		}

		return result;
	}

	public static void main(String... aa) {
		int[] nums1= {1,2,2,1};
		int[] nums2= {2,2};
	
		System.out.println(intersect(nums1,nums2));
	}
}

Plus One

Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

 

Example 1:

Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.

Example 2:

Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.

Example 3:

Input: digits = [0]
Output: [1]

 

Constraints:

  • 1 <= digits.length <= 100
  • 0 <= digits[i] <= 9

Java

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > ...

Followers

Search This Blog

Popular Posts