Old Java
String[][] names = { { "John", "Mary", "Steven" }, { "Tom", "Jeff" } };
List<String> list1 = new ArrayList<String>();
for (String[] s1 : names) {
for (String s2 : s1) {
list1.add(s2);
}
}
Java 8 Stream API
String[][] names = { { "John", "Mary", "Steven" }, { "Tom", "Jeff" } };
List<String> list2 = Arrays.stream(names)
.flatMap(s1 -> Arrays.stream(s1))
.collect(Collectors.toList());
C# LINQ (method syntax)
string[][]...
11/04/2014
8/26/2014
Docker
Docker 是一個開源的軟體容器(software containers), 利用 Linux kernel 中的隔離(isolation)特性, 實現了OS level virtualization
目前常見虛擬化技術類型(Virtualization Types)
PV(paravirtualization), 透過修改OS核心,植入Hypercall,讓原本無法被虛擬化的指令,可以經過 Hyper interface 直接向硬體提出請求
http://en.wikipedia.org/wiki/Paravirtualization
HVM(hardware virtual machine)http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/virtualization_types.html
Application level virtualization - JVM, CLR
OS level virtualization - docker
Hypervisor 有兩種類型
Type...
PLSQL Developer & SQL Navigator password 備份
PLSQL Developer 儲存的 password 預設存在 C:\Users\[username]\AppData\Roaming\PLSQL Developer\Preferences\[username]\user.prefs或安裝目錄下的 \PLSQL Developer\Preferences\[username]\user.prefs自訂路徑 可修改安裝目錄下的 \PLSQL Developer\params.ini 並且定義 prefpath 參數
SQL Navigator
password 儲存在 HKEY_CURRENT_USER\Software\Quest Software\SQL Navigator ?.?.?\Logon Password...
6/17/2014
Named and Optional Arguments in C#, Python, JavaScript
C#
C# 在使用具名引數上非常方便, function 不需做任何修改即可使用
public double CalculateBMI(double height, double weight, bool isPrint = true)
{
double heightMeter = height / 100D;//double suffix
double bmi = weight / Math.Pow(heightMeter, 2);
//double bmi = Math.Floor(weight / Math.Pow(heightMeter, 2) * 100) / 100;
if (isPrint) Console.WriteLine("BMI=" + bmi);
return bmi;
}
傳入參數的方式使用類似 JSON 的表示法, 且傳入不需有順序性
CalculateBMI(height: 170, weight: 65);
CalculateBMI(height: 170, weight:...
Variable-length Argument in Java, C#, Python
Java
public int sum(int... nums)
{
//nums.getClass().getTypeName() -> int[]
int total = 0;
for (int num : nums )
total += num;
return total;
}
C#
public int Sum(params int[] nums)
{
//nums.GetType() -> System.Int32[]
int total = 0;
for (int i = 0; i < nums.Length; i++)
total += nums[i];
return total;
}
Python
def sum(*nums):
#type(nums) -> <class 'tuple'>
total = 0
for num in nums:
total...
5/27/2014
Apache Commons Logging 整合 java.util.logging
設定 commons logging 使用 java.util.logging
在應用程式的 classpath 中加入 commons-logging.properties 檔案
commons-logging.properties 內容如下,作用是要 commons.logging 使用 Jdk14Logger
org.apache.commons.logging.Log=org.apache.commons.logging.impl.Jdk14Logger
若不使用 commons-logging.properties, commons logging 會先搜尋 classpath 中是否存在 log4J , 若存在 log4J 則優先使用 log4J, 若不存 log4J 才會使用 java.util.logging, 順序可參考 org.apache.commons.logging.LogSource
建立 java.util.logging 設定檔
建立設定檔 logging.properties, 檔名可自訂
handlers=...
5/22/2014
Java Type Erasure
Java 的泛型其實是在編譯器上做了手腳,Java 在編譯時期由編譯器幫我們做了型別轉換的檢查,當 compile 成 bytecode 後,我們使用工具觀察,可以發現我們在程式中所使用的泛型全都不見了,也因此程式在 run time 時是看不到泛型的影子,這種泛型的實現方式稱作 type erasure, 其優點是確保了相容性, 而 .NET 的泛型就不一樣了, .NET 的泛型是真正運行於 CLR 層上的功能, 也因此 Java 的泛型與 .NET 相比, Java 多了許多限制
舉個例子,假設我們要實做一個 List, 當索引值超出 List 長度時, 回傳傳入泛型型別的實例
C#
public class Animal
{
public override String ToString()
{
return this.GetType().Name;
}
}
public class Cat : Animal {}
public class Dog : Animal {}
public class...
Subscribe to:
Posts (Atom)