add、clear、addAll、isEmpty、size()
@Test public void test1 ( ) { Collection coll = new ArrayList ( ) ; coll. add ( "AA" ) ; coll. add ( "BB" ) ; coll. add ( 123 ) ; coll. add ( new Date ( ) ) ; System . out. println ( coll. size ( ) ) ; Collection coll1 = new ArrayList ( ) ; coll1. add ( 456 ) ; coll1. add ( "CC" ) ; coll. addAll ( coll1) ; System . out. println ( coll. size ( ) ) ; System . out. println ( coll) ; coll. clear ( ) ; System . out. println ( coll. isEmpty ( ) ) ; }
contains、Arrays.asList、containsAll
注: 向Collection 接口的实现类的对象中添加数据obj时,要求obj所在类要重写equals ( ) .
@Test public void test1 ( ) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ;
coll. add ( new Person ( "Jerry" , 20 ) ) ; coll. add ( new String ( "Tom" ) ) ; coll. add ( false ) ; boolean contains = coll. contains ( 123 ) ; System . out. println ( contains) ; System . out. println ( coll. contains ( new String ( "Tom" ) ) ) ; System . out. println ( coll. contains ( new Person ( "Jerry" , 20 ) ) ) ; Collection coll1 = Arrays . asList ( 123 , 4567 ) ; System . out. println ( coll. containsAll ( coll1) ) ; }
Arrays . asList ( …) 方法返回的List 集合,既不是ArrayList 实例,也不是Vector 实例。 Arrays . asList ( …) 返回值是一个固定长度的List 集合
remove、removeAll
@Test public void test2 ( ) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( new Person ( "Jerry" , 20 ) ) ; coll. add ( new String ( "Tom" ) ) ; coll. add ( false ) ; coll. remove ( 1234 ) ; System . out. println ( coll) ; coll. remove ( new Person ( "Jerry" , 20 ) ) ; System . out. println ( coll) ; Collection coll1 = Arrays . asList ( 123 , 456 ) ; coll. removeAll ( coll1) ; System . out. println ( coll) ; }
retainAll、equals(Object obj)
@Test public void test3 ( ) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( new Person ( "Jerry" , 20 ) ) ; coll. add ( new String ( "Tom" ) ) ; coll. add ( false ) ; Collection coll1 = Arrays . asList ( 123 , 456 , 789 ) ; coll. retainAll ( coll1) ; System . out. println ( coll) ; Collection coll1 = new ArrayList ( ) ; coll1. add ( 456 ) ; coll1. add ( 123 ) ; coll1. add ( new Person ( "Jerry" , 20 ) ) ; coll1. add ( new String ( "Tom" ) ) ; coll1. add ( false ) ; System . out. println ( coll. equals ( coll1) ) ; }
toArray()、 Arrays.asList
@Test public void test4 ( ) { Collection coll = new ArrayList ( ) ; coll. add ( 123 ) ; coll. add ( 456 ) ; coll. add ( new Person ( "Jerry" , 20 ) ) ; coll. add ( new String ( "Tom" ) ) ; coll. add ( false ) ; System . out. println ( coll. hashCode ( ) ) ; Object [ ] arr = coll. toArray ( ) ; for ( int i = 0 ; i < arr. length; i++ ) { System . out. println ( 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 ( ) ) ; System . out. println ( arr1) ; List arr2 = Arrays . asList ( new Integer [ ] { 123 , 456 } ) ; System . out. println ( arr2. size ( ) ) ; System . out. println ( arr2) ;
复习:
1. 比较 throw 和 throws 的异同
throw : 1. 手动生成一个异常对象,并抛出,使用在方法内部< - > 2. 自动抛出异常对象
throws : 处理异常的方式,使用在方法声明处的末尾“上游排污 下游治污”2. 谈谈你对同步代码块中同步监视器和共享数据的理解及各自要求。
同步监视器:俗称锁。①任何一个类的对象都可以充当锁。② 多个线程共用同一把锁。
共享数据:多个线程共同操作的数据,即为共享数据。
需要使用同步机制将操作共享数据的代码包起来(保证共享数据的线程安全)。不能包多了,也不能包少了。