Collection Framework Study Tips

xiaoxiao2021-03-06  118

Hashset class and Treeset class

"Collection Frame" supports two common implementations of the Set interface: Hashset and Treeset. In more cases, you will use the Hashset to store a collection of repeating freedom. Taking into account the efficiency, the object added to the Hashset needs to implement the HashCode () method in a way that is properly allocated. Although most system classes covers the default HashCode () implementation in Object, don't forget to override HashCode () when you create a class you want to add to havehset. The TreeSet implementation is useful when you want to extract an element in an ordered manner from a collection. In order to be smooth, the elements added to the Treeset must be sorted. "Collection Frame" adds support for the Comparable element, which will be described in detail in the "comparable interface" section. We temporarily and assume that a tree knows how to keep the Java.lang packaging prune class element. Generally, the elements are added to havehset, and the collection is converted to Treeset to make an orderly traversal will be faster.

To optimize the use of Hashset space, you can tune the initial capacity and load factors. Treeset does not include tuning options because the tree is always balanced, ensuring insertion, deletion, and query performance is log (n).

Hashset and Treeset implements a Cloneable interface. Add the same element in the Hashset, there is only one such element in the Hashset, and it is not repeated. The element is arranged in ascending order.

The HashMap class and the TreeMap class "Collection Framework" offer two conventional MAP implementations: HashMap and TreeMap. Like all specific implementations, which implementation uses depends on your specific needs. Insert, delete, and locate elements in Map, HashMap is the best choice. But if you want to traverse keys in order, TreeMap will be better. Depending on the size of the set, first add the elements to havehMap, and then convert this mapping into a TreeMap used to traverse the ordered key. The key class that uses HashMap as required to explicitly define the HashCode () implementation. With TreeMap implementation, the elements added to the map must be sorted. We will introduce in the sort.

To optimize the use of HashMap space, you can tune the initial capacity and load factors. This TreeMap has no tuning options because the tree is always equilibrium. HashMap and Treemap implements a Cloneable interface. Add the same element in HashMap, only one such element in HashMap is not repeated. The HashTable class and the Properties class are the historical implementation of the MAP interface.

"Collection Framework" provides a special SET interface: sortedset, which keeps the elements' ordered order. The interface is set by the set of subsets and its both ends (ie, and tail) provide an access method. When you handle the subset of the list, the change subseason reflects the source set. In addition, the change source set is also reflected in the subset. This happens because the subset is specified by the elements of both ends instead of the subscript element. Also, if FromeElement is part of the source set, it is part of the subset. But if TOELEMENT is part of the source set, it is not part of a subset. If you want a special high-end element (TO-Element) in subset, you must find the next element. For a String, the next element is the same string with empty characters (String "/ 0"). Adding to sORTEDSET must implement Comparable, otherwise you must provide a constructor for implementation classes to provide a Compare: Treeset (you can implement yourself. But "Collection Framework" only provides such a specific implementation class.) "Collection Framework provides a special MAP interface: sortedMap, which is used to hold the orderly order of the keys. This interface provides an access method for the subset of the maps including two endpoints. In addition to the sorting is the key acting on the mapping, SortedMap is processed, and the SortedSet is processed. The implementation class provided by the Collection Framework is TreeMap. Because each key can only correspond to a value, if the two keys generate zero return values ​​(by Compareto () method or through the Compare () method) method) So, the original key is replaced by the new value. If the two elements are equal, it is ok. But if you don't wait, then you should modify the comparison method, which makes the comparison method and equals ().

转载请注明原文地址:https://www.9cbs.com/read-125592.html

New Post(0)