Skip to content

Instantly share code, notes, and snippets.

@avence12
Last active April 17, 2016 14:18
Show Gist options
  • Save avence12/15fcdad1d371e2422b2a5d418784c35c to your computer and use it in GitHub Desktop.
Save avence12/15fcdad1d371e2422b2a5d418784c35c to your computer and use it in GitHub Desktop.
Code of Java Interview Question - http://www.lightblue.asia/java-interview-questions
//Example 1: new Runnable
Runnable run = new Runnable() {
public void run() {
System.out.println("run me!");
}
};
// one line lambda
Runnable run = () -> System.out.println("run me!");
// Exmaple 2: Iterate Map
Set keySet = map.keySet();
for (String s : keySet) {
System.out.print(s + ":" + map.get(s));
}
// one line lambda
map.forEach((k, v) -> System.out.print(k + ":" + v));
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("5");
list.add("4");
list.stream().filter(s -> Integer.valueOf(s) < 3).forEach(s -> System.out.print(s));
public class MySingleton{
private static MySingleton uniqueInstance;
private MySingleton(){} // 使用Private以確保mySingleton 只能透過 API:getInstance() 產生Instance
public static synchronized MySingleton getInstance() {
if(uniqueInstance == null ) {uniqueInstance = new mySingleton();}
return uniqueInstance;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment