スキップしてメイン コンテンツに移動

投稿

7月, 2014の投稿を表示しています

Java 8: Reduce Redundant Code Using Lambda Expression

Redundant Code Problem If you are working on mathematical program, you might have written code for creating new Function object from source function object. For example, creating new function "g(x)" which is defined as "f(x) - target" Before Java 8, this kind of creating new function object needs redundant code as below: // using anonymous inner class public final static Function subtract(Function f, double target) { return new Function() { @Override public double f(double x) { return f.f(x) - target; } }; } // function interface definition public interface Function { double f(double x); } Of course, the code is perfect but the redundant part "new Function()..." prevent developers understanding core logic part quickly. Lambda Expression Now in Java 8, "lambda expression" helps reducing this kind of redundant code. See below code which uses lambda expression. public final stat

Escape Html by Javascript

When I copy and paste some code snippet on this blog, I have to escape some special characters for example "<", ">", etc. I googled how to escape html special characters by javascript and found escaping-html-strings-with-jquery . Based on the answer, I created simple html escape tool below. copy & paste text in this text area, then html escaped text will be output text area below. The whole javascript code is below! <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script> <script> $(function(){ $("#src_html").keyup(function(){ $("#dest_html").val(escapeHtml($("#src_html").val())); }); var entityMap = { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': '&quot;', "'": '&#39;', "/": '&#x2F;' }; func

Java 8 Lamnda: Print All Elements in Collection

When you write a program, I think sometimes you would like to prontall elements in collection for debugging purpose. Before Jva 7 we need to wrote small code snippet for iterating collection and printing out its element. However in Java 8 you can write this kind of code quite quickly. Here is an example for printing out all system properties. System.getProperties().entrySet().stream().forEach(System.out::println); I think the advantedge of this code is readability and writability - You can read or write code just from left to right :D

Java 8 Lambda : Comparator

Java 8 makes your code much more readble and simpler. In this post, I will show you Comparator example. Beofre Java 8 I always had to write and put utility comparator for String in my program. Annoying... package com.dukesoftware.utils.common; import java.util.Comparator; public enum StringComparator implements Comparator<String> { Instance; public int compare(String o1, String o2) { if(o1 == nul){ if(o2 == null) return 0; return -1; } return o1.compareTo(o2); } } Of course you can use Java Commons library, but it is too much to add jar to buildpath only for small code snippet. Java 8 Lambda Expression See this code! Quite short! Comparator.nullsFirst(Comparator.comparing(String::toString));

Java 8 Lambda Expression

I am very excited about new Java 8 features, especially lambda expression. I am jealous that C# had lambda expression when it was introduced in C# 3.0 loooong time ago. Let me show you a power of lambda expression by simple code example - FileFiter. Why I choose this example? Well, I have been so frustrated that I always had to write a bit redundant code even if for very simple FileFilter. Please see following example code. Hope the example attracts you... package com.dukesoftware.io; import java.io.File; import java.io.FileFilter; public class FileFilters { // legacy file filter - no lambda expression public static final FileFilter DIRECTORY_FILTER_BEFORE_JAVA8_NO_LAMBDA = new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }; // basic lambda expression introduced since java 8 public static final FileFilter DIRECTORY_FILTER_JAVA8_LAMBDA_FIRST = (File pathname) -&g

6 Things You Shoud Check for MySQL Query Performance

In this post, I will explain some performance tuning tips for MySQL query. 1) Don't use wildcard (e.g. *) in SELECT (=Pick only required column on SELECT). 2) Comare performance between with and without 'JOIN'. Sometimes JOIN helps MySQL engine will reduce examined number of rows. But in my exeprience, most of the case, removing LEFT JOIN makes performance improvement. 3) Comare performance between with and without 'ORDER BY'. 4) Comare performance between with and without 'SQL_CALC_FOUND_ROWS'. 5) Check INDEX is properly used for JOIN, GROUP BY, ORDER BY. 6) Compare performance between following 2 strategy. a) pick up all required columns (sometimes with ORDER BY, DISTINCT, LIMIT etc.) by single query b) pick only required id column, and then pull all required columns using JOIN or execute 2nd query based on id columns picked up from the 1st query. Okay the last 6) might need example. 6) says " comapre performance following a)