Collections

Hierarcy 一、Collection Hierarchy

 

     List:An ordered data items.

     Set:Unique data itme.There is no duplicate element

     Map:maps the keys to values.

二、Fetures of collections

 

      Performance:Accessing a giving data item,Insertion and Deletion a giving data items.ArrayList spend a constant time accessing a giving data item,its deletion/insertion is linear in time.If delete or insert data item frequently,it's better to use LinkedList

     Sorted/ordered:The data structure is ordered if the data itmes is in some order.For example,ArrayList is ordered by its index.

     Uniqueness:There is no duplicate data item.Such as Set,Map

     Synchronize:support synchronization for thread safety.Such as Vector,HashTable.

 

三、Iterator

      Iterator is  a safe way to remove an element from a collection when traversing it.Traversing by index or enumeration is not safe way.

       A collection is create as bellow: 

      Delete empty String by enumeration: 

      Delete empty String by iterator: 

发现第一中删除元素的方法并不正确,因为当删除了一个元素以后,集合的长度和要访问的下一个元素的index已经变成被删除元素的index。

四、hashCode()和equals()

      Two objects are equals,determined by equals() method,must return the same hashCode.The reverse is not required.

      HashCode specify the uniqueness of a data item in a data structure.

      According to the equals()  method in object class,s1 and s0 is not equal because they don't refer to the same object.However ,you might like to see they as equal because you might use s0 as key when storing a data item,while use s1 as key when you want to retrieve.Hope this to happen,we need to override equals() and hashCode() method.   

五、Object ordering

      Ordering improve the perfomance of retrieval operation such as search.

      Natural ordering

      You can sort a collection in natural ordering by using the following method.     

     The classes of the data items should implement comparable interface,thereby support natural ordering.

       If FinalScore class don't implement Comparable interface,a exception will be thrown while

Collections.sort(a); was executed.

     We can also sort finalScore collection whose element don't implementComparable interface with the help of Comparator.

Test Code:

  Either the class of the object doesn't implement Comparable interface or the order in which you want to sort the elements other than natural ordering.In this case,you want to use Comparator intertace.

  Change NaturalOrder code into reverse way:

  The result of  Collections.sort(a,order); as below:

 

六、Binary Search:

     The binary search algorithm seached for specified element in a sorted list.The algorithm is implemented in two forms.

    

    The first form assuems the collection is sorted into ascending order according to its natural ordering of the data items.If search a specified item in a collection without ordering,it will return -1 even though the specified item existed.

      

  The second form take a comparator in addition to a list and a search key.It assumes the list is sorted into acsending order according to the specified comparator.The sort algorithm can be used to sort the list prior to calling the search algorithm.

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值