Java碎碎念
收集些容易忽视的Java的用法
Java
Collections Interface
List
ArrayList
LinkedList
LinkedList Methods:
Methods | Description |
---|---|
addFirst() | Adds an item to the beginning of the list. |
addLast() | Add an item to the end of the list |
removeFirst() | Remove an item from the beginning of the list. |
removeLast() | Remove an item from the end of the list |
getFirst() | Get the item at the beginning of the list |
getLast() | Get the item at the end of the list |
Deque
LinkedList 实现
ArrayDeque 实现
总结
其实就是array和linkedlist区别,大部分时间里array会比较快。但是当添加元素时超了array的容积,则ArrayDeque需要resize.
Stream()
- List to array one line:
java
1 | List.stream().mapToInt(i -> i).toArray(); |
- Print elements in an int[]:
java
1 | Arrays.stream(ret).forEach(a -> System.out.print(a + " ")); |
Reduce()
有三个组成元素:
identity, accumulator, combiner:
- Identity – an element that is the initial value of the reduction operation and the default result if the stream is empty
- Accumulator – a function that takes two parameters: a partial result of the reduction operation and the next element of the stream
- Combiner – a function used to combine the partial result of the reduction operation when the reduction is parallelized or when there’s a mismatch between the types of the accumulator arguments and the types of the accumulator implementation
java
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6); |
0 is
identity
subtotal, element -> subtotal + element is
accumulator
Pair
java
1 | Pair<Integer, String> pair = new Pair<>(1, "One"); |
Comparator
在Arrays.sort中使用 Comparator:
java
1 | Arrays.sort(costs, new Comparator<int[]>(){ |
在Collections.sort中使用:
java
1 | Collections.sort(costs, new Comparator<List<Integer>>(){ |
和Map.Entry
结合在一起:
java
1 | List<Map.Entry<Character, int[]>> ls = new ArrayList<>(hm.entrySet()); |
通过位运算来互换大小写
大写字符与其对应的小写字符的 ASCII 的差为 32
也就是 1<<5
所以:
变换大小写这件事等价于:
- 如果字符是小写字符,减去 32 得到大写字符;
- 如果字符是大写字符,加上 32 得到小写字符。
而这两者合并起来,就是给这个字符做一次不进位的加法,即异或上 1 << 5。
为什么两者合并起来相当于一个不进位的加法?
考虑一下异或操作的特性:
- 0 XOR 0 = 0
- 1 XOR 0 = 1
- 0 XOR 1 = 1
- 1 XOR 1 = 0
这与不进位加法的规则相同,即:
- 0 + 0 = 0
- 1 + 0 = 1
- 0 + 1 = 1
- 1 + 1 = 0 (不考虑进位)
现在,考虑ASCII值。大写字母与其对应的小写字母的ASCII值之间的差异在第5位上。例如:
- ‘A’ = 65 = 1000001 (二进制)
- ‘a’ = 97 = 1100001 (二进制)
请注意第5位的差异。要从’A’转到’a’,我们需要将第5位从0变为1,这可以通过加32(即1左移5位)来实现。反之亦然。
当我们异或一个数字时,我们实际上是在为该数字的每一位执行不进位加法。因此,当我们对字符值进行异或操作
1 << 5
时,我们实际上是在进行以下操作:
- 如果第5位是0(即该字符是大写字母),我们将其加上1,从而将该字符转换为小写字母。
- 如果第5位是1(即该字符是小写字母),我们将其加上0,从而将该字符转换为大写字母。
因此,异或操作实际上是实现不进位加法的一种快速方法。在这种情况下,它可以用来在大写和小写字符之间进行快速切换。
代码:
java
1 | cArr[startIdx] ^= 1 << 5; |
使用此技巧的例题
784. 字母大小写全排列
这个题在 从二叉树到回溯到DP 中
java
1 | class Solution { |
通过左括号指针判断括号是否合法:
思路是用代表左括号的指针移动,碰到左括号右移,右括号左移,如果left == 0证明合法,否则比如左括号的idx < 0则代表右括号>左括号数量,不合法。
java
1 | private boolean isValid(String sb) { |
使用此技巧的例题
301. 删除无效的括号
java
1 | class Solution { |
StringBuilder
java
1 | sb.append(c); |