Posts

Introduction to Lambda Expression with Examples

Java 8 comes up with one great features called Lambda Expressions. It's first step to the functional programming. With Lambda expression we can treat functionality as method arguments i.e. you can pass a method as argument to another method. Previously we use to write anonymous class if we wanted to pass some method as argument to another method but with lambda expression, we can pass a plain method as argument. Lambda expressions is a anonymous function i.e. It has arguments, a body and return type. Syntax of Lambda Expression : (Argument(s)) → {Body} Eg : (int x, int y, int z) - > {return x+y+z}; (String msg) - > {System.out.println(msg);} () - > { return 100;} Structure of Lambda Expression : It can have zero, one or more number of parameters. For empty set of parameters, Empty parentheses are used. e.g () -> 100 Type of the passed parameter can be explicitly declared or can be taken from context. e.g. (int x) is same as (x). One...

How to download and save image from URL

The 'javax.imageio.ImageIO' is a handy class which provides lots of utility methods related to images processing in Java. Using this class we can read and write images into disk. In below example, We will see how to use 'javax.imageio.ImageIO' to read an image from URL and save it into different formats. import java.awt.image.BufferedImage; import java.io.File; import java.net.URL; import javax.imageio.ImageIO; /** * This class will download the image from the specified URL and download it in * different format. * * @author abdulwaheed18 @gmail.com * */ public class ImageDownloader { /** * @param args */ public static void main(String[] args ) { String imageUrl = "http://img.gettyimageslatam.com/public/userfiles/redesign/images/landing/home/img_entry_002.jpg" ; try { System. out .println( "Downloading Image..." ); URL url = new URL( imageUrl ); ...

What is Semaphore with example ?

What is Semaphore? Semaphore is used to control access to common resource for completing multiple resources . It guards a critical section against entry by more than N threads at a time. The  java.util.concurrent.Semaphore   class was first introduced by Java in V 1.5. It has two main methods(): ·          acquire() ·          release() Semaphore is initialized with a given number of "permits” i.e. counter which keeps track of the number of resources available. When a request comes to resources, Semaphore checks the counter and if it is less than total count then it grant access to resources and subsequently reduces the counter. Similarly while releasing a resources, it increments the count. Thus, at most N threads can access the acquire method without any release() calls where N is number of permits the semaphore was initialized with. If the permits is in ON|OFF mode i....

How to extract a JSON value from a BASH script

Requirement : I was working on shell script where I need to get Id value from JSON and I tried so many thing but unable to get it without using any other tools. So, After trying so many failure attempt, I end up with this site (A big thanks to you) which resolved my issue. Here is how I fetched value from JSON, JSON = <YOUR_JSON> PROPERTY_TO_FETCH="<VALUE>" (In my case, it was "id") ID = `echo $JSON| sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $PROPERTY_TO_FETCH` echo ${ID ##*|} Which will print the value of 'id' from JSON.

How to execute maven goal on parent module but not on childer

I am working on multi module maven project and If I define any plugin in the parent pom.xml file, It get executed for all the child build as well. In simple language, I wanted to use one plugin in parent pom.xml file which read properties file from the base directory but whenever I executes it, all the sub pom.xml tries to read the properties file from their base directory and its ends up in maven failed. To resolve the above issue, We just need to add "inherit' attributes in the plugin. Example : <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>properties-maven-plugin</artifactId> <version>1.0-alpha-2</version> <inherited>false</inherited> <executions> <execution> <phase>initialize</phase> <goals> <goal>read-project-properties</goal> </goals> <configuration> <files> ...

How to Map a list of strings with JPA/Hibernate annotations ?

Image
Yesterday while working on my project, I got one requirement where I need to store list of Array into the database, I checked on Google, went through various site but didn't get much information. Everyone talks about creating new entity and do onetomany relationship BUT I wanted to create a collection of basic types. Finally I come across @ElementCollection annotation provided by JPA 2.0 which resolves my problem. Problem : Input JSON which we need to store in DB: { "schemas": ["urn:scim:schemas:core:1.0", "urn:scim:schemas:extension:enterprise:1.0"] }            Where schemas is Array of String. Solution : Just add following annotation in your POJO i,e @ElementCollection @CollectionTable(name = "SCIM_SCHEMAS", joinColumns = @JoinColumn(name = "SCHEMA_ID")) @Column(name = "SCIM_SCHEMA") private List<String> schemas; It will create the table named "SCIM_SCHEMAS" having columns 'SCHE...