Arrays.asList()
关于:Collection coll1 = Arrays . asList ( 123 , 4567 ) ; 1 、
Arrays . asList ( ) 方法属于"Arrays类" ,返回的是一个"java.util.Arrays.ArrayList" ( 这是一个 AbstractList 的私有静态内部类,不是"java.util.ArrayList" )
所以,使用 Arrays . asList ( 123 , 4567 ) 时,需要导入 "import java.util.Arrays" Arrays . asList ( ) ,返回的是AbstractList 的一个实例,这里发生了向上转型,即一个更具体的类(AbstractList )的实例被赋值给了一个更一般的接口(Collection )类型的变量。2 、
Arrays . asList ( 123 , 4567 ) ,详解如下:将一组元素,转换为,一个固定大小的列表( List ) 的静态方法,需要注意的是,返回的列表( List ) 大小是固定的,不能添加或删除元素,如果,你尝试添加、删除,将会抛出"UnsupportedOperationException" 。另,如果你需要一个可以修改的列表,应该使用 new ArrayList < > ( Arrays . asList ( 123 , 4567 ) ) 来创建一个新的 ArrayList 实例,这样就可以修改它的内容了。3 、代码示例import java. util. ArrayList ; import java. util. Arrays ; import java. util. Collection ; import java. util. List ; public static void main ( String [ ] args) { List < Integer > fixedList = Arrays . asList ( 123 , 4567 ) ; List < Integer > modifiableList = new ArrayList < > ( fixedList) ; System . out. println ( "Fixed List= " + fixedList) ; System . out. println ( "Modifiable List= " + modifiableList) ; modifiableList. add ( 8910 ) ; System . out. println ( "Modifiable= " + modifiableList) ; fixedList. add ( 9876 ) ; }
17、集合框架
1 、"集合的概述" :集合、数组都是对多个数据进行存储操作的结构,简称Java 容器。java集合类,可以用与存储数量不等的多个对象,还可用于保存具有映射关系的关联数组。2 、集合可分为"Collection" 和 "Map" 两种体系,如下,a、Collection 接口:单例数据,用来存随一个一个的对象,定义了存取一组对象的方法集合List :元素有序,可重复的集合Set :元素无序,不可重复的集合b、Map 接口,双列数据,用来存随一对一对的,保存具有映射关系"key-value对" 的集合
Collection接口
Collection 接口:集合框架的顶级接口,所有接口都是从"Collection接口" 继承过来的,是"Set 和 List的父接口" ,但"不是Map的父接口" 。
常用方法-1
1 、add ( Object e) :将元素e添加到集合中2 、size ( ) :获取添加元素的个数3 、addAll ( Collection coll) :将coll集合中的元素添加到当前的集合中4 、clear ( ) :清空集合元素5 、isEmpty ( ) :判断当前集合是否为空import java. util. ArrayList ;
import java. util. Collection ;
import java. util. Date ; public static void main ( String [ ] args) { Collection coll = new ArrayList ( ) ; coll. add ( "AA" ) ; coll. add ( "BB" ) ; coll. add ( 123 ) ; coll. add ( new Date ( ) ) ; System . out. println ( "coll= " + coll) ; System . out. println ( coll. size ( ) ) ; Collection coll2 = new ArrayList ( ) ; coll2. add ( 456 ) ; coll2. add ( "CC" ) ; System . out. println ( "coll2= " + coll2) ; coll. addAll ( coll2) ; System . out. println ( "coll= " + coll) ; System . out. println ( coll. isEmpty ( ) ) ; coll. clear ( ) ; System . out. println ( "coll= " + coll) ; System . out. println ( coll. isEmpty ( ) ) ;
}
常用方法-2
1 、contains ( Object obj) :判断当前集合中是否包含obj2 、containsAll ( Collection coll) :判断形参coll中的所有元素,是否都存在于当前集合中3 、remove ( Object obj) :从当前集合中移除obj元素4 、removeAll ( Collection coll) :差集,从当前集合中移除coll中所有的元素5 、retainAll ( Collection coll) :交集,获取当前集合和coll集合的交集,并返回给当前集合6 、equals ( Object obj) :要想返回true ,需要当前集合和形参集合的元素都相同7 、hashCode ( ) :返回当前对象的哈希值8 、集合 —> 数组:toArray ( ) "注意" :向Collection 接口的实现类的对象中,添加数据obj时,要求obj所在类要重写equals ( )
public static void main ( String [ ] args) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( 789 ) ; coll. add ( new String ( "Tom" ) ) ; System . out. println ( coll) ; boolean contains1 = coll. contains ( 123 ) ; boolean contains2 = coll. contains ( 1234 ) ; System . out. println ( contains1) ; System . out. println ( contains2) ; Collection coll2 = Arrays . asList ( 123 , 456 ) ; System . out. println ( coll. containsAll ( coll2) ) ; Collection coll3 = Arrays . asList ( 123 , 4567 ) ; System . out. println ( coll. containsAll ( coll3) ) ; coll. remove ( 123 ) ; System . out. println ( coll) ; Collection coll1 = Arrays . asList ( 123 , 456 ) ; coll. removeAll ( coll1) ; System . out. println ( coll) ;
}
public static void main ( String [ ] args) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( 789 ) ; coll. add ( new String ( "Tom" ) ) ; System . out. println ( coll) ; Collection coll2 = Arrays . asList ( 123 , 456 , 789 ) ; coll. retainAll ( coll2) ; System . out. println ( coll) ; System . out. println ( coll. equals ( coll2) ) ; coll. add ( 000 ) ; System . out. println ( coll. equals ( coll2) ) ;
}
public static void main ( String [ ] args) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( 789 ) ; coll. add ( new String ( "Tom" ) ) ; System . out. println ( coll) ; System . out. println ( coll. hashCode ( ) ) ; Object [ ] arr = coll. toArray ( ) ; System . out. println ( Arrays . toString ( arr) ) ; for ( int i = 0 ; i < arr. length; i++ ) { if ( i != arr. length- 1 ) { System . out. print ( arr[ i] + "-" ) ; } else { System . out. print ( arr[ i] ) ; } } List < String > list = Arrays . asList ( new String [ ] { "AA" , "BB" , "CC" } ) ; System . out. println ( list) ; List arr1 = Arrays . asList ( new int [ ] { 123 , 456 } ) ; System . out. println ( arr1. size ( ) ) ; List arr2 = Arrays . asList ( new Integer [ ] { 123 , 456 } ) ; System . out. println ( arr2. size ( ) ) ;
}
迭代器接口
GOP 给迭代器模式定义为:提供一种方法,访问容器对象中各个元素,而又不需暴露该对象内部细节,迭代器模式,就是为容器而生用于遍历Collection集合中的元素,"Collection接口继承了java.long.Iterable接口" ,该接口有一个 iterator ( ) 方法,那么,所有实现了Collection接口的集合类,都有一个 iterator ( ) 方法。
import java. util. ArrayList ;
import java. util. Collection ;
import java. util. Iterator ; public static void main ( String [ ] args) { coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( 789 ) ; coll. add ( new String ( "Tom" ) ) ; Iterator iterator = coll. iterator ( ) ; while ( iterator. hasNext ( ) ) { System . out. println ( iterator. next ( ) ) ; } Iterator iterator = coll. iterator ( ) ; while ( ( iterator. next ( ) ) != null ) { System . out. println ( iterator. next ( ) ) ; } while ( coll. iterator ( ) . hasNext ( ) ) { System . out. println ( coll. iterator ( ) . next ( ) ) ; }
}
import java. util. ArrayList ;
import java. util. Collection ;
import java. util. Iterator ; public static void main ( String [ ] args) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( "Tom" ) ; coll. add ( 789 ) ; Iterator iterator = coll. iterator ( ) ; while ( iterator. hasNext ( ) ) { Object obj = iterator. next ( ) ; if ( "Tom" . equals ( obj) ) { iterator. remove ( ) ; System . out. println ( "Removed: " + obj) ; } else { System . out. println ( "Kept: " + obj) ; } }
}
Collection - List
List 接口的实现类有:ArrayList LinkedList Vector
1 、List 接口的常用方法:void add ( int index, Object ele) :在index位置插入ele元素boolean addAll ( int index, Collection eles) :从index位置开始,将eles中的所有元素添加进来Object get ( int index) :获取指定index位置的元素int indexOf ( Object obj) :返回obj在集合中首次出现的位置,如果不存在 返回- 1 int lastIndexOf ( Object obj) :返回obj在当前集合中末次出现的位置Object remove ( int index) :移除指定index位置的元素,并返回此元素Object set ( int index, Object ele) :设置指定index位置的元素为eleList subList ( int fromIndex, int toIndex) :返回,从fromIndex到toIndex位置的子集合2 、总结:常用方法增:add ( Object obj) 删:remove ( int index) / remove ( Object obj) 改:set ( int index, Object ele) 查:get ( int index) 插:add ( int index, Object ele) 长度:size ( ) 遍历:Iterator 迭代器方式增强for 循环普通的循环
import java. util. ArrayList ;
import java. util. Arrays ;
import java. util. List ; public static void main ( String [ ] args) { ArrayList list = new ArrayList ( ) ; list. add ( 123 ) ; list. add ( 456 ) ; list. add ( "AA" ) ; list. add ( 456 ) ; System . out. println ( "list=" + list) ; list. add ( 1 , "BB" ) ; System . out. println ( list) ; List < Integer > list1 = Arrays . asList ( 1 , 2 , 3 ) ; list. addAll ( list1) ; System . out. println ( list) ; System . out. println ( list. get ( 1 ) ) ; System . out. println ( list. indexOf ( 456 ) ) ; System . out. println ( list. indexOf ( 456789 ) ) ; System . out. println ( list. lastIndexOf ( 456 ) ) ; Object o = list. remove ( 0 ) ; System . out. println ( o) ; System . out. println ( list) ; list. set ( 1 , "CC" ) ; System . out. println ( list) ; List list2 = list. subList ( 0 , 3 ) ; System . out. println ( list2) ; Iterator iterator = list. iterator ( ) ; while ( iterator. hasNext ( ) ) { System . out. println ( iterator. next ( ) ) ; } for ( Object obj: list) { System . out. println ( obj) ; } for ( int i = 0 ; i < list. size ( ) ; i++ ) { System . out. println ( list. get ( i) ) ; }
}
ArrayList
1 、内部存储用的数据结构,是用* * 数组(动态调整大小)* * 实现,默认初始容量为10 。每次扩容大小是增加50 % (在java8版本以及之后的版本,java6使用的是1.5 倍)。2 、优点:使用数组实现,因此,内部元素可以通过索引实现快速随机访问。缺点:a. 从ArrayList 中间位置插入和删除元素,都需要循环移动其他元素元素的位置。b. 数组空间不够需要扩容时,会开辟一个新的数组,把旧的数组元素拷贝过去,比较耗性能。c. 线程不安全。3 、扩容java8及之后,扩容计算方法:int newCapacity = oldCapacity + ( oldCapacity >> 1 ) ; 这意味着,在原来数组大小的基础上,扩大50 % 作为新数组容量的大小。java6的计算方法:int newCapacity = ( oldCapacity * 3 ) / 2 + 1 ; 这意味着,在原来数组大小的基础上,扩大1.5 倍作为新数组容量的大小。4 、"总之,ArrayList基于数组实现 查改快,增删慢,线程不安全" "在需要做一次性插入 和 多次查询业务时,可以使用此集合,但是,ArrayList不保证线程安全,只能在单线程时候做使用"
源码解析
"源码" :class ArrayList < E > extends AbstractList < E > implements List < E > , RandomAccess , Cloneable , java. io. Serializable
"AbstractList<E>" :是List接口第一抽象类"RandomAccess" :RandomAccess接口是一个标志接口(Marker)它支持快速随机访问"Cloneable" :支持克隆"java.io.Serializable" :RandomAccess接口也是是一个标志接口(Marker) 它支持序列化和反序列化
"属性" :private static final long serialVersionUID = 8683452581122892189L ; private static final int DEFAULT_CAPACITY = 10 ; private static final Object [ ] EMPTY_ELEMENTDATA = { } ; private static final Object [ ] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = { } ; transient Object [ ] elementData; private int size; "无参构造器 - 构造一个初始容量为10的一个数组" :public ArrayList ( ) { this . elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA ; } "带参构造器 - 1" :
int initialCapacity为ArrayList 底层数组初始的长度,构造一个指定长度的数组
public ArrayList ( int initialCapacity) { if ( initialCapacity > 0 ) { this . elementData = new Object [ initialCapacity] ; } else if ( initialCapacity == 0 ) { this . elementData = EMPTY_ELEMENTDATA ; } else { throw new IllegalArgumentException ( "Illegal Capacity: " + initialCapacity) ; }
} "带参构造器 - 2"
"Collection<? extends E> c" :c是泛型E 的子类,构造一个包含指定集合的元素的列表,按照它们由集合的迭代器返回的顺序。
public ArrayList ( Collection < ? extends E > c) { Object [ ] a = c. toArray ( ) ; if ( ( size = a. length) != 0 ) { if ( c. getClass ( ) == ArrayList . class ) { elementData = a; } else { elementData = Arrays . copyOf ( a, size, Object [ ] . class ) ; } } else { elementData = EMPTY_ELEMENTDATA ; }
} "扩容机制"
private Object [ ] grow ( ) { return grow ( size + 1 ) ;
} private Object [ ] grow ( int minCapacity) { return elementData = Arrays . copyOf ( elementData, newCapacity ( minCapacity) ) ;
} private int newCapacity ( int minCapacity) { int oldCapacity = elementData. length; int newCapacity = oldCapacity + ( oldCapacity >> 1 ) ; if ( newCapacity - minCapacity <= 0 ) { if ( elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA ) return Math . max ( DEFAULT_CAPACITY , minCapacity) ; if ( minCapacity < 0 ) throw new OutOfMemoryError ( ) ; return minCapacity; } return ( newCapacity - MAX_ARRAY_SIZE <= 0 ) ? newCapacity: hugeCapacity ( minCapacity) ;
} private static int hugeCapacity ( int minCapacity) { if ( minCapacity < 0 ) throw new OutOfMemoryError ( ) ; return ( minCapacity > MAX_ARRAY_SIZE ) ? Integer . MAX_VALUE : MAX_ARRAY_SIZE ;
}
Vector
1 、基于 数组(动态调整大小) 数据结构实现,初始容量是10 。2 、优点:线程安全。缺点:效率低,增加元素、删除元素、查找元素都很慢。
LinkList
1 、内部存储用的数据结构,是用双向链表实现。2 、优点:使用链表实现,适合动态的插入和删除。缺点:a. 随机访问元素的速度相对较慢。b. 基于链表数据结构的实现,占用的内存空间比较大(除了保存数据本身,还要保存指针信息)。"总之" ,LinkedList ,底层通过双向链表的形式实现,增删快,遍历和查询慢,线程不安全。LinkedList ,底层使用双向链表的形式存储数据,不用向ArrayList 存在地址浪费,并且在增删时效率高,我们可以在需要经常增删时使用此集合来提高我们的效率。
"源码" :public class LinkedList < E > extends AbstractSequentialList < E > implements List < E > , Deque < E > , Cloneable , java. io. Serializable 与 ArrayList 不同的是,LinkedList 继承的是 AbstractSequentialList ,但,AbstractSequentialList 也是继承自 AbstractList ,其他和 ArrayList 相同
"属性" transient int size = 0 ; transient Node < E > first; transient Node < E > last; "构造方法"
public LinkedList ( ) { } public LinkedList ( Collection < ? extends E > c) { this ( ) ; addAll ( c) ;
} "链表节点静态类,储存的数据的实体Node"
private static class Node < E > { E item; Node < E > next; Node < E > prev; Node ( Node < E > prev, E element, Node < E > next) { this . item = element; this . next = next; this . prev = prev; }
}