The difference between a set and an array
1, an array (which can store the base data type ) is a container for storing existing objects, but the length of the array is fixed and is not suitable for use when the number of objects is unknown.
2. The length of the collection ( objects can only be stored , object types may be different) is variable , and can be used in most cases.
Schematic and description of the collection frame in Java
1, collection interface is the root interface of the collection class, there is no direct implementation class for this interface in Java. But let it be inherited produced two interfaces, that is, set and list. The set cannot contain duplicate elements. A list is an ordered collection that can contain duplicate elements that provide a way to access by index. Common methods are as follows:
| add (e E) |
Add the specified object to the collection |
| remove (Object o) |
Removes the specified object from the collection, removes the successful return true, and does not successfully return false |
| contains (Object o) |
See if the collection contains the specified object, containing the return true, Does not contain a return flase |
| size () |
Returns the number of objects held in the collection. The return value is int |
| clear () |
Remove all objects from the collection and empty the collection. |
| iterator () |
Returns a Iterator object containing all objects, Used to iterate through |
| toarray () |
Returns an array containing all the objects, The type is Object |
| toarray (t[] T) |
Returns an object containing all objects. An array of the specified type |
2, map is another interface in the Java.util package, it is not related to the collection interface, is independent of each other, but all belong to the collection class part. Map contains the key-value pair. A map cannot contain duplicate keys, but it can contain the same value.
3, Iterator, all of the collection class, all implemented the Iterator interface, which is an interface for iterating over the elements in the collection, mainly consists of the following three ways:
(1) whether Hasnext () has the next element.
(2) Next () returns the next element.
(3) Remove () deletes the current element.
Iii. Introduction to several commonly used interfaces and classes in Java
1.List Interface
The list is concerned with indexes, which are specific to the index, compared to other collections: Get (int index), add (int index,object o), indexOf (Object o).
ArrayList can interpret it as an array of growth that provides fast iterations and the ability to quickly random access.
The elements in the LinkedList are double-linked, and LinkedList becomes the perfect choice in the list when quick insertions and deletions are required.
Vector is a thread-safe version of ArrayList, which has a lower performance than ArrayList and is now rarely used
2.set interface
Set cares about uniqueness, it does not allow duplication .
HashSet You can use this class when you do not want duplicate values in the collection, and you do not care about the order between elements.
Linkedhashset This class when you do not want duplicate values in the collection, and you want to iterate through the elements in the order in which they are inserted.
TreeSet can take this class when you do not want duplicate values in the collection and want to sort by the natural order of the elements. (Natural order means something that is not related to the insertion order, but rather to the ordering of the content and attributes of the element itself, such as "ABC" in front of "Abd"). )
3. Queue interface
The queue is used to save the list of tasks that will be performed.
The LinkedList also implements the queue interface, which enables FIFO-first queues.
priorityqueue is used to create a priority queue for natural sorting. This is a detailed blog URL: http://blog.csdn.net/u011116672/article/details/50997622 interested can see.
4. Map interface
Map is concerned with the unique identifier. He maps the unique key to an element. Of course, the keys and values are objects.
HashMap When a key-value pair is required and the order is not concerned, HASHMAP can be used.
Hashtable Note that T in Hashtable is lowercase, it is a thread-safe version of HashMap and is now rarely used.
Linkedhashmap can be used when a key-value pair is required and is concerned about the insertion order.
TreeMap can be used when a key-value pair is required and is concerned about the natural ordering of the elements.
The common methods for map interfaces are shown in the following table:
| Put (K key, V value) |
Adds the specified key-value pair to the collection |
| Putall (Map <? extends K,? extends V> t) |
Adds all the key-value pairs in a map to the collection |
| ContainsKey (Object key) |
Returns true if the key is included |
| Containsvalue (Object value) |
Returns True if the value is included |
| Get (Object key) |
Returns the corresponding value object according to the key |
| KeySet () |
Returns all the keys in the collection as a set collection |
| VALUES () |
Returns all values in the collection in collection form |
| Remove (Object key) |
If the specified key is present, the key value pair is removed, the value corresponding to the key is returned, and null if it does not exist |
| Clear () |
Removes all key-value pairs from the map, or empties the collection |
| IsEmpty () |
See if a key-value pair exists in the map |
| Size () |
To view the number of key-value pairs contained in the collection, return the INT type |
Because the keys in the map must be unique, the key can be null, only one key is null, and the value in map does not have this restriction, and a value of NULL is often present, so the get (Object key) method returns NULL, There are two cases in which the key value pair does not exist, and the value object that corresponds to the key is null. To ensure that a key is actually in a map, the method you should use is ContainsKey (Object key).
Overview of collections in Java