Posts

Mystry of Java String.intern

Mystery of Java String.intern We have all learn about how Java has optimised handling string objects. The String class has been made immutable and string literal definition makes sure that existing instance from string pool will be returned instance creating new one. Here is simple code snippet that explains it String firstName = "John"; String firstNameWithNew = new String("John"); String duplicateFirstName = "John"; //All there strings are same System.out.println(firstName.equals(duplicateFirstName) && firstName.equals(firstNameWithNew));//=> true //Since they defiend using string iterals, even there memory references are same System.out.println(firstName == duplicateFirstName);//=> true //Since one of the object has defined explictly using new operator, there memory references are different. System.out.println(firstName == firstNameWithNew);//=> false This is very basic understanding that everyone has. Then I came across String.in...

Table sorting accessibility for AngularJS table/list

Has your SPA accessibility compliance done ? Ahhh … let me get back to you. That’s usual scenario for dynamic single page application. Since content of the web app changes dynamically/asynchronously. It required need addition planning to make dynamic web accessibility complain. For those who are new to web accessibility The power of the Web is in its universality. Access by everyone regardless of disability is an essential aspect. There are many tools are being provided to make the web accessible like screen reader. The screen reader read out the html content with help of aria attributes. One of the accessibility attribute is aria-sort . It let the user know that the given html table is sorted by of the its column and in which order. The sorted column of the table expected to have ng-aria=”ascending/descending”. The problem with SPA table is that on sorting the table the content get sorted without loading the web, so having static aria-sort attribute is going t...

Management lesson from The Bridge Riddle

Most of us come across the Bridge Riddle sometime before, it is all about optimizing time to cross the bridge. Here is the riddle You, an assistance, a jenitor and a scientist have to cross a wooden bridge in 17 minutes . The bridge is weak and only able to carry the weight of two of them at a time. Because they are in a rush and the light is fading they must cross in the minimum time possible and must carry a torch (flashlight,) on each crossing. They only have one torch and it can’t be thrown. Because of their different fitness levels and some minor injuries they can all cross at different speeds. You can cross in 1 minute, assistance in 2 minutes, the janitor in 5 minutes and the scientist in 10 minutes. Below TED Ed video explains and solve this riddle https://www.youtube.com/watch?v=7yDmGnA8Hw0 As explained in the video the key decision is to reply on the assistant who is comparatively less efficient than “You”, the assistant takes double time t...

All about Java stream collect offering

Lets start with a problem statement “Provided collection of employees, we need to find average, max and min age of employee by county” public static void avgMaxMinEmployeeAgeByCounter() { String AVG = "AVG"; String MAX = "MAX"; String MIN = "MIN"; String SUM = "SUM"; String COUNT = "COUNT"; List<Employee> employees = Employee.EMPLOYEES;//LIst of all employees, pojos // Mapping country name to average, max, min age of employee Map<String, Map<String, Double>> summary = new HashMap<>(); for(Employee e: employees) { if(summary.containsKey(e.getCountry())){ Map<String, Double> map = summary.get(e.getCountry()); map.put(SUM, (map.get(SUM) + e.getAge())); map.put(COUNT, map.get(COUNT) +1); map.put(MAX, map.get(MAX) < e.getAge() ? e.getAge():map.get(MAX)); map.put(MIN, map.get(MIN) > e.getAge() ? e.ge...

Utilizing ES6 Generator

I am going through Mozilla documentation about the Iterator and Generator concept introduced in ECMAScript6. Here is the overview as per Mozilla JS guide Processing each of the items in a collection is a very common operation. JavaScript provides a number of ways of iterating over a collection, from simple for loops to map() and filter(). Iterators and Generators bring the concept of iteration directly into the core language and provide a mechanism for customizing the behavior of for…of loops. This post is not about how it works, it is better explained at Mozilla JS guide . I have tried to apply the JS generator for one of the our day to day code. I think it is better suitable for lazy traversing and evaluating each item and proceed further if required. A common example comes in my mind is the tree traversing. I have implemented Depth First Search ( pre-order) using generator. Consider the scenario where you need to traverse the tree node, evaluate/process t...

Looping & Closure

Image
Here is simple code to dynamically create set of buttons that greet in different languages. document.body.onload = createButtons; function createButtons() { var greetings = [ {lang:'English', msg:'Good Morning !!'}, {lang:'German', msg:'Guten Morgen !!'}, {lang:'French', msg:'Bonjour !!'}, {lang:'Finnish', msg:'Huomenta !!'} ]; document.createElement('h1'); console.log(greetings); for(var i=0;i<greetings.length; i++){ var greet = greetings[i] var input = document.createElement("button"); input.appendChild(document.createTextNode(greet.lang)); input.setAttribute('class','btn btn-default btn-lg'); input.onclick = function(event){ alert(greet.msg); }; document.body.appendChild(input); } } Pretty simple, looping through array of objects and adding buttons with lan...

NPE as well as Null check free code ... Really ?

Image
NPE as well as Null check free code … Really ? Is this how your Java code review start ? NPE is always nightmare for a Java developer. Lets not discussion to much and jump on an usual Java code snippet public interface Service { public boolean switchOn(int timmer); public boolean switchOff(int timmer); //Other controls } public class RefrigeratorService implements Service { // ... } public class HomeServices { private static final int NOW = 0; private static HomeServices service; public static HomeServices get() { //Null Check #1 if(service == null) { service = new HomeServices(); } return service; } public Service getRefrigertorControl() { return new RefrigeratorService(); } public static void main(String[] args) { /* Get Home Services handle */ HomeServices homeServices = HomeServices.get(); //Null Check #2 if(homeServices != null) { Serv...