Posts

Java 8: Sorting of Collection using lambda Expression

Image
In my previous blog, I have already discussed on Lambda Expression and its Syntax as well as Functional Interface. So, if you are not aware of then I would suggest you to read those blogs before starting this one. http://www.waheedtechblog.com/2019/01/how-to-write-lambda-expression-in-java-8.html http://www.waheedtechblog.com/2019/01/functional-interface-and-lambda.html As we know Comparator Interface is also a Functional Interface as it has only one abstract method. In this blog, we will see how we can do sorting using lambda expression. Let’s start with List and will cover Set, Map as well as Custom Class as well. List Sorting of elements on any Array List can be done easily using Collections.sort(…) functions where sort() can take argument as list or with list, you can also pass comparator object. In below example, we will see how can we sort using comparator and the same task with lambda expression. Output: 2  2.   ...

Don't clear logs after app get crashed - Android Studio

Image
Currently, I am working on one app which keep getting crash at PIE version and I was not getting any crash log under logcat. After some research here and there, I get to know that by default logcat logs based on selected process and once your app get crashes, it will clear the crash log as the existing process is gone. To see the crash logs: ·         Goto logcat and click on right hand side drop down button and select Edit Configuration. ·         Add Filter Name as your app Name and project Name as Package Name. Save it. Now try again. You will able to see your app crash log now.   Thank you. Happy Coding…!!!

Functional Interface and Lambda Expression

Functional Interface and Lambda Expression Functional Interfaces A functional interface is an interface that contains only one abstract method. It can have one or more default or static method but abstract method should be only one. Runnable , Callable, ActionListener , Comparable are some of the examples of functional interfaces as it contains only one abstract method. Let’s see some functional Interface implementation example: Example 1: public interface SimpleInterface {     public void abstractMethod(); } It’s a functional Interface as it only contains a single abstract method. Example 2: public interface ComplexInterface {     public void abstractMethod();     public default void defaultMethod(){         System.out.println(“Default method”);     }     public static void staticMethod(){         System.out.println(“Static me...

How to write Lambda Expression in Java 8

   How to write Lambda Expression? With the introduction of Lambda Expression in JAVA 8, We can write more readable, maintainable and concise code but I have seen people keep forgetting the syntax of lambda expression or they are always confused with it. In this blog, I ll just try to explain the basic things i.e. how easy is it to write Lambda Expression but before starting with the syntax, Let’ see what is lambda Expression? In simple term, Lambda expression (->) is an anonymous function which     - doesn’t have any name     - doesn’t have any modifiers     - doesn’t have any return type. To understand it in a more easy way, Let’s first write a simple Java function which display some message on the console. Example 1: Display Message public void display (){      system.out.println(“Learning Lambda Expression”);  }  Now to convert the above code into lambda expression, read the lambda definition once aga...

Java 8 - Type Inference

Image
Java 8 – Type Inference Type inference was first introduced in JDK 7 and later improved in JDK 8. It is a feature of Java which provides ability to compiler to look at each method invocation and corresponding declaration to determine the type of argument(s). The inference algorithm checks the types of the arguments and,if available, assigned type is returned. It tries to find a specific type which can full fill all type parameters. Till Java 6, We have to declare an Arraylist by mentioning its type explicitly at both side.             List<Integer> numbers = new ArrayList<Integer>(); and if we keep the type as blank at right side then Compiler generates unchecked conversion.             List<Integer> numbers = new ArrayList<>(); With JDK 7, It got improved and we started mentioning the type of arraylist only at one side. Below we can left second side as blank diamond and compil...