一、Collection概述
集合中不能直接存储基本数据类型,也不能存储Java对象,只是存储java对象的内存地址 存放在集合中的类型,一定要重写equals()方法
二、Collection中的常用方法
1. add()
boolean add(Object e) 向集合中添加元素
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( 100 ) ;
c. add ( true ) ;
c. add ( new Object ( ) ) ;
c. add ( new Student ( ) ) ;
}
}
class Student {
}
2. size()
int size() 获取集合中元素的个数 (不是容量)
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( 100 ) ;
c. add ( true ) ;
c. add ( new Object ( ) ) ;
c. add ( new Student ( ) ) ;
System. out. println ( "集合中元素个数是:" + c. size ( ) ) ;
}
}
class Student {
}
3. clear()
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( 100 ) ;
c. add ( true ) ;
c. add ( new Object ( ) ) ;
c. add ( new Student ( ) ) ;
System. out. println ( "集合中元素个数是:" + c. size ( ) ) ;
c. clear ( ) ;
System. out. println ( "集合中元素个数是:" + c. size ( ) ) ;
}
}
class Student {
}
4. contains()
boolean contains(Object o) 判断当前集合中是否包含某元素,包含返回true,不包含返回false 底层实质上调用了equals()方法 存放在集合中的类型,一定要重写equals()方法
4.1 测试一
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
String s1 = new String ( "abc" ) ;
c. add ( s1) ;
String s2 = new String ( "123" ) ;
c. add ( s2) ;
System. out. println ( "元素的个数是:" + c. size ( ) ) ;
String s3 = new String ( "abc" ) ;
System. out. println ( c. contains ( s3) ) ;
}
}
4.2 测试二
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
User u1 = new User ( "lzj" ) ;
User u2 = new User ( "lzj" ) ;
c. add ( u1) ;
System. out. println ( c. contains ( u2) ) ;
c. remove ( u2) ;
System. out. println ( c. size ( ) ) ;
}
}
class User {
private String name;
public User ( ) {
}
public User ( String name) {
this . name = name;
}
public boolean equals ( Object o) {
if ( o == null || ! ( o instanceof User ) )
return false ;
if ( o == this )
return true ;
User u = ( User) o;
return u. name. equals ( this . name) ;
}
}
4.3 源码分析
public boolean contains ( Object o) {
return indexOf ( o) >= 0 ;
}
public int indexOf ( Object o) {
if ( o == null) {
for ( int i = 0 ; i < size; i++ )
if ( elementData[ i] == null)
return i;
} else {
for ( int i = 0 ; i < size; i++ )
if ( o. equals ( elementData[ i] ) )
return i;
}
return - 1 ;
}
LinkedList底层contains()源码:
public boolean contains ( Object o) {
return indexOf ( o) != - 1 ;
}
public int indexOf ( Object o) {
int index = 0 ;
if ( o == null) {
for ( Node< E> x = first; x != null; x = x. next) {
if ( x. item == null)
return index;
index++ ;
}
} else {
for ( Node< E> x = first; x != null; x = x. next) {
if ( o. equals ( x. item) )
return index;
index++ ;
}
}
return - 1 ;
}
public boolean contains ( Object o) {
return map. containsKey ( o) ;
}
public boolean containsKey ( Object key) {
return getNode ( hash ( key) , key) != null;
}
final Node< K, V> getNode ( int hash, Object key) {
Node< K, V> [ ] tab; Node< K, V> first, e; int n; K k;
if ( ( tab = table) != null && ( n = tab. length) > 0 &&
( first = tab[ ( n - 1 ) & hash] ) != null) {
if ( first. hash == hash &&
( ( k = first. key) == key || ( key != null && key. equals ( k) ) ) )
return first;
if ( ( e = first. next) != null) {
if ( first instanceof TreeNode )
return ( ( TreeNode< K, V> ) first) . getTreeNode ( hash, key) ;
do {
if ( e. hash == hash &&
( ( k = e. key) == key || ( key != null && key. equals ( k) ) ) )
return e;
} while ( ( e = e. next) != null) ;
}
}
return null;
}
5. remove()
boolean remove(Object o) 删除集合中某个元素 底层实质上调用了equals()方法
5.1 测试一
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( "hello" ) ;
c. add ( "world" ) ;
c. add ( 1 ) ;
c. remove ( "hello" ) ;
System. out. println ( "集合中元素个数是:" + c. size ( ) ) ;
}
}
5.2 测试二
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
String s1 = new String ( "hello" ) ;
c. add ( s1) ;
String s2 = new String ( "hello" ) ;
c. remove ( s2) ;
System. out. println ( c. size ( ) ) ;
}
}
5.3 源码分析
public boolean remove ( Object o) {
if ( o == null) {
for ( int index = 0 ; index < size; index++ )
if ( elementData[ index] == null) {
fastRemove ( index) ;
return true ;
}
} else {
for ( int index = 0 ; index < size; index++ )
if ( o. equals ( elementData[ index] ) ) {
fastRemove ( index) ;
return true ;
}
}
return false ;
}
6. isEmpty()
boolean isEmpty() 判断该集合是否为空
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( "hello" ) ;
c. add ( "world" ) ;
System. out. println ( c. isEmpty ( ) ) ;
}
}
7. toArray()
Object[] toArray() 集合转换成数组
import java. util. ArrayList;
import java. util. Collection;
public class Test {
public static void main ( String[ ] args) {
Collection c = new ArrayList ( ) ;
c. add ( "hello" ) ;
c. add ( "world" ) ;
Object[ ] objs = c. toArray ( ) ;
for ( int i = 0 ; i < objs. length; i++ ) {
Object o = objs[ i] ;
System. out. println ( o) ;
}
}
}