How much do you know about java collections ?, Java Collection
After using java collections for so long, I haven't systematically studied the java Collection Structure. Today I personally drew the class diagram, and I finally got some gains.
1. All sets implement the Iterable interface.
The Iterable Interface contains an abstract method: Iterator <T> iterator (); each set that implements this method returns an Iterator object.
Iterator: it contains three methods: hashNext (), next (), and remove (), which can be used to traverse a set and delete elements. For example:
Collection <String> list = new ArrayList <String> ();
List. add ("");
List. add ("B ");
List. add ("c ");
Iterator <String> iterator = list. iterator ();
While (iterator. hasNext ()){
String element = iterator. next ();
System. out. println (element); // a B c
}
System. out. println (list); // [a, B, c] iterator = list. iterator (); iterator. next ();
Iterator. remove ();
System. out. println (list); // [B, c]
Ps: Why not directly implement the Iterator interface? Because: Iterator will carry the location information of the current set, and it will not start from 0 when it is used again next time; the Iterable interface returns an Iterator object (Iterator implemented through internal classes), and each Iterator does not affect each other.
Ii. ListIterator
The public ListIterator <E> listIterator () method is encapsulated from AbstractList, and a ListIterator is returned. It adds methods such as add (), previous (), and hasPrevious () based on Iterator, bidirectional traversal is supported.
AbstractList <String> list = new ArrayList <String> ();
List. add ("");
List. add ("B ");
List. add ("c ");
ListIterator <String> iterator = list. listIterator (3 );
While (iterator. hasPrevious ()){
String element = iterator. previous ();
System. out. println (element); // c, B,
}
Iii. Comparison
ArrayList: Allows storing duplicate elements in an orderly manner, making random access easier.
Linked List: implements a linked list, which is better than ArrayList during insertion and deletion.
HashSet: duplicate elements are not allowed to be unordered (the hash function sorts the elements and can be quickly queried), and null values are allowed.
TreeSet: red/black tree sorting. The contained elements must implement the Comparable interface and define the compareTo method. null values are not allowed.
HashMap: The thread is insecure. The key and value values allow null. The containsKey () method is used to determine whether the key is contained. Duplicate keys are not allowed. The default size of the hash array is 16, it must be an index of 2 and re-calculate the hash value.
HashTable: thread security. The key and value values cannot contain null values, duplicate keys are not allowed, and the default size of the array is 11. The addition method is old * 2 + 1, use the hash value that contains the object.
Iv. Collections and Arrays
Collections: a special class under java. util. It contains various static methods related to set operations, and can perform operations such as search, sorting, and thread security for various sets.
Arrays: a special class under java. util used to operate Arrays. It provides static methods such as search, sorting, copying, and conversion.
Follow ginger to talk about technology, no.: helojava, or scan the following QR code.
One post per day, chicken soup for technical purposes.