集合(一)
一、集合概述
1、集合概述
A: 集合的由来
面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,Java就提供了集合类。
B: 数组和集合的区别
( 1 ) : 长度区别:
数组的长度是固定的而集合的长度是可变的
( 2 ) : 存储数据类型的区别:
数组可以存储基本数据类型 , 也可以存储引用数据类型; 而集合只能存储引用数据类型
( 3 ) : 内容区别:
数组只能存储同种数据类型的元素 , 集合可以存储不同类型的元素
2、常用集合继承体系图
二、接口Collection
1、Collection常用功能
Collection的功能概述( 通过API查看即可得到)
a: 添加功能
boolean add ( Object obj) : 在集合末尾添加一个元素
boolean addAll ( Collection c) : 在集合末尾添加进另一个集合中的所有元素
b: 删除功能
void clear ( ) : 移除集合所有元素
boolean remove ( Object o) : 移除指定元素
boolean removeAll ( Collection c) : 移除该集合与指定集合的交集元素,有交集元素,返回true ,如果没有交集元素,则移除失败,返回false .
boolean retainAll ( Collection c) : 移除该集合与指定集合的非交集元素
c: 判断功能
boolean contains ( Object o) :判断集合中是否包含指定的元素
boolean containsAll ( Collection c) :判断该集合是否包含另一个集合( 所有元素)
boolean isEmpty ( ) :判断集合是否为空
d: 获取功能
Iterator< E> iterator ( ) : 获取迭代器
int size ( ) : 获取集合中元素的个数
e: 把集合转换为数组
Object[ ] toArray ( )
2、Collection常用功能测试
import java. util. ArrayList;
import java. util. Arrays;
import java. util. Collection;
import java. util. Iterator;
public class Blog {
public static void main ( String[ ] args) {
Collection coll = new ArrayList ( ) ;
Collection coll1 = new ArrayList ( ) ;
coll. add ( 11 ) ;
coll. add ( 22 ) ;
coll. add ( 11 ) ;
coll. add ( 22 ) ;
coll. add ( 33 ) ;
coll. add ( 44 ) ;
System. out. println ( coll) ;
coll1. addAll ( coll) ;
System. out. println ( coll1) ;
coll1. clear ( ) ;
coll. remove ( 11 ) ;
System. out. println ( coll) ;
coll1. add ( 11 ) ;
coll1. add ( 22 ) ;
coll. removeAll ( coll1) ;
System. out. println ( coll) ;
boolean contains = coll. contains ( 11 ) ;
System. out. println ( contains) ;
boolean b = coll. containsAll ( coll1) ;
System. out. println ( b) ;
boolean empty = coll. isEmpty ( ) ;
System. out. println ( empty) ;
Iterator iterator = coll. iterator ( ) ;
int size = coll. size ( ) ;
System. out. println ( size) ;
Object[ ] objects = coll. toArray ( ) ;
System. out. println ( Arrays. toString ( objects) ) ;
}
}
3、Collection集合遍历
A:把集合转为数组遍历
public static void listTraversing ( Collection coll) {
if ( coll== null) {
System. out. println ( "null" ) ;
return ;
}
Object[ ] obj = coll. toArray ( ) ;
for ( int i = 0 ; i < obj. length; i++ ) {
System. out. println ( obj[ i] ) ;
}
}
B:使用迭代器遍历
private static void iteratorTraversing ( Collection coll) {
if ( coll== null) {
System. out. println ( "null" ) ;
return ;
}
Iterator iterator = coll. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
Object o = iterator. next ( ) ;
System. out. println ( o) ;
}
}
C:增强for循环遍历
private static void increaseForTraversing ( Collection coll) {
for ( Object o : coll) {
System. out. println ( o) ;
}
}
三、接口List
1、List特点
元素有序,每个元素都有一个索引,且可有重复元素。
2、List特有功能
void add ( int index, E element) : 在指定索引处添加元素
E remove ( int index) : 移除指定索引处的元素, 返回的是移除的元素
E get ( int index) : 获取指定索引处的元素
E set ( int index, E element) : 更改指定索引处的元素 返回的而是被替换的元素
3、List特有功能测试
import java. util. ArrayList;
import java. util. List;
public class Blog2 {
public static void main ( String[ ] args) {
List< String> strList = new ArrayList < > ( ) ;
strList. add ( "aaa" ) ;
strList. add ( "bbb" ) ;
strList. add ( "ccc" ) ;
strList. add ( 1 , "ddd" ) ;
System. out. println ( strList) ;
strList. remove ( 1 ) ;
System. out. println ( strList) ;
String str= strList. get ( 1 ) ;
System. out. println ( str) ;
strList. set ( 1 , "你好" ) ;
System. out. println ( strList) ;
}
}
运行结果:
[ aaa, ddd, bbb, ccc]
[ aaa, bbb, ccc]
bbb
[ aaa, 你好, ccc]
4、List新增遍历方式
A:通过get()和size()方法遍历
private static void methodTraversal ( List list) {
if ( list== null) {
System. out. println ( "null" ) ;
return ;
}
for ( int i = 0 ; i < list. size ( ) ; i++ ) {
Object o = list. get ( i) ;
System. out. println ( o) ;
}
}
B:使用List特有迭代器遍历
private static void listIteratorTraversal ( List list) {
if ( list== null) {
System. out. println ( "null" ) ;
return ;
}
ListIterator listIterator = list. listIterator ( ) ;
while ( listIterator. hasNext ( ) ) {
Object o = listIterator. next ( ) ;
System. out. println ( o) ;
}
System. out. println ( "--------------------" ) ;
while ( listIterator. hasPrevious ( ) ) {
Object o1 = listIterator. previous ( ) ;
System. out. println ( o1) ;
}
}
5、并发修改异常产生原因及解决方案
产生原因:在迭代器或者增强for 循环遍历集合过程中,由于迭代器或者增强for 循环已经提前获取集合的长度,这时再在集合遍历过程中添删元素,会改变集合的长度,导致集合实际长度与迭代器或增强for 循环获取的长度不一致,所以会产生并发修改异常。
解决方案:1 、使用ListIterator迭代器自身的add ( ) 方法添加元素;
2 、使用普通for 循环遍历添加元素
6、Arrays工具类的asList()方法的使用
int [ ] arr= { 1 , 2 , 3 } ;
List< int [ ] > ints= Arrays. asList ( arr) ;
List< Integer> list= Arrays. asList ( 1 ,2 ,3 ,4 ,5 ) ;
Integer[ ] arr1= { 1 , 2 , 3 } ;
Integer[ ] arr2= { 4 , 5 , 6 } ;
List< Integer> integerList = Arrays. asList ( arr1) ;
List< Integer[ ] > asList = Arrays. asList ( arr1, arr2) ;
四、List的三个子类
1、List的三个子类的特点
List的三个子类的特点:
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高。
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低。
LinkedList:
底层数据结构是链表,查询慢,增删快。
线程不安全,效率高。
2、Vector常用特有功能
import java. util. Enumeration;
import java. util. Vector;
public class Blog4 {
public static void main ( String[ ] args) {
Vector< String> vector = new Vector ( ) ;
vector. addElement ( "q" ) ;
vector. addElement ( "w" ) ;
System. out. println ( vector) ;
String str= vector. elementAt ( 0 ) ;
System. out. println ( str) ;
Enumeration elements = vector. elements ( ) ;
elementsTraversing ( vector, elements) ;
}
private static void elementsTraversing ( Vector vector, Enumeration elements) {
while ( elements. hasMoreElements ( ) ) {
Object o = elements. nextElement ( ) ;
System. out. println ( o) ;
}
}
}
3、LinkedList常用特有功能
public void addFirst ( E e) 及 addLast ( E e)
public E getFirst ( ) 及 getLast ( )
public E removeFirst ( ) 及 public E removeLast ( )
3.1、LinkedList常用特有功能测试:
import java. util. LinkedList;
public class Blog5 {
public static void main ( String[ ] args) {
LinkedList< Integer> linkedList = new LinkedList < > ( ) ;
linkedList. add ( 11 ) ;
linkedList. add ( 22 ) ;
linkedList. addFirst ( 33 ) ;
System. out. println ( linkedList) ;
linkedList. addLast ( 44 ) ;
System. out. println ( linkedList) ;
Integer first = linkedList. getFirst ( ) ;
System. out. println ( first) ;
Integer last = linkedList. getLast ( ) ;
System. out. println ( last) ;
Integer removeFirst = linkedList. removeFirst ( ) ;
System. out. println ( removeFirst) ;
System. out. println ( linkedList) ;
Integer removeLast = linkedList. removeLast ( ) ;
System. out. println ( removeLast) ;
System. out. println ( linkedList) ;
}
}
3.2、使用LinkedList模拟栈数据结构的集合
import java. util. LinkedList;
public class Blog6 {
public static void main ( String[ ] args) {
MyList myList = new MyList ( ) ;
myList. addEle ( "aaa" ) ;
myList. addEle ( "bbb" ) ;
myList. addEle ( "ccc" ) ;
for ( int i = 0 ; i < 3 ; i++ ) {
Object ele = myList. getEle ( ) ;
System. out. println ( ele) ;
}
}
}
class MyList {
LinkedList linkedList= null;
public MyList ( ) {
linkedList = new LinkedList ( ) ;
}
public void addEle ( String ele) {
linkedList. addFirst ( ele) ;
}
public Object getEle ( ) {
Object obj = linkedList. removeFirst ( ) ;
linkedList. addLast ( obj) ;
return obj;
}
}
运行结果:
ccc
bbb
aaa
3.3、集合嵌套
import java. util. ArrayList;
public class Blog7 {
public static void main ( String[ ] args) {
ArrayList< String> integerType = new ArrayList < > ( ) ;
integerType. add ( "byte" ) ;
integerType. add ( "short" ) ;
integerType. add ( "int" ) ;
integerType. add ( "long" ) ;
ArrayList< String> decimalType = new ArrayList < > ( ) ;
decimalType. add ( "float" ) ;
decimalType. add ( "double" ) ;
ArrayList< String> characterType = new ArrayList < > ( ) ;
characterType. add ( "char" ) ;
ArrayList< String> booleanType = new ArrayList < > ( ) ;
booleanType. add ( "boolean" ) ;
ArrayList< ArrayList< String> > basicDataType = new ArrayList < > ( ) ;
basicDataType. add ( integerType) ;
basicDataType. add ( decimalType) ;
basicDataType. add ( characterType) ;
basicDataType. add ( booleanType) ;
}
}