使用 HashMap 的方法 :

computeIfAbsent(K key, Function remappingFunction)

其中 remappingFunction 是一個 Functional interface

  • input 為 map 的 key
  • output 會成為 map 的 value

HashMap 的 computeIfAbsent 方法,在 key 不存在時,會做 remappingFunction 的操作,所以再也不會因為漏寫 if x == null 而出現空指針的 bug 了。

Continue reading

刷題時很常出現 Array 的結構如 int[]、char[] 等等…,故在這邊條列一些常用的 Arrays 方法。這次主要整理下 Java 中 Arrays 類的常用方法,在使用過程也可以複習 java 提供的工具類,還有一些泛型的坑…

Continue reading

把 List of array 轉成 2D-array

List<T[]> list = new ArrayList();
// 記得 list.size()
// 後面還有個[]
T result[][] = list.toArray(new T[list.size()][]);

// 實際 example
List<int[]> listOfIntegers = List.of( new int[] { 1, 2 }, new int[] { 3, 55, 65 } );
int[][] array2D = listOfIntegers.toArray( new int[listOfIntegers.size()][] );

Continue reading

Author's picture

李昀陽 YunYang Lee

Welcome to my Tech Note. You can read some of the chapters below.

Software Engineer

Taiwan