Category Archives: java

Supplier memoization

Below code is a simple way for memoization: private String value; public String getValue() { if (value == null) { value = “abcd”; } return value; } This can be also wrote in Guava way: Supplier<String> memo = Suppliers.memoize(() -> { return “abcd”; // inside function }); System.out.println(memo.get()); // will call the function System.out.println(memo.get()); //… Read More »

Create ExecutorService and ListeningExecutorService

Executor is a interface, there is no way to create it. Normally, there are 2 ways to create ExecutorService: ExecutorService service = Executors.newFixedThreadPool(10); ExecutorService service = Executors.newSingleThreadExecutor(); Normally, use decorator to create ListeningExecutorService by ExecutorService: ListeningExecutorService service = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(10)); Compared to Executor, ExecutorService has shutdown function. ExecutorService returns Future, ListeningExecutorService returns ListenableFuture. Compared to Future, ListenableFuture can… Read More »

submit, execute, callable, runnable

ExecutorService and ListeningExecutorService has submit() and execute() funciton: Future<T> submit(Callable<T> task) Future<?> submit(Runnable task) void execute(Runnable command) Both ExecutorService and ListeningExecutorService can submit a Runnable and Callable parameter. Submit a Callable: public static void submitCallable() throws Exception{ ExecutorService executorService = Executors.newSingleThreadExecutor(); Callable<String> r = () -> { return “abcd”; }; Future<String> future = executorService.submit(r); System.out.println(future.get());… Read More »

Stop ExecutorService

Look at this code, it won’t stop: public static void main(String[] args) throws Exception{ ExecutorService executorService = Executors.newSingleThreadExecutor(); Runnable r = () -> { System.out.println(1); }; executorService.submit(r); } Main reason is that it didn’t call executorService.shutdown(); Exector is a simple interface. It only has a execute() function. ExecutorService has shutdown() function, which can stop the… Read More »

Lambda Generic

This is a piece of code to use lambda to implement the generic type interface. public class LambdaGeneric { public static void main(String[] args) { test1(); } public static void test1() { A a = () -> {}; B b = (b1, b2) -> {}; b.apply(“hello”, 1); // b.apply(2, 1); won’t pass because the type… Read More »

Arrays.asList(), Array to List

This is a summary for Arrays.asList() and transformation from Array to List. See below examples and comments. public static void test1() { boolean[] b = new boolean[10]; // wrong, Arrays.asList doesn’t accept array of primitive type List<Boolean> list = Arrays.asList(b); } public static void test2() { Boolean[] b = new Boolean[10]; // correct, because it… Read More »

Linked List Cycle II

leetcode 142. Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Solution. First of all, we know that we can use slow / fast pointer to check if it has cycle. Assume they meet at point Z. We know slow walks distance of a + b,… Read More »

A decent gcd code

This is a very decent code to calculate gcd. I found this when I was doing the water and jug problem. public static int gcd(int x, int y) { while (y != 0) { int tmp = y; y = x % y; x = tmp; } return x; }

Lambda study summary

Below code is a basic way to self-define a lambda. Basically, it requires: 1. a interface with only one function 2. a method which has the interface as parameter, and in the method, it will call the function in that interface implementation. public static void simpleWay() { Math add = new Math() { @Override public int… Read More »

SpringMVC Restful Service

When I tried to use SpringMVC to design a restful web service, it showed this error message: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. After a long journey of the debug, I found that in SpringMVC, we should setup “Access-Control-Allow-Origin” to “*” in reponseHeader. In order to do this, first in web.xml, we should… Read More »