How to map integers to doubles using Java stream

import java.util.Arrays;
import java.util.List;

public class HelloWorld{

     public static void main(String []args){
        List numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
        List doubles =;
        List doubleSquares =>i.doubleValue() * 2).boxed().collect(Collectors.toList());

Quick snippet – Selecting only certain columns for a model in laravel

With a model named (Person) that maps to a table named (persons) with a structure defined below

CREATE TABLE `persons` (
  `first_name` VARCHAR(255) NOT NULL,
  `last_name` VARCHAR(255)  NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `persons` (`email`)

Then, Consider the eloquent statement given below

$result = Person::find(1);

this is equivalent to,


The above can be wasteful if all you require is just a few columns such as the firstname and lastname, to fix this, we use the eloquent statement below

$result = Person::find(1, ['first_name', 'last_name']);

This is equivalent to,

SELECT first_name, last_name FROM persons

Hope, you have been able to learn something new today.

Introduction to Logical Shift And Its Use Cases

If like me, you have a degree in computer science, you must have encountered logical shifts during the course of your academic studies, if not, if you have learnt pretty much any programming language, you would have encountered them. The issue is that most people treat them as something to be aware of but not really understood. If you give me a couple of minutes, I hope to help simplify it to you.

What is logical shift

Logical shift is a type of bitwise operation that shifts/moves the bits in its operands in a particular direction. In simple terms, if you logically shift X by Y, it means that the operation will move the bits in X by Y positions. Still sounds geeky, let us break it down together.

There are two types of logical shifts, namely

  1. Right Shift (denoted by >> )
  2. Left Shift (denoted by << )

If you consider the definition above, Right shift means that the bits in (X) would be shifted to the right by Y places, also Left shift means that the bits in (X) would be shifted to the left by Y places.

Example of Left Shift

let X = 4 and Y = 2, lets find X << Y

Convert X  to binary, then you have

X = 100  (i.e. the binary representation of 4 is 0000 0100 )

Note: you should use the 8 digits representation

If you find the binary calculation confusing, please read up about it here.

Now, to calculate (X << Y), i.e we move the bits in X  to the left by 2 places.

  1. before move X = 0000 0100
  2. after left move (1) , X = 0000 1000
  3. after leftmove (2) , X = 0001 0000

Now we convert (0001 0000) back to decimal and we have 2.

Hence, 4 << 2  == 16.

Example of Right Shift

let X = 8 and Y = 2, lets find X >> Y

Convert X  to binary, then you have

X = 1000  (i.e. the binary representation of 8 is 0000 1000).

Now, to calculate (X >> Y), i.e we move the bits in X to the right by 2 places.

  1. before move X = 0000 1000
  2. after right move (1) , X = 0000 0100
  3. after right move (2) , X = 0000 0010

Note: since the first 4 digits in the binary value of X is (0) and we are shifting to the right, we could have discared it and just used (1000).

Now we convert (0010) back to decimal and we have 2.

Hence, 8  >> 2  is equal to 2.

Use cases of logical shifts (both left and right).

Left shift (<<) is used to multiply a value by any power of 2 and Right bit shift (>>) is used to divide by any power of two. consider the table below

Cheatsheet or Ways of calulating the logical shifts faster

The left logical shift (<<) is the same as

Left Bit Shift
# Operation X Y Resolution Result
1 X << Y 8 2 8 * pow(2,2) 32
2 X << Y 2 4 2 * pow(2,4) 32
3 X << Y 3 2 3 * pow(2,2) 12


Right Bit Shift
# Operation X Y Resolution Result
1 X >> Y 64 4 64 / pow(2,4) 4
2 X >> Y 4 2 4 / pow(2,2) 1
3 X >> Y 8 2 8 / pow(2,2) 2

So, that is all. Hope i have made the use of logical shift alot clearer to you.


Did you know? You can fetch multiple Eloquent records with one command

The Eloquent class in Laravel has a method called find, this you must be aware of , you can use this to find an Eloquent Model record using the primary key. This is used as follows

// Retrieve a model record by its primary key...
$car = App\Car::find(1);

Now, what if we want to find all cars with primary key in the list [1,2,3]? You can suppluy an array of primary keys to the find method or use the findMany method. Consider the code snippet below

$cars = App\Car::find([1, 2, 3]);
$cars = App\Cr::findMany([1, 2, 3]);


Note: If you supply an array of keys to the find method, it calls findMany internally. Whichever you decide to use between the two methods described above, they both return an instance of  \Illuminate\Database\Eloquent\Collection.




Highlighting Active Bootstrap Menu Items (Nav – Item) Based On Route

In this post, I will show you a niifty little trick, one that is always required whenever you are working on some sort of web project with multiple routes.

You occasionally need to set the nav-item that inidicates the current route to be active (i.e. make it coloured ), to achieve this using laravel, consider the code snippet below

<ul class="navbar-nav">
<li class="nav-item {{ is_active('') }}">
    <a class="nav-link" href="{{ route('') }}">Route One</a></li>
<li class="nav-item {{ is_active('') }}">
    <a class="nav-link" href="{{ route('') }}">Route Two</a></li>
<li class="nav-item {{ is_active('') }}">
    <a class="nav-link" href="{{ route('') }}">Route Three</a></li>

As you can see from the snippet displayed above, for every <li> element, the is_active function call has been added to class attribute , this function takes the name of a route as its argument and it returns a class name (active) if the current route name matches that which was supplied. Consider the snippet below to see the logic in the function stored in a file called helpers.php in a new folder called custom (note:  you can store your helper file anywhere you deem logical, I have just chosen custom/helpers.php).

if (!function_exists('is_active')) {
     * @param string $routeName
     * @param string $activeClassName
     * @return string
    function is_active(string $routeName, string $activeClassName = 'active')
        $result = '';
        if (request()->route()->getName() === $routeName) {
            $result = $activeClassName;

        return $result;

How do you make your function available globally?

To make the function in the [core/helpers.php] file available globally, you need to autoload it using the files attribute of the composer.json file as follow.

"autoload": {
    "classmap": [
    "psr-4": {
      "App\\": "app/"
    "files": [

Hope you have found this helpful..

How to extend the laravel Collection with a macro

At this point in time, if you are familiar with the Laravel framework, I am sure you must have used the lovely Collections class. It is one of my favourite features of the laravel framework, so much so that I use in some bespoke projects outside laravel.

What is Laravel Collections?

It is nothing but php arrays on steroids. It allows you to perform very cool manipulations and processing of php arrays.

How do you create a collection?

$collectionInstance = collect([1,2,3,4,5,6,7]);

$collectionInstance2 = \Illuminate\Support\Collection::make([1,2,3,4,5,6,7]);

The collection class has a hosts of methods , such as

  1. avg
  2. sum
  3. unique
  4. and a lot more

However, sometimes you wish to use a method on the collection class and it isn’t available, so how do you do it? Collection Macros to the rescue.

Creating a macro to convert all items of a collection to camel case.

Consider, if you wish to convert all the strings in a collection to their respective camel case values, and there isn’t one method to do this. Obviously you can use a map or transform to achieve this, but for convenience sake, lets create a macro.

 Collection::macro('camelise', function(){
    return $this->map(function ($value) {
        return camel_case($value);

// to use it
$items = ['foo_bar', 'hello_world'];
$expectedValues = ['fooBar', 'helloWorld'];
$camelValues = collect($items)->camelise();

// assert that result matches the expected values.
$arraysAreEquals = $expectedValues === $camelValues->toArray();


Where do I put the macros?
As helpful as this is, imagine if you have a 100 of such methods, where do you keep them?

Now this is a subjective answer, you can extract your methods into files and require them from a loop of the glob of the location of these files or you create a class with your macros and you use the Php Reflection Class to loop through them and add them to the Collection class.

Also, the AppServiceProvider will be a good point to add the logic described above.



Fetching Laravel Eloquent results ordered by oldest to newest

In the previous post, I showed you how to order you results from newest to oldest using Eloquent model.

It is only logical to show you how to reverse the operation, ordering you results from the oldest to the newest. As always, Laravel in its majesty has already created a method for this and guess what it is called? oldest, so obvious right?

Consider the code below


This will return the results already ordered by created_at from oldest to newest.