Java List
这是我参与11月更文挑战的第1天,活动详情查看:2021最后一次更文挑战
Day1 概述
简单介绍一下Java 集合,本篇主要讲解一下List中的一些方法
Java 框架图
关于Java集合的重要性而言对于从事Java开发同仁来说就不言而喻吧,反正我大概三年的开发来说,就目前处于的公司,对于性能上已经选择了mongoDB的数据库,往往更复杂的逻辑是在业务段处理上,那么对于Java的数据容器的使用肯定是比较多的,那么不多扯皮,进入正题。Java集合框架图,如下
图片源自 菜鸟教程
日常使用最多的几个:List Map Set
List
源码中的定义:
遇见过的一些小问题
一般在实际开发过程中可能会涉及使用 Arrays.asList("1", "2"); 这种方式建立一些数组啥的,但是切记这个方法建立的数据是不能够对该元素进行操作的比如 remove ;
retainAll 比较鸡肋的一个方法
方法定义:
/**
* Retains only the elements in this list that are contained in the
* specified collection (optional operation). In other words, removes
* from this list all of its elements that are not contained in the
* specified collection.
*
* @param c collection containing elements to be retained in this list
* @return <tt>true</tt> if this list changed as a result of the call
* @throws UnsupportedOperationException if the <tt>retainAll</tt> operation
* is not supported by this list
* @throws ClassCastException if the class of an element of this list
* is incompatible with the specified collection
* (<a href="Collection.html#optional-restrictions">optional</a>)
* @throws NullPointerException if this list contains a null element and the
* specified collection does not permit null elements
* (<a href="Collection.html#optional-restrictions">optional</a>),
* or if the specified collection is null
* @see #remove(Object)
* @see #contains(Object)
*/
boolean retainAll(Collection<?> c);
测试代码:
private static void retainAll(){
List<String> l1 = new ArrayList<>();
l1.add("1");
l1.add("2");
List<String> l2 = new ArrayList<>();
l2.add("2");
l2.add("3");
List<String> l3 = new ArrayList<>();
l3.add("5");
l3.add("6");
List<String> l4 = new ArrayList<>();
l4.add("1");
l4.add("2");
List<String> l5 = new ArrayList<>();
l5.add("1");
l5.add("2");
l5.add("3");
System.out.println(l1.retainAll(l2));
l1.forEach(System.out::println);
System.out.println(l2.retainAll(l3));
l2.forEach(System.out::println);
System.out.println(l4.retainAll(l5));
l4.forEach(System.out::println);
}
结论
retainAll() 方法用于保留 arraylist 中在指定集合中也存在的那些元素,也就是删除指定集合中不存在的那些元素; 如果 arraylist 中删除了元素则返回 true。 如果 arraylist 类中存在的元素与指定 collection 的类中元素不兼容,则抛出 ClassCastException 异常。 如果 arraylist 包含 null 元素,并且指定 collection 不允许 null 元素,则抛出 NullPointerException
subList 这个是用来截取数组的,在定义中就说了不包含后面的那一个元素哦!
/**
* Returns a view of the portion of this list between the specified
* <tt>fromIndex</tt>, inclusive, and <tt>toIndex</tt>, exclusive. (If
* <tt>fromIndex</tt> and <tt>toIndex</tt> are equal, the returned list is
* empty.) The returned list is backed by this list, so non-structural
* changes in the returned list are reflected in this list, and vice-versa.
* The returned list supports all of the optional list operations supported
* by this list.<p>
.
.
.
sort 这个方法源码定义中提到了一定得实现了类似 CompareAble 或者 行内lamda 比较对象的方法,对于基本数据类型有些是存在自我实现的,所以不需要,对象比较必须要重写hashCode 以及equals 方法这里衍生一点,单独重写equals时不重写hashcode对象比较时可能会导致某些数据在碰撞的时候会代码认为一致而导致数据去重等操作误删数据。
/**
* Sorts this list according to the order induced by the specified
* {@link Comparator}.
*
* <p>All elements in this list must be <i>mutually comparable</i> using the
* specified comparator (that is, {@code c.compare(e1, e2)} must not throw
* a {@code ClassCastException} for any elements {@code e1} and {@code e2}
* in the list).
.
.
.
ArrayList
初始化数组量 10,数组列表,属于无序的 ,查询速度比较快
LinkedList
初始化数组量 10,类似带'指针'有序数组列表 ,插入速度快,查询慢
Vector
线程安全的 数组列表 源码中它的方法大部分实用方法都使用了 synchronized 锁
End
希望自己能坚持更新吧,后续将会继续Java集合的介绍 map set .