@@ -52,9 +52,9 @@ Objects.equals(null,"SnailClimb");// false
5252我们看一下` java.util.Objects#equals ` 的源码就知道原因了。
5353``` java
5454public static boolean equals(Object a, Object b) {
55- // 可以避免空指针异常。如果a==null的话此时a.equals(b)就不会得到执行,避免出现空指针异常。
56- return (a == b) || (a != null && a. equals(b));
57- }
55+ // 可以避免空指针异常。如果a==null的话此时a.equals(b)就不会得到执行,避免出现空指针异常。
56+ return (a == b) || (a != null && a. equals(b));
57+ }
5858```
5959
6060** 注意:**
@@ -104,14 +104,18 @@ System.out.println(a == b);// false
104104BigDecimal a = new BigDecimal (" 1.0" );
105105BigDecimal b = new BigDecimal (" 0.9" );
106106BigDecimal c = new BigDecimal (" 0.8" );
107- BigDecimal x = a. subtract(b);// 0.1
108- BigDecimal y = b. subtract(c);// 0.1
109- System . out. println(x. equals(y));// true
107+
108+ BigDecimal x = a. subtract(b);
109+ BigDecimal y = b. subtract(c);
110+
111+ System . out. println(x); /* 0.1 */
112+ System . out. println(y); /* 0.1 */
113+ System . out. println(Objects . equals(x, y)); /* true */
110114```
111115
112116### 1.3.2. BigDecimal 的大小比较
113117
114- ` a.compareTo(b) ` : 返回 -1 表示小于 ,0 表示 等于 , 1表示 大于 。
118+ ` a.compareTo(b) ` : 返回 -1 表示 ` a ` 小于 ` b ` ,0 表示 ` a ` 等于 ` b ` , 1表示 ` a ` 大于 ` b ` 。
115119
116120``` java
117121BigDecimal a = new BigDecimal (" 1.0" );
@@ -167,7 +171,7 @@ Reference:《阿里巴巴Java开发手册》
167171` Arrays.asList() ` 在平时开发中还是比较常见的,我们可以使用它将一个数组转换为一个List集合。
168172
169173``` java
170- String [] myArray = { " Apple" , " Banana" , " Orange" };
174+ String [] myArray = {" Apple" , " Banana" , " Orange" };
171175List<String > myList = Arrays . asList(myArray);
172176// 上面两个语句等价于下面一条语句
173177List<String > myList = Arrays . asList(" Apple" ," Banana" , " Orange" );
@@ -177,8 +181,9 @@ JDK 源码对于这个方法的说明:
177181
178182``` java
179183/**
180- *返回由指定数组支持的固定大小的列表。此方法作为基于数组和基于集合的API之间的桥梁,与 Collection.toArray()结合使用。返回的List是可序列化并实现RandomAccess接口。
181- */
184+ *返回由指定数组支持的固定大小的列表。此方法作为基于数组和基于集合的API之间的桥梁,
185+ * 与 Collection.toArray()结合使用。返回的List是可序列化并实现RandomAccess接口。
186+ */
182187public static < T > List<T > asList(T . .. a) {
183188 return new ArrayList<> (a);
184189}
@@ -197,20 +202,20 @@ public static <T> List<T> asList(T... a) {
197202` Arrays.asList() ` 是泛型方法,传入的对象必须是对象数组。
198203
199204``` java
200- int [] myArray = { 1 , 2 , 3 };
205+ int [] myArray = {1 , 2 , 3 };
201206List myList = Arrays . asList(myArray);
202207System . out. println(myList. size());// 1
203208System . out. println(myList. get(0 ));// 数组地址值
204209System . out. println(myList. get(1 ));// 报错:ArrayIndexOutOfBoundsException
205- int [] array= (int []) myList. get(0 );
210+ int [] array = (int []) myList. get(0 );
206211System . out. println(array[0 ]);// 1
207212```
208213当传入一个原生数据类型数组时,` Arrays.asList() ` 的真正得到的参数就不是数组中的元素,而是数组对象本身!此时List 的唯一元素就是这个数组,这也就解释了上面的代码。
209214
210215我们使用包装类型数组就可以解决这个问题。
211216
212217``` java
213- Integer [] myArray = { 1 , 2 , 3 };
218+ Integer [] myArray = {1 , 2 , 3 };
214219```
215220
216221** 使用集合的修改方法:` add() ` 、` remove() ` 、` clear() ` 会抛出异常。**
@@ -296,7 +301,7 @@ static <T> List<T> arrayToList(final T[] array) {
296301 for (final T s : array) {
297302 l. add(s);
298303 }
299- return (l) ;
304+ return l ;
300305}
301306```
302307
@@ -344,6 +349,14 @@ List<String> list = new ArrayList<String>();
344349CollectionUtils . addAll(list, str);
345350```
346351
352+ ** 6. 使用 Java9 的 ` List.of() ` 方法**
353+ ``` java
354+ Integer [] array = {1 , 2 , 3 };
355+ List<Integer > list = List . of(array);
356+ System . out. println(list); /* [1, 2, 3] */
357+ /* 不支持基本数据类型 */
358+ ```
359+
347360## 2.2. Collection.toArray()方法使用的坑&如何反转数组
348361
349362该方法是一个泛型方法:` <T> T[] toArray(T[] a); ` 如果` toArray ` 方法中没有传递任何参数的话返回的是` Object ` 类型数组。
@@ -365,6 +378,16 @@ s=list.toArray(new String[0]);//没有指定类型的话会报错
365378
366379> ** fail-fast 机制** :多个线程对 fail-fast 集合进行修改的时,可能会抛出ConcurrentModificationException,单线程下也会出现这种情况,上面已经提到过。
367380
381+ Java8开始,可以使用` Collection#removeIf() ` 方法删除满足特定条件的元素,如
382+ ``` java
383+ List<Integer > list = new ArrayList<> ();
384+ for (int i = 1 ; i <= 10 ; ++ i) {
385+ list. add(i);
386+ }
387+ list. removeIf(filter - > filter % 2 == 0 ); /* 删除list中的所有偶数 */
388+ System . out. println(list); /* [1, 3, 5, 7, 9] */
389+ ```
390+
368391` java.util ` 包下面的所有的集合类都是fail-fast的,而` java.util.concurrent ` 包下面的所有的类都是fail-safe的。
369392
370393![ 不要在 foreach 循环里进行元素的 remove/add 操作] ( https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019/7/foreach-remove:add.png )
0 commit comments