Collection classes in Java (j2se entry 11)

Source: Internet
Author: User
Tags comparable

Collection class

A collection (Collection class Object) is used to manage several other objects.It is similar to a container in the C ++ standard template library, but it can be used to store various types of objects in Java Collection classes.

Interfaces and classes constitute a collection framework and a collection concept. An object can be loaded with multiple objects. This object is a collection object..

Collection framework

Interface

CollectionUsed to manage multiple objects. Each element in the set is an object.

MapIn map, there is no object, but a key-Value Pair consisting of key and value. Keys cannot be repeated. Values can be the same. A key corresponds to a value one by one.

Class used in the collection. The interface is in the Java. util package. Pay attention to introduce it into the import when using it.

Collection Interface(The following describes its subinterfaces)

1)ListObjects of the implementation class of a list are organized in order when multiple objects are managed (that is, objects are stored in order). Objects of the List Implementation class are ordered, the content in the List Implementation class object can be repeated. (Note: The difference between order and sorting)

2)SetA set implementation class represents a set of mathematical concepts. The elements in the object of the set implementation class areUnorderedIs not stored in the input order.Elements are not repeated.

3)SortedsetIs the sub-interface of the Set, and its implementation class sorts the elements in the set. However, to specify a sorting rule, it will be sorted by the sorting rule.

Map Interface(The following describes its subinterfaces)

1)SortedmapThe implementation class of this interface can also be implemented, but it sorts the keys in the key-value pairs. The implementation class of this interface also needs to specify the sorting rules.

List interface implementation class

1>ArraylistIs similar to the function of the Collection class, the essence of arrylist isAutomatically increasing array, Arraylist is a list interface implemented by using an encapsulated array.

