Design a site like this with WordPress.com
Get started

Configure your Git Bash with Github (Windows)

Step 1 Open your git bash terminal (Intall from here https://git-scm.com/downloads)

Step 2 Generate a key pair using ssh-key gen
ssh-keygen

Step 3 Add the generated private key to you ssh agent with “ssh-add”

Step 4 Login to your github account and navigate to settings

Step 4 On the settings page click on “SSH and GPG Keys” and click on “New SSH key”
Step 5 Add the contents of “.pub” file generated in our 2nd step in “key ” text area and provide a name in the “Title text” and click on “Add SSH key”

Step 6 Your setup is done. Now create a repository and try cloning it with git clone from your git bash terminal,

Advertisement

Java8 Streams – Convert List to Map

A lot of times we are required to convert one collection into another. It could be from list to set , set to map or otherwise. Before java introduced streams and functional programming, for doing such a transformation of collection was possible but then we had write a lot more code than we have to today with Java8 features.

Lets look at a simple example and transform a list to a map.

Lets say we have list of employee’s each having a name and id attribute. Our requirement is to convert the list into a map where key in the map is the “id” attribute of the employee and value is the employee object.

List<Employee> lstEmplyees = new ArrayList<>();
        lstEmplyees.add(new Employee(6606,"dipak"));
        lstEmplyees.add(new Employee(1212,"tony"));
        lstEmplyees.add(new Employee(779,"chetan"));

        Map<Integer,Employee> map =lstEmplyees.stream()
                                        .collect(Collectors.toMap(Employee::getId,Employee::get));
        System.out.println(map);

The above code creates a list 3 employee objects and then converts it into a map with help of streams.

Once you stream the collection, it provides us back with a stream of values in the list and then we can apply collect method to transform it into map. The collect method takes in Collector parameter, several of the implementations are already present in the “Collectors” class. For our use case we use Collectors.toMap. The toMap function takes two functions as input, first being the key mapper and second is the value mapper.

So as we know when we stream a collection, we are fed with stream of the objects inside the collection, one at a time.
In map function, we pass a function to identify the key from the employee object in our case. So for us it is the id attribute, so we return back the id attribute and the second parameter is the value function which return the value we want to associate with the streamed object.

Here is a simplified version of the above code

    List<Employee> lstEmplyees = new ArrayList<>();
        lstEmplyees.add(new Employee(6606,"dipak"));
        lstEmplyees.add(new Employee(1212,"tony"));
        lstEmplyees.add(new Employee(779,"chetan"));

        Function<Employee,Integer> keyMapper = (Employee employee) -> employee.getId();
        Function<Employee,Employee> valueMapper=  Function.identity();  // equvivaled to (Employee employee) -> employee;

        Map<Integer,Employee> map =lstEmplyees.stream()
                                        .collect(Collectors.toMap(keyMapper,valueMapper));
        System.out.println(map);

The above code works fine for just about any collection to convert to map. However, the above code will throw exception when there are duplicates in the collection. We haven’t told java what to do when there is a duplicate key. There is a third parameter that we can pass to tell java what do in case there is duplicate key.

The third parameter is the of the type – BinaryOperator which means it takes in 2 parameters of the same type and returns value of same type

Lets update our code to handle duplicates, in case of duplicates we want the new value to overwrite the old value.

List<Employee> lstEmplyees = new ArrayList<>();
        lstEmplyees.add(new Employee(6606,"dipak"));
        lstEmplyees.add(new Employee(1212,"tony"));
        lstEmplyees.add(new Employee(779,"chetan"));
        lstEmplyees.add(new Employee(779,"chetan1"));

        Function<Employee,Integer> keyMapper = (Employee employee) -> employee.getId();
        Function<Employee,Employee> valueMapper=  Function.identity();  // equvivaled to (Employee employee) -> employee;
        BinaryOperator<Employee> merge = (Employee oldVal,Employee newVal) -> newVal;

        Map<Integer,Employee> map =lstEmplyees.stream()
                                        .collect(Collectors.toMap(keyMapper,valueMapper));

Interesting to note here is that the merge function has parameter “Employee” instead of “Id attribute”. Its because the merge function is asking us to select the value to choose when there are duplicate keys.

There is one more variant of the “toMap” function. In the fourth parameter you can pass the map in which you would want to add the values.

Null Pointer Exception

Null Pointer Exception or NPE is one of the most common exception that anyone of us have encountered as a development engineer.

It is usually thrown when we try to access a method on a reference variable for which a value is evaluated to null.

for e.g

Employee e = getEmployee(123);
e.getEmployeeName();

The above code can throw a NPE. The above code assumes that the “getEmployee” method will always return a value but it may happen that it wont and return null;

In such a case the code at line 2 throws NPE as we are trying to access “getEmployeeName()” on a null value.

Mind you, the NPE is a runtime exception and the compiler will not complain that you have not handled a null check.

How to Resolve

In most cases a simple null check resolves the problem. Like in the above example an If condition will avoid NPE

Employee e = getEmployee(123);
if(e!=null)
e.getEmployeeName();

A code should be written to identify such conditions and handle them wherever necessary. As NPE is a runtime exception, if the code is not handled for null object access, the program might very well terminate abruptly for a small null check.