Java Collection Series One: Collection Overview __java

Source: Internet
Author: User

Write in the beginning: A great God blog has a series of lectures on the collection of articles, write really good, remember to view the original address set Frame analysis: Original address (reprint)

This article is about the collection classification, the concept has the general introduction:
The set framework includes: interface, implementation class, Algorithm---
Interface: Collection interface, list interface, set interface, map interface, etc...
Collection classes: LinkedList, ArrayList, HashSet, HashMap, and so on, as well as the more common collection classes such as Vector,stack,properties.
Set algorithm: * * did not see what meaning **/disgrace
The article also introduced the concept of iterators, but did not expand on how to use the Java set, list, map of the difference between the original address (reprint)

This paper introduces the use of three kinds of sets and the method of collection combination.
Methods in the Collection interface: Boolean Add (Object o), void Clear (), IsEmpty (), contains (object o), size (), and so on.
Here's an emphasis: iterator iterator () and Object ToArray () methods: both are applicable to all elements in the collection, the former returns a iterator object, which is an array
Iterator interface: Included method: Hasnext () – Determines whether the collection is traversed, and returns true if it is not. Next () – returns the next element. Remove ()-Deletes the last element returned with next ()

Set interface: Set is the simplest kind of collection. Objects in the collection are not sorted in a particular way, and there are no duplicate objects. The set interface mainly implements two implementation classes:
The Hashset:hashset class accesses objects in the collection according to the hashing algorithm, and the access speed is faster
The Treeset:treeset class implements the SortedSet interface, which is able to sort the objects in the collection.
The set's Add () method determines whether the object is already stored in the collection.
Boolean isexists=false;
Iterator It=set.iterator ();
while (It.hasnext ()) {
String Oldstr=it.next ();
if (Newstr.equals (OLDSTR)) {
Isexists=true; } }

list: The feature of the list is that its elements are stored in a linear fashion, and the collection can hold duplicate objects.
The main implementation classes of the list interface include:
ArrayList (): Represents the length can change the group. Elements can be randomly accessed, and the insertion and deletion of elements into ArrayList () is slow.
LinkedList (): In the implementation of the use of linked list data structure. Fast insertion and deletion, slow access.
Using Iterators (iterator):
Iterator It=list.iterator ();
while (It.hashnext) {
System.out.println (It.next);
}

map (map):
A map is a collection of key and value object mappings, each of which contains a pair of key objects and value objects. When the map does not inherit from the collection interface to retrieve elements from the map collection, the corresponding value object is returned whenever the key object is given. Summary

the function method of list
There are actually two kinds of lis: One is the basic ArrayList, the advantage is the random access element, the other is the more powerful linkedlist, it is not designed for fast random access, but has a more general approach.

List: Order is the most important feature of the list: it guarantees that the order of the elements is maintained. List adds a number of methods to collection that allow you to insert and remove elements into the middle of the list (this is recommended only for linkedlist use.) A list can generate Listiterator, using it to traverse the list in two directions, or to insert and remove elements from the middle of the list.

ArrayList: A list implemented by an array. Allows quick random access to elements, but inserts and removes elements in the middle of the list is slow. Instead of inserting and removing elements, listiterator should only be used to traverse the ArrayList from the back forward. Because it's a lot bigger than the linkedlist overhead.

LinkedList: The sequential access is optimized, and the overhead of inserting and deleting into the list is not significant. Random access is relatively slow. (use ArrayList instead.) Also has the following methods: AddFirst (), AddLast (), GetFirst (), GetLast (), Removefirst (), and Removelast (), these methods (not defined in any interface or base class) Enables LinkedList to be used as stacks, queues, and bidirectional queues.

the function method of set
The set has exactly the same interface as collection, so there is no additional functionality, unlike the two different lists that preceded it. In fact set is collection, but behavior is different. (This is a typical application of inheritance and polymorphic thinking: behavior that behaves differently.) Set does not save duplicate elements (more responsible for determining the same element)

Set: Each element that is stored in the set must be unique because the set does not save the repeating element. Elements that join a set must define the Equals () method to ensure the uniqueness of the object. Set has exactly the same interface as collection. The set interface does not guarantee the order of elements to be maintained.
HashSet: Set for quick find design. The object that is stored in the HashSet must define HASHCODE ().
TreeSet: Save order set, bottom is tree structure. Use it to extract ordered sequences from set.
Linkedhashset: Has a hashset query speed and internally uses a linked list to maintain the order of elements (in the order of insertion). So when you use iterators to traverse set, the results are displayed in the order in which the elements are inserted.

the functional method of map
Method put (Object key, object value) adds a "value" (What you Want) and a key that is associated with the value (used to find it). The method get (Object key) returns the value associated with the given key. You can use ContainsKey () and Containsvalue () to test whether a "key" or "value" is included in the map. The standard Java class library contains several different map:hashmap, TreeMap, Linkedhashmap, Weakhashmap, and Identityhashmap. They all have the same basic interface map, but behavior, efficiency, sorting strategies, the lifecycle of saving objects, and the policy of determining "key" equivalence are different.

Execution efficiency is a big problem in map. Look at what gets () to do, and you'll see why it's rather slow to search for "keys" in ArrayList. And that's where hashmap is raising speed. HashMap uses a special value, called hash code, to replace a slow search for keys. "Hash code" is "relatively unique" to represent an int value of an object, which is generated by converting some of the object's information. All Java objects can produce hash codes, because Hashcode () is a method defined in the base class object.

HashMap is a quick query using an object's Hashcode (). This method can significantly improve performance.

MAP: Maintains the association of key-value pairs so that you can find the value by "key"

Hashmap:map is based on a hash table implementation. The cost of inserting and querying key-value pairs is fixed. The capacity capacity and load factor load factor can be set through the constructor to adjust the performance of the container.

Linkedhashmap: Similar to HashMap, but when iterating through it, the order in which "key value pairs" is obtained is its insertion order, or the order in which the least recent (LRU) is used. Just a little slower than HashMap. It is faster when iterating through the access, because it uses a linked list to maintain the internal order.

TREEMAP: The implementation of the data structure based on the red-black tree. When you view key or key-value pairs, they are sorted (in order by Comparabel or comparator). TreeMap is characterized by the fact that the results you get are sorted. TreeMap is the only map with a Submap () method that can return a subtree.

Weakhashmao: The object used in the weak key (weak key) Map,map is also allowed to be released: This is designed to solve special problems. This key can be reclaimed by the garbage collector if a reference other than the map points to a key.

Identifyhashmap:: Use = = instead of equals () hash map for "key" comparison. Designed to solve specific problems.

Support original, fight piracy

Related Article

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.