Collection implementation Class Object Traversal method is implemented by iteration.
When using the iterator, you must first get an iterator object,Iterator(Iterator Interface) This is an interface that is implemented in the Collection class, that is, it is implemented by an internal class (anonymous internal class.
Commonly used methods defined in the iterator interface include hasnext () and next ().
Hasnext (). This method uses a cursor and determines whether the cursor points to an object.
The next () method is also defined in the iterator interface. This method points the cursor to the next element. The cursor skips the first element and returns the content.

CollectionsThis is a tool class and Java. in the util package, the sort (Object of the implementation class of the List Interface) method in this class. Its parameters are the objects of a collection class. This method is used to sort the objects of the Collection class. In the future, I will use the set name as the collection class object. For the set of String object content, it will be alphabetically ordered (in ascending order ), the numbers are sorted in ascending order.

The sorting can be divided into two parts, one isSorting rulesThat is, sort by and in what order. The second isSorting AlgorithmWhich determines the sorting efficiency.

InCustom setWhen sorting content types, you must first define the sorting rules of that type.
Comparable InterfaceThis interface only defines oneCompareto (ObjectO)The Return Value of the method is an integer type. If the current object is greater than the parameter object, a positive number is returned. If the current object is equal to the parameter object, 0 is returned. If the current object is less than the parameter object, a negative value is returned, in this way, writing is in ascending order, and vice versa, it is in descending order. When the methods in this interface are implemented, there are only two return value definitions.

The comparable interface is implemented based on the sorting rules of the specified type, so that the set containing this type can be sorted as a whole. The comparable interface is also called a comparable interface. This interface is in the Java. lang package. This interface can be sorted.

Next we will introduce another pairMethod for sorting the set of custom object, That is, the implementationComparator interface (comparator)This interface definesCompare (ObjectO1, object O2)Method to compare two objects. The return value definition of this method is the same as the method described above.

Note: In the API, the parameter type of the preceding two methods in the help document is T, which indicates the template type, that is, the type of content stored in the Set, in jdk1. 4. The parameters are of the object type. The details of the template type are described in the new jdk5.0 feature.

The comparator interface can be implemented in an anonymous internal class. The sort (a collection object, comparator) method in collections can sort the set of custom types of content as a whole.

2>ShortlistIt is the implementation class of the List interface, and its underlying layer isBidirectional cyclic linked list.
Note:The query efficiency of arraylist is relatively high, and the efficiency of adding and deleting actions is relatively poor. It is suitable for query comparison.A set of elements that are frequently added and deleted..
The query efficiency of the shortlist is low, but the addition and deletion efficiency is high. It is applicable to Element Management sets that are frequently added or deleted and have a small number of queries..

Both arraylist and worker list are thread-insecure..

Implementation Stack
1,Array(Arraylist, Which is inefficient at adding, deleting, and not suitable)
2,Shortlist(A good way to implement the stack)
3,Java. util. Stack class. Stack is a subclass of vector, and Vector class is a thread-safe (a heavyweight class)And inherits the vector method. The verctor class (this class is also the implementation class of the List Interface) and arraylist functions are almost the same. (Stack is not recommended.).

Set interface implementation class

Hashset

The Set object of the set implementation class cannot have repeated elements. The hashset also uses an identifier to determine that the elements are not repeated, hashset uses an algorithm to ensure that elements in a hashset are unique,Underlying Implementation of hashset or Array.

The hashcode () method in the object class is that all subclasses will inherit this method. This method uses the hash algorithm to calculate a hash code value and return it, hashset uses the hash code value to modulo the array length (this modulo is the position where the object is to be stored in the array) when the elements in the array are the same as those of the object to be added, the elements in the array are added if they are different.

Hash is a hash algorithm.

Note:: Therefore, the custom classes in the collection objects to be stored in the hashset must overwrite the hashcode () and equals () methods to ensure that the elements in the collection are not repeated. When overwriting and hashcode () methods, make the hashcode () method of the same object return the same value, overwrite the equals () method, and then determine its content. To ensure efficiency, when overwriting the hashcode () method, try to make different objects return different hash code values as much as possible.

If the elements in the array and the hashcode () of the object to be added return the same hash value (same object), The equals () method is used to determine whether the content of the two objects is the same.

SortedsetAn interface is a sub-interface of set.
TreesetYesSortedsetInterface implementation class, which can sort the elements in the set.
To store the custom class objects in the treeset, this class either has implemented the comparable interface or can provide the comparator. The treeset can automatically filter out duplicate elements, so it does not need to overload hashcode () method: treeset checks whether the content of the element is the same based on the comparison rules. treeset sorts the elements after they are stored. (When the treeset provides sorting rules, you must pay attention to the conditions that the object content is equal, and be sure to consider that the content of the two objects is the same subjectively, in order to use fewer conditions for judgment)

The treeset class is used only when sorting is required (the storage efficiency is relatively low), and the storage efficiency of hashset is relatively high. When sorting hashset objects, you can put the elements in the hashset into the treeset class..


MapInterface implementation class
Map can only store key-value pairs (Key, value), where key cannot be repeated. Key and value correspond one by one.

HashmapIt is the implementation class of the map interface. Keys are stored out of order, and keys cannot be repeated. It also ensures that keys are not repeated through hash code values, key and value correspond one by one. If the key-value pair to be added is the same as the key of the key-value pair in the hashmap, the value of the key in the set is overwritten.When a custom type is used as the key, it overwrites the hashcode () and equals () methods.That is, the processing method is the same as that of the custom type to be put in the hashset. The object of this class isThread Security.

When traversing a map, you need to use its keyset () method to obtain a set of keys. You can traverse this set and use the get () method to obtain the value corresponding to the key, then, the map is traversed.

HashtableIt is also the implementation class of the map interface. It is similar to hashmap, but this class object isHeavyweightYesThread Security. The key and value cannot be null.

PropertiesThis class is a subclass of hashtable. Its key and value can only be strings.

SortedmapIs a map Sub-interface

TreemapIs the implementation class of sortedmap, Which is sorted by key. Like the treeset class, you must use a custom class to implement the comparable interface when using a custom class as the key.

Note:In fact, the bottom layer of hashset is a hashmap, but the value values are all null values, while the bottom layer of hashmap is implemented using arrays..

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.