Skip to main content

Posts

Amazing Facts About java

Exceptions:-    1) One catch with many Exception(I am talking about java 7 special feature) do not allow to keep same type (i.e from same hierarchy ) in one catch with piped symbols. catch (FileNotFoundException | IOException e)  2) It is not mandatory to catch or handle unchecked exception, Following method we can call with try block easily:- public static void doSomething() throws NullPointerException{  }  3) You can not override methods with broader exception , For example super class methods throws FileNotFoundException then your sub class can not override same method changing the exception to IOException. 4) You can not assign values to exception reference if it is multi-catch block , Reason is it is made final by Java giants .   Threads:-  1) Even you make thread to sleep , it is not guaranteed it will start working right after completing the sleep time. It will go in runnable state and whenever it will get time from scheduler will start executing. Scheduler is OS
Recent posts

Stream with filter and collection - Java 8

We can use stream with filters and collections:- a) Stream provide link with a collection. b) Filter can filter the data from linked filter c) Collector can collect the data based on used filter. Here is the simple demonstration:- package com.shadab.collections.stream; import java.util.Arrays; import java.util.List; import java.util.Set; import java.util.stream.Collectors; public class StreamDemo { public static void main(String[] args) { List<Integer> listIntegers = Arrays.asList(8, 9, 10, 7, 4,3,2,1,5,6,7,9); System.out.println("All numbers are:- "+listIntegers); Set<Integer> uniqueOddNumbers = listIntegers.stream().filter(number -> number % 2 != 0).collect(Collectors.toSet());   System.out.println("Unique odd numbers are:- "+uniqueOddNumbers); } } If you get surprised by seeing number -> number  in java code please click here . Output:- All numbers are:- [8, 9, 10, 7, 4, 3, 2, 1, 5, 6, 7, 9] Unique odd numbers are:- [1